github.com/theQRL/go-zond@v0.1.1/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  //
    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(msg, LvlTrace, ctx, skipLevel)
    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(msg, LvlDebug, ctx, skipLevel)
    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(msg, LvlInfo, ctx, skipLevel)
    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(msg, LvlWarn, ctx, skipLevel)
    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(msg, LvlError, ctx, skipLevel)
    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(msg, LvlCrit, ctx, skipLevel)
   108  	os.Exit(1)
   109  }
   110  
   111  // Output is a convenient alias for write, allowing for the modification of
   112  // the calldepth (number of stack frames to skip).
   113  // calldepth influences the reported line number of the log message.
   114  // A calldepth of zero reports the immediate caller of Output.
   115  // Non-zero calldepth skips as many stack frames.
   116  func Output(msg string, lvl Lvl, calldepth int, ctx ...interface{}) {
   117  	root.write(msg, lvl, ctx, calldepth+skipLevel)
   118  }