github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/common/flogging/global.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package flogging
     8  
     9  import (
    10  	"io"
    11  
    12  	"go.uber.org/zap/zapcore"
    13  	"google.golang.org/grpc/grpclog"
    14  )
    15  
    16  const (
    17  	defaultFormat = "%{color}%{time:2006-01-02 15:04:05.000 MST} %{id:04x} %{level:.4s}%{color:reset} [%{module}] %{color:bold}%{shortfunc}%{color:reset} -> %{message}"
    18  	defaultLevel  = zapcore.InfoLevel
    19  )
    20  
    21  var Global *Logging
    22  
    23  func init() {
    24  	logging, err := New(Config{})
    25  	if err != nil {
    26  		panic(err)
    27  	}
    28  
    29  	Global = logging
    30  	grpcLogger := Global.ZapLogger("grpc")
    31  	grpclog.SetLogger(NewGRPCLogger(grpcLogger))
    32  }
    33  
    34  // Init initializes logging with the provided config.
    35  func Init(config Config) {
    36  	err := Global.Apply(config)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  }
    41  
    42  // Reset sets logging to the defaults defined in this package.
    43  //
    44  // Used in tests and in the package init
    45  func Reset() {
    46  	Global.Apply(Config{})
    47  }
    48  
    49  // LoggerLevel gets the current logging level for the logger with the
    50  // provided name.
    51  func LoggerLevel(loggerName string) string {
    52  	return Global.Level(loggerName).String()
    53  }
    54  
    55  // MustGetLogger creates a logger with the specified name. If an invalid name
    56  // is provided, the operation will panic.
    57  func MustGetLogger(loggerName string) *FabricLogger {
    58  	return Global.Logger(loggerName)
    59  }
    60  
    61  // ActivateSpec is used to activate a logging specification.
    62  func ActivateSpec(spec string) {
    63  	err := Global.ActivateSpec(spec)
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  }
    68  
    69  // DefaultLevel returns the default log level.
    70  func DefaultLevel() string {
    71  	return defaultLevel.String()
    72  }
    73  
    74  // SetWriter calls SetWriter returning the previous value
    75  // of the writer.
    76  func SetWriter(w io.Writer) io.Writer {
    77  	return Global.SetWriter(w)
    78  }
    79  
    80  // SetObserver calls SetObserver returning the previous value
    81  // of the observer.
    82  func SetObserver(observer Observer) Observer {
    83  	return Global.SetObserver(observer)
    84  }