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