github.com/ethereum/go-ethereum@v1.14.3/log/root.go (about)

     1  package log
     2  
     3  import (
     4  	"log/slog"
     5  	"os"
     6  	"sync/atomic"
     7  )
     8  
     9  var root atomic.Value
    10  
    11  func init() {
    12  	root.Store(&logger{slog.New(DiscardHandler())})
    13  }
    14  
    15  // SetDefault sets the default global logger
    16  func SetDefault(l Logger) {
    17  	root.Store(l)
    18  	if lg, ok := l.(*logger); ok {
    19  		slog.SetDefault(lg.inner)
    20  	}
    21  }
    22  
    23  // Root returns the root logger
    24  func Root() Logger {
    25  	return root.Load().(Logger)
    26  }
    27  
    28  // The following functions bypass the exported logger methods (logger.Debug,
    29  // etc.) to keep the call depth the same for all paths to logger.Write so
    30  // runtime.Caller(2) always refers to the call site in client code.
    31  
    32  // Trace is a convenient alias for Root().Trace
    33  //
    34  // Log a message at the trace level with context key/value pairs
    35  //
    36  // # Usage
    37  //
    38  //	log.Trace("msg")
    39  //	log.Trace("msg", "key1", val1)
    40  //	log.Trace("msg", "key1", val1, "key2", val2)
    41  func Trace(msg string, ctx ...interface{}) {
    42  	Root().Write(LevelTrace, msg, ctx...)
    43  }
    44  
    45  // Debug is a convenient alias for Root().Debug
    46  //
    47  // Log a message at the debug level with context key/value pairs
    48  //
    49  // # Usage Examples
    50  //
    51  //	log.Debug("msg")
    52  //	log.Debug("msg", "key1", val1)
    53  //	log.Debug("msg", "key1", val1, "key2", val2)
    54  func Debug(msg string, ctx ...interface{}) {
    55  	Root().Write(slog.LevelDebug, msg, ctx...)
    56  }
    57  
    58  // Info is a convenient alias for Root().Info
    59  //
    60  // Log a message at the info level with context key/value pairs
    61  //
    62  // # Usage Examples
    63  //
    64  //	log.Info("msg")
    65  //	log.Info("msg", "key1", val1)
    66  //	log.Info("msg", "key1", val1, "key2", val2)
    67  func Info(msg string, ctx ...interface{}) {
    68  	Root().Write(slog.LevelInfo, msg, ctx...)
    69  }
    70  
    71  // Warn is a convenient alias for Root().Warn
    72  //
    73  // Log a message at the warn level with context key/value pairs
    74  //
    75  // # Usage Examples
    76  //
    77  //	log.Warn("msg")
    78  //	log.Warn("msg", "key1", val1)
    79  //	log.Warn("msg", "key1", val1, "key2", val2)
    80  func Warn(msg string, ctx ...interface{}) {
    81  	Root().Write(slog.LevelWarn, msg, ctx...)
    82  }
    83  
    84  // Error is a convenient alias for Root().Error
    85  //
    86  // Log a message at the error level with context key/value pairs
    87  //
    88  // # Usage Examples
    89  //
    90  //	log.Error("msg")
    91  //	log.Error("msg", "key1", val1)
    92  //	log.Error("msg", "key1", val1, "key2", val2)
    93  func Error(msg string, ctx ...interface{}) {
    94  	Root().Write(slog.LevelError, msg, ctx...)
    95  }
    96  
    97  // Crit is a convenient alias for Root().Crit
    98  //
    99  // Log a message at the crit level with context key/value pairs, and then exit.
   100  //
   101  // # Usage Examples
   102  //
   103  //	log.Crit("msg")
   104  //	log.Crit("msg", "key1", val1)
   105  //	log.Crit("msg", "key1", val1, "key2", val2)
   106  func Crit(msg string, ctx ...interface{}) {
   107  	Root().Write(LevelCrit, msg, ctx...)
   108  	os.Exit(1)
   109  }
   110  
   111  // New returns a new logger with the given context.
   112  // New is a convenient alias for Root().New
   113  func New(ctx ...interface{}) Logger {
   114  	return Root().With(ctx...)
   115  }