github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/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 InitMagneticLog(logpath string) {
    14  
    15  	//root.SetHandler(MultiHandler(LvlFilterHandler(LvlInfo,StdoutHandler),LvlFilterHandler(LvlInfo,Must.FileHandler("magnetic.log", TerminalFormat(true)))))
    16  
    17  
    18  	root.SetHandler(LvlFilterHandler(LvlInfo,Must.FileHandler(logpath,TerminalFormat(false))))
    19  }
    20  
    21  // New returns a new logger with the given context.
    22  // New is a convenient alias for Root().New
    23  func New(ctx ...interface{}) Logger {
    24  	return root.New(ctx...)
    25  }
    26  
    27  // Root returns the root logger
    28  func Root() Logger {
    29  	return root
    30  }
    31  
    32  // The following functions bypass the exported logger methods (logger.Debug,
    33  // etc.) to keep the call depth the same for all paths to logger.write so
    34  // runtime.Caller(2) always refers to the call site in client code.
    35  
    36  // Trace is a convenient alias for Root().Trace
    37  func Trace(msg string, ctx ...interface{}) {
    38  	root.write(msg, LvlTrace, ctx)
    39  }
    40  
    41  // Debug is a convenient alias for Root().Debug
    42  func Debug(msg string, ctx ...interface{}) {
    43  	root.write(msg, LvlDebug, ctx)
    44  }
    45  
    46  // Info is a convenient alias for Root().Info
    47  func Info(msg string, ctx ...interface{}) {
    48  	root.write(msg, LvlInfo, ctx)
    49  }
    50  
    51  // Warn is a convenient alias for Root().Warn
    52  func Warn(msg string, ctx ...interface{}) {
    53  	root.write(msg, LvlWarn, ctx)
    54  }
    55  
    56  // Error is a convenient alias for Root().Error
    57  func Error(msg string, ctx ...interface{}) {
    58  	root.write(msg, LvlError, ctx)
    59  }
    60  
    61  // Crit is a convenient alias for Root().Crit
    62  func Crit(msg string, ctx ...interface{}) {
    63  	root.write(msg, LvlCrit, ctx)
    64  	os.Exit(1)
    65  }