github.com/jxskiss/gopkg@v0.17.3/zlog/example_test.go (about)

     1  package zlog
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  
     7  	"go.uber.org/zap"
     8  	"go.uber.org/zap/zapcore"
     9  )
    10  
    11  func testHelperReplaceGlobalsToStdout(ctxFunc func(ctx context.Context, args CtxArgs) CtxResult) func() {
    12  	cfg := &Config{
    13  		Level:             "trace",
    14  		Format:            "json",
    15  		DisableTimestamp:  true,
    16  		DisableCaller:     true,
    17  		DisableStacktrace: true,
    18  		GlobalConfig: GlobalConfig{
    19  			CtxFunc: ctxFunc,
    20  		},
    21  	}
    22  	l, p, err := NewWithOutput(cfg, zapcore.AddSync(os.Stdout))
    23  	if err != nil {
    24  		panic(err)
    25  	}
    26  	return ReplaceGlobals(l, p)
    27  }
    28  
    29  func ExampleBuilder() {
    30  	defer testHelperReplaceGlobalsToStdout(nil)()
    31  
    32  	logger := B(context.TODO()).
    33  		Named("example_builder").
    34  		Method().
    35  		With(zap.String("k1", "v1"), zap.Int64("k2", 54321)).
    36  		Build()
    37  	logger.Info("example builder")
    38  
    39  	// Output:
    40  	// {"level":"info","logger":"example_builder","msg":"example builder","methodName":"zlog.ExampleBuilder","k1":"v1","k2":54321}
    41  }
    42  
    43  func ExampleBuilder_namespace() {
    44  	defer testHelperReplaceGlobalsToStdout(nil)()
    45  
    46  	builder := B(nil).
    47  		With(zap.String("k1", "v1"), zap.String("k2", "v2")).
    48  		With(zap.Namespace("subns"))
    49  	builder = builder.With(zap.String("k1", "sub1"), zap.String("k2", "sub2"))
    50  	builder.Build().Info("example builder namespace")
    51  
    52  	// Output:
    53  	// {"level":"info","msg":"example builder namespace","k1":"v1","k2":"v2","subns":{"k1":"sub1","k2":"sub2"}}
    54  }
    55  
    56  func ExampleBuilder_newNamespace() {
    57  	defer testHelperReplaceGlobalsToStdout(nil)()
    58  
    59  	builder := B(nil)
    60  	builder = builder.With(zap.String("k1", "v1"), zap.String("k2", "v2"))
    61  	builder = builder.With(
    62  		zap.String("k1", "override"),
    63  		zap.Namespace("subns"),
    64  		zap.String("k1", "sub1"), zap.String("k2", "sub2"))
    65  	builder.Build().Info("example builder new namespace")
    66  
    67  	// Output:
    68  	// {"level":"info","msg":"example builder new namespace","k1":"override","k2":"v2","subns":{"k1":"sub1","k2":"sub2"}}
    69  }
    70  
    71  func ExampleWithBuilder() {
    72  	defer testHelperReplaceGlobalsToStdout(nil)()
    73  
    74  	// Make a Builder.
    75  	builder := B(context.TODO()).
    76  		Method().
    77  		With(zap.String("k1", "v1"), zap.Int64("k2", 54321))
    78  	builder.Build().Info("with builder")
    79  
    80  	// Pass it to another function or goroutine.
    81  	ctx := WithBuilder(context.Background(), builder)
    82  
    83  	func(ctx context.Context) {
    84  		builder := B(ctx). // get Builder from ctx
    85  					Method().                       // override the method name
    86  					With(zap.String("k1", "inner")) // override "k1"
    87  
    88  		// do something
    89  
    90  		builder.Build().Info("another function")
    91  	}(ctx)
    92  
    93  	// Output:
    94  	// {"level":"info","msg":"with builder","methodName":"zlog.ExampleWithBuilder","k1":"v1","k2":54321}
    95  	// {"level":"info","msg":"another function","methodName":"zlog.ExampleWithBuilder.func1","k1":"inner","k2":54321}
    96  }
    97  
    98  func ExampleWith() {
    99  	defer testHelperReplaceGlobalsToStdout(nil)()
   100  
   101  	With(zap.String("k1", "v1"), zap.Int64("k2", 54321)).
   102  		Info("example with")
   103  
   104  	// Output:
   105  	// {"level":"info","msg":"example with","k1":"v1","k2":54321}
   106  }
   107  
   108  func ExampleWithCtx() {
   109  
   110  	demoCtxFunc := func(ctx context.Context, args CtxArgs) CtxResult {
   111  		return CtxResult{
   112  			Fields: []zap.Field{zap.String("ctx1", "v1"), zap.Int64("ctx2", 123)},
   113  		}
   114  	}
   115  	defer testHelperReplaceGlobalsToStdout(demoCtxFunc)()
   116  
   117  	logger := WithCtx(context.Background(),
   118  		zap.String("k3", "v3"),         // add a new field
   119  		zap.String("ctx2", "override"), // override "ctx2" from context
   120  	)
   121  	logger.Info("example with ctx")
   122  
   123  	// Output:
   124  	// {"level":"info","msg":"example with ctx","ctx1":"v1","ctx2":"override","k3":"v3"}
   125  }