github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/log/root.go (about) 1 package log 2 3 import ( 4 "os" 5 ) 6 7 var ( 8 root = &logger{[]interface{}{}, new(swapHandler)} 9 StdoutHandler = StreamHandler(os.Stdout, LogfmtFormat()) 10 StderrHandler = StreamHandler(os.Stderr, LogfmtFormat()) 11 ) 12 13 func init() { 14 root.SetHandler(DiscardHandler()) 15 } 16 17 // New returns a new logger with the given context. 18 // New is a convenient alias for Root().New 19 func New(ctx ...interface{}) Logger { 20 return root.New(ctx...) 21 } 22 23 // Root returns the root logger 24 func Root() Logger { 25 return root 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 func Trace(msg string, ctx ...interface{}) { 34 root.write(msg, LvlTrace, ctx, skipLevel) 35 } 36 37 // Debug is a convenient alias for Root().Debug 38 func Debug(msg string, ctx ...interface{}) { 39 root.write(msg, LvlDebug, ctx, skipLevel) 40 } 41 42 // Info is a convenient alias for Root().Info 43 func Info(msg string, ctx ...interface{}) { 44 root.write(msg, LvlInfo, ctx, skipLevel) 45 } 46 47 // Warn is a convenient alias for Root().Warn 48 func Warn(msg string, ctx ...interface{}) { 49 root.write(msg, LvlWarn, ctx, skipLevel) 50 } 51 52 // Error is a convenient alias for Root().Error 53 func Error(msg string, ctx ...interface{}) { 54 root.write(msg, LvlError, ctx, skipLevel) 55 } 56 57 // Crit is a convenient alias for Root().Crit 58 func Crit(msg string, ctx ...interface{}) { 59 root.write(msg, LvlCrit, ctx, skipLevel) 60 os.Exit(1) 61 } 62 63 func OnTrace(fn func(l Logging)) { 64 if root.GetHandler().Level() >= LvlTrace { 65 fn(root.Trace) 66 } 67 } 68 69 func OnDebug(fn func(l Logging)) { 70 if root.GetHandler().Level() >= LvlDebug { 71 fn(root.Debug) 72 } 73 } 74 func OnInfo(fn func(l Logging)) { 75 if root.GetHandler().Level() >= LvlInfo { 76 fn(root.Info) 77 } 78 } 79 func OnWarn(fn func(l Logging)) { 80 if root.GetHandler().Level() >= LvlWarn { 81 fn(root.Warn) 82 } 83 } 84 func OnError(fn func(l Logging)) { 85 if root.GetHandler().Level() >= LvlError { 86 fn(root.Error) 87 } 88 } 89 func OnCrit(fn func(l Logging)) { 90 if root.GetHandler().Level() >= LvlCrit { 91 fn(root.Crit) 92 } 93 } 94 95 // Output is a convenient alias for write, allowing for the modification of 96 // the calldepth (number of stack frames to skip). 97 // calldepth influences the reported line number of the log message. 98 // A calldepth of zero reports the immediate caller of Output. 99 // Non-zero calldepth skips as many stack frames. 100 func Output(msg string, lvl Lvl, calldepth int, ctx ...interface{}) { 101 root.write(msg, lvl, ctx, calldepth+skipLevel) 102 }