github.com/matrixorigin/matrixone@v1.2.0/pkg/common/log/example_log_test.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package log_test
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/log"
    22  	"github.com/matrixorigin/matrixone/pkg/logutil"
    23  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    24  	"github.com/matrixorigin/matrixone/pkg/util/trace"
    25  	"go.uber.org/zap"
    26  )
    27  
    28  func ExampleGetServiceLogger() {
    29  	logger := getServiceLogger(metadata.ServiceType_CN, "cn0")
    30  	logger.Info("this is a info log")
    31  	// Output logs:
    32  	// 2022/11/17 15:25:49.375367 +0800 INFO cn-service log/example_log_test.go:32 this is a info log {"uuid": "cn0"}
    33  }
    34  
    35  func ExampleGetModuleLogger() {
    36  	cnServiceLogger := getServiceLogger(metadata.ServiceType_CN, "cn0")
    37  	txnClientLogger := log.GetModuleLogger(cnServiceLogger, log.TxnClient)
    38  	txnClientLogger.Info("this is a info log")
    39  	// Output logs:
    40  	// 2022/11/17 15:27:24.562799 +0800 INFO cn-service.txn-client log/example_log_test.go:40 this is a info log {"uuid": "cn0"}
    41  }
    42  
    43  func ExampleMOLogger_Info() {
    44  	getServiceLogger(metadata.ServiceType_CN, "cn0").Info("this is a info log")
    45  	// Output logs:
    46  	// 2022/11/17 15:27:52.036861 +0800 INFO cn-service log/example_log_test.go:46 this is a info log {"uuid": "cn0"}
    47  }
    48  
    49  func ExampleMOLogger_Debug() {
    50  	getServiceLogger(metadata.ServiceType_CN, "cn0").Debug("this is a debug log")
    51  	// Output logs:
    52  	// 2022/11/17 15:27:52.036861 +0800 DEBUG cn-service log/example_log_test.go:52 this is a debug log {"uuid": "cn0"}
    53  }
    54  
    55  func ExampleMOLogger_Error() {
    56  	getServiceLogger(metadata.ServiceType_CN, "cn0").Error("this is a error log")
    57  	// Output logs:
    58  	// 2022/11/17 15:27:52.036861 +0800 ERROR cn-service log/example_log_test.go:58 this is a error log {"uuid": "cn0"}
    59  	// error stacks
    60  }
    61  
    62  func ExampleMOLogger_Warn() {
    63  	getServiceLogger(metadata.ServiceType_CN, "cn0").Warn("this is a warn log")
    64  	// Output logs:
    65  	// 2022/11/17 15:27:52.036861 +0800 WARN cn-service log/example_log_test.go:65 this is a warn log {"uuid": "cn0"}
    66  }
    67  
    68  func ExampleMOLogger_Panic() {
    69  	getServiceLogger(metadata.ServiceType_CN, "cn0").Panic("this is a panic log")
    70  	// Output logs:
    71  	// 2022/11/17 15:27:52.036861 +0800 PANIC cn-service log/example_log_test.go:71 this is a panic log {"uuid": "cn0"}
    72  	// panic stacks...
    73  }
    74  
    75  func ExampleMOLogger_Fatal() {
    76  	getServiceLogger(metadata.ServiceType_CN, "cn0").Fatal("this is a fatal log")
    77  	// Output logs:
    78  	// 2022/11/17 15:27:52.036861 +0800 FATAL cn-service log/example_log_test.go:78 this is a fatal log {"uuid": "cn0"}
    79  	// fatal stacks...
    80  }
    81  
    82  func ExampleMOLogger_Log() {
    83  	getServiceLogger(metadata.ServiceType_CN, "cn0").Log("this is a example log",
    84  		log.DefaultLogOptions(),
    85  		zap.String("field-1", "field-1"),
    86  		zap.String("field-2", "field-2"))
    87  	// Output logs:
    88  	// 2022/11/17 15:27:52.036861 +0800 INFO cn-service log/example_log_test.go:85 this is a example log {"uuid": "cn0", "field-1": "field-1", "field-2": "field-2"}
    89  }
    90  
    91  func ExampleMOLogger_LogAction() {
    92  	someAction()
    93  	// Output logs:
    94  	// 2022/11/17 15:28:15.599321 +0800 INFO cn-service log/example_log_test.go:125 do action {"uuid": "cn0"}
    95  	// 2022/11/17 15:28:16.600754 +0800 INFO cn-service log/example_log_test.go:127 do action {"uuid": "cn0", "cost": "1.001430792s"}
    96  }
    97  
    98  func ExampleMOLogger_WithProcess() {
    99  	processStep1InCNWithoutID()
   100  	processStep2InTNWithoutID()
   101  	processStep3InLOGWithoutID()
   102  
   103  	// Output logs:
   104  	// 2022/11/17 15:36:04.724470 +0800 INFO cn-service log/example_log_test.go:131 step 1 {"uuid": "cn0", "process": "txn", "process-id": "txn uuid"}
   105  	// 2022/11/17 15:36:04.724797 +0800 INFO dn-service log/example_log_test.go:136 step 2 {"uuid": "dn0", "process": "txn", "process-id": "txn uuid"}
   106  	// 2022/11/17 15:36:04.724812 +0800 INFO log-service log/example_log_test.go:141 step 3 {"uuid": "log0", "process": "txn", "process-id": "txn uuid"}
   107  }
   108  
   109  func ExampleLogOptions_WithSample() {
   110  	logger := getServiceLogger(metadata.ServiceType_CN, "cn0")
   111  
   112  	n := 2
   113  	for i := 0; i < n; i++ {
   114  		logger.Log("example sample log",
   115  			log.DefaultLogOptions().WithSample(log.ExampleSample))
   116  	}
   117  	// Output logs:
   118  	// 2022/11/17 15:43:14.645242 +0800 INFO cn-service log/example_log_test.go:116 example sample log {"uuid": "cn0"}
   119  }
   120  
   121  func ExampleLogOptions_WithProcess() {
   122  	processStep1InCN("txn uuid")
   123  	processStep2InTN("txn uuid")
   124  	processStep3InLOG("txn uuid")
   125  
   126  	// Output logs:
   127  	// 2022/11/17 15:36:04.724470 +0800 INFO cn-service log/logger.go:51 step 1 {"uuid": "cn0", "process": "txn", "process-id": "txn uuid"}
   128  	// 2022/11/17 15:36:04.724797 +0800 INFO dn-service log/logger.go:51 step 2 {"uuid": "dn0", "process": "txn", "process-id": "txn uuid"}
   129  	// 2022/11/17 15:36:04.724812 +0800 INFO log-service log/logger.go:51 step 3 {"uuid": "log0", "process": "txn", "process-id": "txn uuid"}
   130  }
   131  
   132  func someAction() {
   133  	logger := getServiceLogger(metadata.ServiceType_CN, "cn0")
   134  	defer logger.InfoAction("do action")()
   135  	time.Sleep(time.Second)
   136  }
   137  
   138  func processStep1InCNWithoutID() {
   139  	logger := getServiceLogger(metadata.ServiceType_CN, "cn0").WithProcess(log.Txn)
   140  	logger.Log("step 1", log.DefaultLogOptions())
   141  }
   142  
   143  func processStep2InTNWithoutID() {
   144  	logger := getServiceLogger(metadata.ServiceType_TN, "dn0").WithProcess(log.Txn)
   145  	logger.Log("step 2", log.DefaultLogOptions())
   146  }
   147  
   148  func processStep3InLOGWithoutID() {
   149  	logger := getServiceLogger(metadata.ServiceType_LOG, "log0").WithProcess(log.Txn)
   150  	logger.Log("step 3", log.DefaultLogOptions())
   151  }
   152  
   153  func processStep1InCN(id string) {
   154  	logger := getServiceLogger(metadata.ServiceType_CN, "cn0")
   155  	logger.Log("step 1", log.DefaultLogOptions().WithProcess(log.Txn, id))
   156  }
   157  
   158  func processStep2InTN(id string) {
   159  	logger := getServiceLogger(metadata.ServiceType_TN, "dn0")
   160  	logger.Log("step 2", log.DefaultLogOptions().WithProcess(log.Txn, id))
   161  }
   162  
   163  func processStep3InLOG(id string) {
   164  	logger := getServiceLogger(metadata.ServiceType_LOG, "log0")
   165  	logger.Log("step 3", log.DefaultLogOptions().WithProcess(log.Txn, id))
   166  }
   167  
   168  func getServiceLogger(serviceType metadata.ServiceType, uuid string) *log.MOLogger {
   169  	return log.GetServiceLogger(logutil.GetGlobalLogger(), serviceType, uuid)
   170  }
   171  
   172  func ExampleMOLogger_WithContext() {
   173  	ctx, span := trace.Start(context.Background(), "ExampleMOLogger_WithContext")
   174  	defer span.End()
   175  	logger := getServiceLogger(metadata.ServiceType_CN, "cn0").WithContext(ctx)
   176  	logger.Info("info log 1, with Context in MOLogger.")
   177  	logger.Info("info log 2, with Context in MOLogger.")
   178  	// 2022/12/17 16:37:22.995805 +0800 INFO cn-service log/example_log_test.go:152 info log 1, with Context in MOLogger. {"uuid": "cn0", "span": {"trace_id": "349315cb-2044-f2dd-b4b1-e20365c8a944", "span_id": "fd898a22b375f34e"}}
   179  	// 2022/12/17 16:37:22.995812 +0800 INFO cn-service log/example_log_test.go:153 info log 2, with Context in MOLogger. {"uuid": "cn0", "span": {"trace_id": "349315cb-2044-f2dd-b4b1-e20365c8a944", "span_id": "fd898a22b375f34e"}}
   180  }
   181  
   182  func ExampleLogOptions_WithContext() {
   183  	ctx, span := trace.Start(context.Background(), "ExampleLogOptions_WithContext")
   184  	defer span.End()
   185  	logger := getServiceLogger(metadata.ServiceType_CN, "cn0")
   186  	logger.Log("this is an info log 1, with Context in LogOptions.", log.DefaultLogOptions().WithLevel(zap.InfoLevel).WithContext(ctx))
   187  	logger.Log("this is an info log 2, without Context.", log.DefaultLogOptions().WithLevel(zap.InfoLevel))
   188  	// 2022/12/17 16:37:22.995817 +0800 INFO cn-service log/example_log_test.go:162 this is an info log 1, with Context in LogOptions. {"uuid": "cn0", "span": {"trace_id": "9f0907c7-7fa6-bb7c-2a25-384f52e03cd5", "span_id": "068f75a50921c85f"}}
   189  	// 2022/12/17 16:37:22.995820 +0800 INFO cn-service log/example_log_test.go:163 this is an info log 2, without Context. {"uuid": "cn0"}
   190  }