github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/log/root.go (about)

     1  package log
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  )
     7  
     8  var (
     9  	root          = &logger{[]interface{}{}, new(swapHandler)}
    10  	StdoutHandler = StreamHandler(os.Stdout, LogfmtFormat())
    11  	StderrHandler = StreamHandler(os.Stderr, LogfmtFormat())
    12  )
    13  
    14  func init() {
    15  	root.SetHandler(DiscardHandler())
    16  }
    17  
    18  // New returns a new logger with the given context.
    19  // New is a convenient alias for Root().New
    20  func New(ctx ...interface{}) Logger {
    21  	return root.New(ctx...)
    22  }
    23  
    24  // Root returns the root logger
    25  func Root() Logger {
    26  	return root
    27  }
    28  
    29  // The following functions bypass the exported logger methods (logger.Debug,
    30  // etc.) to keep the call depth the same for all paths to logger.write so
    31  // runtime.Caller(2) always refers to the call site in client code.
    32  
    33  // Trace is a convenient alias for Root().Trace
    34  func Trace(msg string, ctx ...interface{}) {
    35  	root.write(msg, LvlTrace, ctx, skipLevel)
    36  }
    37  
    38  // Debug is a convenient alias for Root().Debug
    39  func Debug(msg string, ctx ...interface{}) {
    40  	root.write(msg, LvlDebug, ctx, skipLevel)
    41  }
    42  
    43  // Info is a convenient alias for Root().Info
    44  func Info(msg string, ctx ...interface{}) {
    45  	root.write(msg, LvlInfo, ctx, skipLevel)
    46  }
    47  
    48  // Warn is a convenient alias for Root().Warn
    49  func Warn(msg string, ctx ...interface{}) {
    50  	root.write(msg, LvlWarn, ctx, skipLevel)
    51  }
    52  
    53  // Error is a convenient alias for Root().Error
    54  func Error(msg string, ctx ...interface{}) {
    55  	root.write(msg, LvlError, ctx, skipLevel)
    56  }
    57  
    58  // Crit is a convenient alias for Root().Crit
    59  func Crit(msg string, ctx ...interface{}) {
    60  	root.write(msg, LvlCrit, ctx, skipLevel)
    61  	os.Exit(1)
    62  }
    63  
    64  // Output is a convenient alias for write, allowing for the modification of
    65  // the calldepth (number of stack frames to skip).
    66  // calldepth influences the reported line number of the log message.
    67  // A calldepth of zero reports the immediate caller of Output.
    68  // Non-zero calldepth skips as many stack frames.
    69  func Output(msg string, lvl Lvl, calldepth int, ctx ...interface{}) {
    70  	root.write(msg, lvl, ctx, calldepth+skipLevel)
    71  }
    72  
    73  // Debugf is a convenient alias for Root().Debugf
    74  func Debugf(format string, args ...interface{}) {
    75  	root.write(fmt.Sprintf(format, args...), LvlDebug, nil, skipLevel)
    76  }
    77  
    78  // Infof is a convenient alias for Root().Infof
    79  func Infof(format string, args ...interface{}) {
    80  	root.write(fmt.Sprintf(format, args...), LvlInfo, nil, skipLevel)
    81  }
    82  
    83  // Warnf is a convenient alias for Root().Warnf
    84  func Warnf(format string, args ...interface{}) {
    85  	root.write(fmt.Sprintf(format, args...), LvlWarn, nil, skipLevel)
    86  }
    87  
    88  // Errorf is a convenient alias for Root().Errorf
    89  func Errorf(format string, args ...interface{}) {
    90  	root.write(fmt.Sprintf(format, args...), LvlError, nil, skipLevel)
    91  }