github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/common/flogging/global.go (about) 1 /* 2 Copyright IBM Corp. 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} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}" 18 defaultLevel = zapcore.InfoLevel 19 ) 20 21 var Global *Logging 22 var logger *FabricLogger 23 24 func init() { 25 logging, err := New(Config{}) 26 if err != nil { 27 panic(err) 28 } 29 30 Global = logging 31 logger = Global.Logger("flogging") 32 grpcLogger := Global.ZapLogger("grpc") 33 grpclog.SetLogger(NewGRPCLogger(grpcLogger)) 34 } 35 36 // Init initializes logging with the provided config. 37 func Init(config Config) { 38 err := Global.Apply(config) 39 if err != nil { 40 panic(err) 41 } 42 } 43 44 // Reset sets logging to the defaults defined in this package. 45 // 46 // Used in tests and in the package init 47 func Reset() { 48 Global.Apply(Config{}) 49 } 50 51 // LoggerLevel gets the current logging level for the logger with the 52 // provided name. 53 func LoggerLevel(loggerName string) string { 54 return Global.Level(loggerName).String() 55 } 56 57 // MustGetLogger creates a logger with the specified name. If an invalid name 58 // is provided, the operation will panic. 59 func MustGetLogger(loggerName string) *FabricLogger { 60 return Global.Logger(loggerName) 61 } 62 63 // ActivateSpec is used to activate a logging specification. 64 func ActivateSpec(spec string) { 65 err := Global.ActivateSpec(spec) 66 if err != nil { 67 panic(err) 68 } 69 } 70 71 // DefaultLevel returns the default log level. 72 func DefaultLevel() string { 73 return defaultLevel.String() 74 } 75 76 // SetWriter calls SetWriter returning the previous value 77 // of the writer. 78 func SetWriter(w io.Writer) io.Writer { 79 return Global.SetWriter(w) 80 } 81 82 // SetObserver calls SetObserver returning the previous value 83 // of the observer. 84 func SetObserver(observer Observer) Observer { 85 return Global.SetObserver(observer) 86 }