github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/conf/logger/ze_logger_test.go (about)

     1  package logger_test
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/google/uuid"
     8  	"github.com/pkg/errors"
     9  	"go.opentelemetry.io/otel"
    10  	"go.opentelemetry.io/otel/trace"
    11  
    12  	"github.com/machinefi/w3bstream/pkg/depends/base/types"
    13  	"github.com/machinefi/w3bstream/pkg/depends/conf/logger"
    14  	"github.com/machinefi/w3bstream/pkg/depends/conf/tracer"
    15  	"github.com/machinefi/w3bstream/pkg/depends/kit/logr"
    16  	"github.com/machinefi/w3bstream/pkg/depends/kit/metax"
    17  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/datatypes"
    18  )
    19  
    20  func ExampleSpanLogger() {
    21  	{
    22  		var c = logger.Config{
    23  			Level:  logr.DebugLevel,
    24  			Output: logger.OUTPUT_TYPE__NEVER,
    25  			Format: logger.FORMAT_TYPE__TEXT,
    26  		}
    27  
    28  		c.SetDefault()
    29  		if c.Init() != nil {
    30  			return
    31  		}
    32  
    33  		ctx := metax.ContextWithMeta(context.Background(), metax.Meta{"_id": {"from context"}, "operator": {"GetByID"}})
    34  		doLog(ctx, "OutputNever")
    35  	}
    36  
    37  	{
    38  		var c = logger.Config{
    39  			Level:  logr.InfoLevel,
    40  			Output: logger.OUTPUT_TYPE__ON_FAILURE,
    41  			Format: logger.FORMAT_TYPE__JSON,
    42  		}
    43  
    44  		c.SetDefault()
    45  		if c.Init() != nil {
    46  			return
    47  		}
    48  
    49  		ctx := metax.ContextWithMeta(context.Background(), metax.Meta{"_id": {"from context"}, "operator": {"GetByID"}})
    50  		doLog(ctx, "OutputOnFailureText")
    51  	}
    52  
    53  	{
    54  		var c = logger.Config{
    55  			Output: logger.OUTPUT_TYPE__ALWAYS,
    56  			Format: logger.FORMAT_TYPE__TEXT,
    57  			Level:  logr.DebugLevel,
    58  		}
    59  
    60  		c.SetDefault()
    61  		if c.Init() != nil {
    62  			return
    63  		}
    64  
    65  		ctx := metax.ContextWithMeta(context.Background(), metax.Meta{"_id": {"from context"}, "operator": {"GetByID"}})
    66  		doLog(ctx, "OutputAlwaysText")
    67  	}
    68  
    69  	{
    70  		var c = logger.Config{
    71  			Output: logger.OUTPUT_TYPE__ALWAYS,
    72  			Format: logger.FORMAT_TYPE__JSON,
    73  			Level:  logr.DebugLevel,
    74  		}
    75  
    76  		c.SetDefault()
    77  		if c.Init() != nil {
    78  			return
    79  		}
    80  
    81  		ctx := metax.ContextWithMeta(context.Background(), metax.Meta{"_id": {"from context"}, "operator": {"GetByID"}})
    82  		doLog(ctx, "OutputAlwaysJson")
    83  	}
    84  
    85  	_ = tr.Shutdown(context.Background())
    86  
    87  	// Output:
    88  }
    89  
    90  var tr *tracer.Config
    91  
    92  func init() {
    93  	tr = &tracer.Config{
    94  		GrpcEndpoint: types.Endpoint{
    95  			Scheme:   "http",
    96  			Hostname: "localhost",
    97  			Port:     4317,
    98  		},
    99  		ServiceName:    "test_log",
   100  		ServiceVersion: "1.0.0",
   101  		InstanceID:     uuid.NewString(),
   102  		DebugMode:      datatypes.TRUE,
   103  	}
   104  	tr.SetDefault()
   105  	if err := tr.Init(); err != nil {
   106  		panic(err)
   107  	}
   108  }
   109  
   110  func doLog(ctx context.Context, name string) {
   111  	println(name)
   112  	defer println()
   113  
   114  	t := otel.Tracer(name)
   115  
   116  	ctx, span := t.Start(ctx, "op", trace.WithTimestamp(time.Now()))
   117  	defer func() {
   118  		span.End(trace.WithTimestamp(time.Now()))
   119  	}()
   120  
   121  	ctx = logr.WithLogger(ctx, logger.SpanLogger(span))
   122  
   123  	someActionWithSpanAndLog(ctx)
   124  
   125  	otherActionsLogOnly(ctx)
   126  }
   127  
   128  func someActionWithSpanAndLog(ctx context.Context) {
   129  	_, l := logr.Start(ctx, "SomeActionWithSpan")
   130  	defer l.End()
   131  
   132  	l.Info("msg")
   133  	l.Debug("msg")
   134  	l.Warn(errors.New("err"))
   135  	l.Error(errors.New("err"))
   136  }
   137  
   138  func otherActionsLogOnly(ctx context.Context) {
   139  	l := logr.FromContext(ctx)
   140  
   141  	l.WithValues("test_key", 2).Info("test")
   142  	l.Error(errors.New("any"))
   143  }