github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/conf/log/logger_z_log_test.go (about) 1 package log_test 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/pkg/errors" 9 "go.opentelemetry.io/otel" 10 "go.opentelemetry.io/otel/exporters/zipkin" 11 "go.opentelemetry.io/otel/trace" 12 13 . "github.com/machinefi/w3bstream/pkg/depends/conf/log" 14 "github.com/machinefi/w3bstream/pkg/depends/kit/metax" 15 ) 16 17 func ExampleLogger() { 18 ctx := WithLogger(context.Background(), Std()) 19 20 l := FromContext(ctx).WithValues("k", "k") 21 22 l.Debug("test %d", 1) 23 l.Trace("test %d", 1) 24 l.Info("test %d", 1) 25 // Output: 26 } 27 28 func ExampleLogger_Start() { 29 ctx := WithLogger(context.Background(), Std()) 30 31 _, l := Start(ctx, "span", "k", "k") 32 defer l.End() 33 34 l.Debug("test %d", 1) 35 l.Trace("test %d", 1) 36 l.Info("test %d", 1) 37 // Output: 38 } 39 40 var ( 41 exporter, _ = zipkin.New("http://localhost:9411/api/v2/spans") 42 ) 43 44 func TestLog(t *testing.T) { 45 t.Run("OutputOnNever", func(t *testing.T) { 46 var logger = Log{ 47 Level: DebugLevel, 48 Output: LOGGER_OUTPUT_TYPE__NEVER, 49 Format: LOGGER_FORMAT_TYPE__TEXT, 50 Exporter: exporter, 51 } 52 53 logger.SetDefault() 54 logger.Init() 55 56 ctx := metax.ContextWithMeta(context.Background(), metax.Meta{"_id": {"from context"}, "operator": {"GetByID"}}) 57 doLog(ctx) 58 }) 59 60 t.Run("OutputOnFailure", func(t *testing.T) { 61 var logger = Log{ 62 Level: DebugLevel, 63 Output: LOGGER_OUTPUT_TYPE__ON_FAILURE, 64 Format: LOGGER_FORMAT_TYPE__TEXT, 65 Exporter: exporter, 66 } 67 68 logger.SetDefault() 69 logger.Init() 70 71 ctx := metax.ContextWithMeta(context.Background(), metax.Meta{"_id": {"from context"}, "operator": {"GetByID"}}) 72 doLog(ctx) 73 }) 74 75 t.Run("OutputAlways", func(t *testing.T) { 76 var logger = Log{ 77 Output: LOGGER_OUTPUT_TYPE__ALWAYS, 78 Format: LOGGER_FORMAT_TYPE__TEXT, 79 Level: DebugLevel, 80 Exporter: exporter, 81 } 82 83 logger.SetDefault() 84 logger.Init() 85 86 ctx := metax.ContextWithMeta(context.Background(), metax.Meta{"_id": {"from context"}, "operator": {"GetByID"}}) 87 doLog(ctx) 88 }) 89 90 t.Run("OutputAlwaysJSON", func(t *testing.T) { 91 var logger = Log{ 92 Output: LOGGER_OUTPUT_TYPE__ALWAYS, 93 Format: LOGGER_FORMAT_TYPE__JSON, 94 Level: DebugLevel, 95 Exporter: exporter, 96 } 97 98 logger.SetDefault() 99 logger.Init() 100 101 ctx := metax.ContextWithMeta(context.Background(), metax.Meta{"_id": {"from context"}, "operator": {"GetByID"}}) 102 doLog(ctx) 103 }) 104 } 105 106 func doLog(ctx context.Context) { 107 tracerName := "xxx" 108 tracer := otel.Tracer(tracerName) 109 110 ctx, span := tracer.Start(ctx, "op", trace.WithTimestamp(time.Now())) 111 defer func() { 112 span.End(trace.WithTimestamp(time.Now())) 113 }() 114 115 ctx = WithLogger(ctx, Span(tracerName, span)) 116 117 someActionWithSpan(ctx) 118 119 otherActions(ctx) 120 } 121 122 func someActionWithSpan(ctx context.Context) { 123 _, log := Start(ctx, "SomeActionWithSpan") 124 defer log.End() 125 126 log.Info("info xxx") 127 log.Debug("debug xxx") 128 log.Warn(errors.New("warn xxx")) 129 } 130 131 func otherActions(ctx context.Context) { 132 log := FromContext(ctx) 133 134 log.WithValues("test_key", 2).Info("test") 135 log.Error(errors.New("")) 136 }