github.com/turingchain2020/turingchain@v1.1.21/common/log/log15/root.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package log15
     6  
     7  import (
     8  	"os"
     9  
    10  	"github.com/turingchain2020/turingchain/common/log/log15/term"
    11  	colorable "github.com/mattn/go-colorable"
    12  )
    13  
    14  // Predefined handlers
    15  var (
    16  	root          *logger
    17  	StdoutHandler = StreamHandler(os.Stdout, LogfmtFormat())
    18  	StderrHandler = StreamHandler(os.Stderr, LogfmtFormat())
    19  )
    20  
    21  func init() {
    22  	if term.IsTty(os.Stdout.Fd()) {
    23  		StdoutHandler = StreamHandler(colorable.NewColorableStdout(), TerminalFormat())
    24  	}
    25  
    26  	if term.IsTty(os.Stderr.Fd()) {
    27  		StderrHandler = StreamHandler(colorable.NewColorableStderr(), TerminalFormat())
    28  	}
    29  
    30  	root = &logger{[]interface{}{}, new(swapHandler), nil, int(LvlDebug)}
    31  	root.SetHandler(StdoutHandler)
    32  }
    33  
    34  // New returns a new logger with the given context.
    35  // New is a convenient alias for Root().New
    36  func New(ctx ...interface{}) Logger {
    37  	return root.New(ctx...)
    38  }
    39  
    40  // Root returns the root logger
    41  func Root() Logger {
    42  	return root
    43  }
    44  
    45  // The following functions bypass the exported logger methods (logger.Debug,
    46  // etc.) to keep the call depth the same for all paths to logger.write so
    47  // runtime.Caller(2) always refers to the call site in client code.
    48  
    49  // Debug is a convenient alias for Root().Debug
    50  func Debug(msg string, ctx ...interface{}) {
    51  	root.write(msg, LvlDebug, ctx)
    52  }
    53  
    54  // Info is a convenient alias for Root().Info
    55  func Info(msg string, ctx ...interface{}) {
    56  	root.write(msg, LvlInfo, ctx)
    57  }
    58  
    59  // Warn is a convenient alias for Root().Warn
    60  func Warn(msg string, ctx ...interface{}) {
    61  	root.write(msg, LvlWarn, ctx)
    62  }
    63  
    64  // Error is a convenient alias for Root().Error
    65  func Error(msg string, ctx ...interface{}) {
    66  	root.write(msg, LvlError, ctx)
    67  }
    68  
    69  // Crit is a convenient alias for Root().Crit
    70  func Crit(msg string, ctx ...interface{}) {
    71  	root.write(msg, LvlCrit, ctx)
    72  }