github.com/fff-chain/go-fff@v0.0.0-20220726032732-1c84420b8a99/log/root.go (about)

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