github.com/hyperledger-labs/bdls@v2.1.1+incompatible/common/flogging/zap.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 "fmt" 11 "strings" 12 13 "go.uber.org/zap" 14 "go.uber.org/zap/zapcore" 15 "go.uber.org/zap/zapgrpc" 16 ) 17 18 // NewZapLogger creates a zap logger around a new zap.Core. The core will use 19 // the provided encoder and sinks and a level enabler that is associated with 20 // the provided logger name. The logger that is returned will be named the same 21 // as the logger. 22 func NewZapLogger(core zapcore.Core, options ...zap.Option) *zap.Logger { 23 return zap.New( 24 core, 25 append([]zap.Option{ 26 zap.AddCaller(), 27 zap.AddStacktrace(zapcore.ErrorLevel), 28 }, options...)..., 29 ) 30 } 31 32 // NewGRPCLogger creates a grpc.Logger that delegates to a zap.Logger. 33 func NewGRPCLogger(l *zap.Logger) *zapgrpc.Logger { 34 l = l.WithOptions( 35 zap.AddCaller(), 36 zap.AddCallerSkip(3), 37 ) 38 return zapgrpc.NewLogger(l, zapgrpc.WithDebug()) 39 } 40 41 // NewFabricLogger creates a logger that delegates to the zap.SugaredLogger. 42 func NewFabricLogger(l *zap.Logger, options ...zap.Option) *FabricLogger { 43 return &FabricLogger{ 44 s: l.WithOptions(append(options, zap.AddCallerSkip(1))...).Sugar(), 45 } 46 } 47 48 // A FabricLogger is an adapter around a zap.SugaredLogger that provides 49 // structured logging capabilities while preserving much of the legacy logging 50 // behavior. 51 // 52 // The most significant difference between the FabricLogger and the 53 // zap.SugaredLogger is that methods without a formatting suffix (f or w) build 54 // the log entry message with fmt.Sprintln instead of fmt.Sprint. Without this 55 // change, arguments are not separated by spaces. 56 type FabricLogger struct{ s *zap.SugaredLogger } 57 58 func (f *FabricLogger) DPanic(args ...interface{}) { f.s.DPanicf(formatArgs(args)) } 59 func (f *FabricLogger) DPanicf(template string, args ...interface{}) { f.s.DPanicf(template, args...) } 60 func (f *FabricLogger) DPanicw(msg string, kvPairs ...interface{}) { f.s.DPanicw(msg, kvPairs...) } 61 func (f *FabricLogger) Debug(args ...interface{}) { f.s.Debugf(formatArgs(args)) } 62 func (f *FabricLogger) Debugf(template string, args ...interface{}) { f.s.Debugf(template, args...) } 63 func (f *FabricLogger) Debugw(msg string, kvPairs ...interface{}) { f.s.Debugw(msg, kvPairs...) } 64 func (f *FabricLogger) Error(args ...interface{}) { f.s.Errorf(formatArgs(args)) } 65 func (f *FabricLogger) Errorf(template string, args ...interface{}) { f.s.Errorf(template, args...) } 66 func (f *FabricLogger) Errorw(msg string, kvPairs ...interface{}) { f.s.Errorw(msg, kvPairs...) } 67 func (f *FabricLogger) Fatal(args ...interface{}) { f.s.Fatalf(formatArgs(args)) } 68 func (f *FabricLogger) Fatalf(template string, args ...interface{}) { f.s.Fatalf(template, args...) } 69 func (f *FabricLogger) Fatalw(msg string, kvPairs ...interface{}) { f.s.Fatalw(msg, kvPairs...) } 70 func (f *FabricLogger) Info(args ...interface{}) { f.s.Infof(formatArgs(args)) } 71 func (f *FabricLogger) Infof(template string, args ...interface{}) { f.s.Infof(template, args...) } 72 func (f *FabricLogger) Infow(msg string, kvPairs ...interface{}) { f.s.Infow(msg, kvPairs...) } 73 func (f *FabricLogger) Panic(args ...interface{}) { f.s.Panicf(formatArgs(args)) } 74 func (f *FabricLogger) Panicf(template string, args ...interface{}) { f.s.Panicf(template, args...) } 75 func (f *FabricLogger) Panicw(msg string, kvPairs ...interface{}) { f.s.Panicw(msg, kvPairs...) } 76 func (f *FabricLogger) Warn(args ...interface{}) { f.s.Warnf(formatArgs(args)) } 77 func (f *FabricLogger) Warnf(template string, args ...interface{}) { f.s.Warnf(template, args...) } 78 func (f *FabricLogger) Warnw(msg string, kvPairs ...interface{}) { f.s.Warnw(msg, kvPairs...) } 79 func (f *FabricLogger) Warning(args ...interface{}) { f.s.Warnf(formatArgs(args)) } 80 func (f *FabricLogger) Warningf(template string, args ...interface{}) { f.s.Warnf(template, args...) } 81 82 // for backwards compatibility 83 func (f *FabricLogger) Critical(args ...interface{}) { f.s.Errorf(formatArgs(args)) } 84 func (f *FabricLogger) Criticalf(template string, args ...interface{}) { f.s.Errorf(template, args...) } 85 func (f *FabricLogger) Notice(args ...interface{}) { f.s.Infof(formatArgs(args)) } 86 func (f *FabricLogger) Noticef(template string, args ...interface{}) { f.s.Infof(template, args...) } 87 88 func (f *FabricLogger) Named(name string) *FabricLogger { return &FabricLogger{s: f.s.Named(name)} } 89 func (f *FabricLogger) Sync() error { return f.s.Sync() } 90 func (f *FabricLogger) Zap() *zap.Logger { return f.s.Desugar() } 91 92 func (f *FabricLogger) IsEnabledFor(level zapcore.Level) bool { 93 return f.s.Desugar().Core().Enabled(level) 94 } 95 96 func (f *FabricLogger) With(args ...interface{}) *FabricLogger { 97 return &FabricLogger{s: f.s.With(args...)} 98 } 99 100 func (f *FabricLogger) WithOptions(opts ...zap.Option) *FabricLogger { 101 l := f.s.Desugar().WithOptions(opts...) 102 return &FabricLogger{s: l.Sugar()} 103 } 104 105 func formatArgs(args []interface{}) string { return strings.TrimSuffix(fmt.Sprintln(args...), "\n") }