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

     1  package logger
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log/slog"
     7  
     8  	"github.com/machinefi/w3bstream/pkg/depends/kit/logr"
     9  )
    10  
    11  func Std() logr.Logger { return &std{lvl: logr.DebugLevel} }
    12  
    13  func StdContext(ctx context.Context) (context.Context, logr.Logger) {
    14  	l := Std()
    15  	return logr.WithLogger(ctx, l), l
    16  }
    17  
    18  type std struct {
    19  	lvl logr.Level
    20  	kvs []interface{}
    21  }
    22  
    23  func (l *std) WithValues(kvs ...interface{}) logr.Logger {
    24  	return &std{
    25  		lvl: l.lvl,
    26  		kvs: append(l.kvs, kvs...),
    27  	}
    28  }
    29  
    30  func (l *std) Start(ctx context.Context, name string, kvs ...interface{}) (context.Context, logr.Logger) {
    31  	return ctx, &std{
    32  		lvl: l.lvl,
    33  		kvs: append(l.kvs, kvs...),
    34  	}
    35  }
    36  
    37  func (l *std) End() {}
    38  
    39  func (l *std) Debug(format string, args ...interface{}) {
    40  	gStdLogger.LogAttrs(context.Background(), slog.LevelDebug, fmt.Sprintf(format, args...), KVsToSlogAttr(l.kvs...)...)
    41  }
    42  
    43  func (l *std) Info(format string, args ...interface{}) {
    44  	gStdLogger.LogAttrs(context.Background(), slog.LevelInfo, fmt.Sprintf(format, args...), KVsToSlogAttr(l.kvs...)...)
    45  }
    46  
    47  func (l *std) Warn(err error) {
    48  	gStdLogger.LogAttrs(context.Background(), slog.LevelWarn, err.Error(), KVsToSlogAttr(l.kvs...)...)
    49  }
    50  
    51  func (l *std) Error(err error) {
    52  	gStdLogger.LogAttrs(context.Background(), slog.LevelError, err.Error(), KVsToSlogAttr(l.kvs...)...)
    53  }