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