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

     1  package log
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/sirupsen/logrus"
     8  
     9  	"github.com/machinefi/w3bstream/pkg/depends/x/ptrx"
    10  )
    11  
    12  func Std() Logger { return &std{lvl: DebugLevel} }
    13  
    14  func StdContext(ctx context.Context) (context.Context, Logger) {
    15  	l := Std()
    16  	return WithLogger(ctx, l), l
    17  }
    18  
    19  type std struct {
    20  	lvl   Level
    21  	spans []string
    22  	kvs   []interface{}
    23  }
    24  
    25  func (l *std) SetLevel(lvl Level) Logger {
    26  	logger := ptrx.Ptr(*l)
    27  	logger.lvl = lvl
    28  	return logger
    29  }
    30  
    31  func (l *std) WithValues(kvs ...interface{}) Logger {
    32  	return &std{
    33  		lvl:   l.lvl,
    34  		spans: l.spans,
    35  		kvs:   append(l.kvs, kvs...),
    36  	}
    37  }
    38  
    39  func (l *std) Start(ctx context.Context, name string, kvs ...interface{}) (context.Context, Logger) {
    40  	return ctx, &std{
    41  		lvl:   l.lvl,
    42  		spans: append(l.spans, name),
    43  		kvs:   append(l.kvs, kvs...),
    44  	}
    45  }
    46  
    47  func (l *std) End() {
    48  	if len(l.spans) != 0 {
    49  		l.spans = l.spans[0 : len(l.spans)-1]
    50  	}
    51  }
    52  
    53  func (l *std) Trace(format string, args ...interface{}) {
    54  	if TraceLevel <= l.lvl {
    55  		logrus.WithFields(KeyValues(l.kvs...)).Tracef(format, args...)
    56  	}
    57  }
    58  
    59  func (l *std) Debug(format string, args ...interface{}) {
    60  	if DebugLevel <= l.lvl {
    61  		logrus.WithFields(KeyValues(l.kvs...)).Debugf(format, args...)
    62  	}
    63  }
    64  
    65  func (l *std) Info(format string, args ...interface{}) {
    66  	if InfoLevel <= l.lvl {
    67  		logrus.WithFields(KeyValues(l.kvs...)).Infof(format, args...)
    68  	}
    69  }
    70  
    71  func (l *std) Warn(err error) {
    72  	if WarnLevel <= l.lvl {
    73  		logrus.WithFields(KeyValues(l.kvs...)).Warn(err)
    74  	}
    75  }
    76  
    77  func (l *std) Error(err error) {
    78  	if ErrorLevel <= l.lvl {
    79  		logrus.WithFields(KeyValues(l.kvs...)).Error(err)
    80  	}
    81  }
    82  
    83  func (l *std) Fatal(err error) {
    84  	if FatalLevel <= l.lvl {
    85  		logrus.WithFields(KeyValues(l.kvs...)).Fatal(err)
    86  	}
    87  }
    88  
    89  func (l *std) Panic(err error) {
    90  	if PanicLevel < l.lvl {
    91  		logrus.WithFields(KeyValues(l.kvs...)).Panic(err)
    92  	}
    93  }
    94  
    95  func KeyValues(kvs ...interface{}) logrus.Fields {
    96  	if len(kvs)%2 != 0 {
    97  		return nil
    98  	}
    99  
   100  	fields := logrus.Fields{}
   101  
   102  	for i := 0; i < len(kvs); i += 2 {
   103  		fields[fmt.Sprintf("%v", kvs[i])] = kvs[i+1]
   104  	}
   105  
   106  	return fields
   107  }