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

     1  package log
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/sirupsen/logrus"
     9  
    10  	"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
    11  )
    12  
    13  type Logger interface {
    14  	// Start to start span for tracing
    15  	//
    16  	// 	ctx log = log.Start(ctx, "SpanName")
    17  	// 	defer log.End()
    18  	//
    19  	Start(context.Context, string, ...interface{}) (context.Context, Logger)
    20  	// End to end span
    21  	End()
    22  
    23  	// WithValues key value pairs
    24  	WithValues(keyAndValues ...interface{}) Logger
    25  
    26  	Trace(msg string, args ...interface{})
    27  	Debug(msg string, args ...interface{})
    28  	Info(msg string, args ...interface{})
    29  	Warn(err error)
    30  	Error(err error)
    31  	Fatal(err error)
    32  	Panic(err error)
    33  }
    34  
    35  type keyLogger struct{}
    36  
    37  func WithLogger(ctx context.Context, l Logger) context.Context {
    38  	return contextx.WithValue(ctx, keyLogger{}, l)
    39  }
    40  
    41  func WithLoggerContext(l Logger) contextx.WithContext {
    42  	return func(ctx context.Context) context.Context {
    43  		return WithLogger(ctx, l)
    44  	}
    45  }
    46  
    47  func FromContext(ctx context.Context) Logger {
    48  	if v, ok := ctx.Value(keyLogger{}).(Logger); ok {
    49  		return v
    50  	}
    51  	return Discard()
    52  }
    53  
    54  func Start(ctx context.Context, name string, kvs ...interface{}) (context.Context, Logger) {
    55  	return FromContext(ctx).Start(ctx, name, kvs...)
    56  }
    57  
    58  type LevelSetter interface {
    59  	SetLevel(lvl Level) Logger
    60  }
    61  
    62  // Level type
    63  type Level uint32
    64  
    65  const (
    66  	// PanicLevel level, the highest level of severity. Logs and then calls panic with the
    67  	// message passed to Debug, Info, ...
    68  	PanicLevel Level = iota
    69  	// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
    70  	// logging level is set to Panic.
    71  	FatalLevel
    72  	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
    73  	// Commonly used for hooks to send errors to an error tracking service.
    74  	ErrorLevel
    75  	// WarnLevel level. Non-critical entries that deserve eyes.
    76  	WarnLevel
    77  	// InfoLevel level. General operational entries about what's going on inside the
    78  	// application.
    79  	InfoLevel
    80  	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
    81  	DebugLevel
    82  	// TraceLevel level. Designates finer-grained informational events than the Debug.
    83  	TraceLevel
    84  )
    85  
    86  // ParseLevel takes a string level and returns the Logrus log level constant.
    87  func ParseLevel(lvl string) (Level, error) {
    88  	switch strings.ToLower(lvl) {
    89  	case "panic":
    90  		return PanicLevel, nil
    91  	case "fatal":
    92  		return FatalLevel, nil
    93  	case "error":
    94  		return ErrorLevel, nil
    95  	case "warn", "warning":
    96  		return WarnLevel, nil
    97  	case "info":
    98  		return InfoLevel, nil
    99  	case "debug":
   100  		return DebugLevel, nil
   101  	case "trace":
   102  		return TraceLevel, nil
   103  	}
   104  
   105  	var l Level
   106  	return l, fmt.Errorf("not a valid level: %q", lvl)
   107  }
   108  
   109  func (lvl Level) String() string {
   110  	if b, err := lvl.MarshalText(); err == nil {
   111  		return string(b)
   112  	} else {
   113  		return "unknown"
   114  	}
   115  }
   116  
   117  func (lvl *Level) UnmarshalText(text []byte) error {
   118  	l, err := ParseLevel(string(text))
   119  	if err != nil {
   120  		return err
   121  	}
   122  	*lvl = l
   123  	return nil
   124  }
   125  
   126  func (lvl Level) MarshalText() ([]byte, error) {
   127  	switch lvl {
   128  	case TraceLevel:
   129  		return []byte("trace"), nil
   130  	case DebugLevel:
   131  		return []byte("debug"), nil
   132  	case InfoLevel:
   133  		return []byte("info"), nil
   134  	case WarnLevel:
   135  		return []byte("warning"), nil
   136  	case ErrorLevel:
   137  		return []byte("error"), nil
   138  	case FatalLevel:
   139  		return []byte("fatal"), nil
   140  	case PanicLevel:
   141  		return []byte("panic"), nil
   142  	}
   143  
   144  	return nil, fmt.Errorf("not a valid level %d", lvl)
   145  }
   146  
   147  func (lvl Level) LogrusLogLevel() logrus.Level { return logrus.Level(lvl) }