trpc.group/trpc-go/trpc-go@v1.0.3/log/example_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package log_test
    15  
    16  import "trpc.group/trpc-go/trpc-go/log"
    17  
    18  func Example() {
    19  	l := log.NewZapLog([]log.OutputConfig{
    20  		{
    21  			Writer:       "console",
    22  			Level:        "debug",
    23  			Formatter:    "console",
    24  			FormatConfig: log.FormatConfig{TimeFmt: "xxx"},
    25  		},
    26  	})
    27  	const defaultLoggerName = "default"
    28  	oldDefaultLogger := log.GetDefaultLogger()
    29  	log.Register(defaultLoggerName, l)
    30  	defer func() {
    31  		log.Register(defaultLoggerName, oldDefaultLogger)
    32  	}()
    33  
    34  	l = log.With(log.Field{Key: "tRPC-Go", Value: "log"})
    35  	l.Trace("hello world")
    36  	l.Debug("hello world")
    37  	l.Info("hello world")
    38  	l.Warn("hello world")
    39  	l.Error("hello world")
    40  	l.Tracef("hello world")
    41  	l.Debugf("hello world")
    42  	l.Infof("hello world")
    43  	l.Warnf("hello world")
    44  	l.Errorf("hello world")
    45  
    46  	// Output:
    47  	// xxx	DEBUG	log/example_test.go:35	hello world	{"tRPC-Go": "log"}
    48  	// xxx	DEBUG	log/example_test.go:36	hello world	{"tRPC-Go": "log"}
    49  	// xxx	INFO	log/example_test.go:37	hello world	{"tRPC-Go": "log"}
    50  	// xxx	WARN	log/example_test.go:38	hello world	{"tRPC-Go": "log"}
    51  	// xxx	ERROR	log/example_test.go:39	hello world	{"tRPC-Go": "log"}
    52  	// xxx	DEBUG	log/example_test.go:40	hello world	{"tRPC-Go": "log"}
    53  	// xxx	DEBUG	log/example_test.go:41	hello world	{"tRPC-Go": "log"}
    54  	// xxx	INFO	log/example_test.go:42	hello world	{"tRPC-Go": "log"}
    55  	// xxx	WARN	log/example_test.go:43	hello world	{"tRPC-Go": "log"}
    56  	// xxx	ERROR	log/example_test.go:44	hello world	{"tRPC-Go": "log"}
    57  }