@@ -29,45 +29,141 @@ import (
29
29
"go.uber.org/zap/zapcore"
30
30
)
31
31
32
+ // New returns a brand new Logger configured with Opts. It
33
+ // uses KubeAwareEncoder which adds Type information and
34
+ // Namespace/Name to the log.
35
+ func New (opts ... Opts ) logr.Logger {
36
+ return zapr .NewLogger (NewRaw (opts ... ))
37
+ }
38
+
32
39
// Logger is a Logger implementation.
33
40
// If development is true, a Zap development config will be used
34
41
// (stacktraces on warnings, no sampling), otherwise a Zap production
35
42
// config will be used (stacktraces on errors, sampling).
43
+ //
44
+ // Deprecated, use New() and the functional opts pattern instead:
45
+ //
46
+ // New(func(o *Options){
47
+ // o.Development: development,
48
+ // })
36
49
func Logger (development bool ) logr.Logger {
37
50
return LoggerTo (os .Stderr , development )
38
51
}
39
52
40
53
// LoggerTo returns a new Logger implementation using Zap which logs
41
54
// to the given destination, instead of stderr. It otherwise behaves like
42
55
// ZapLogger.
56
+ //
57
+ // Deprecated, use New() and the functional opts pattern instead:
58
+ //
59
+ // New(func(o *Options){
60
+ // o.Development: development,
61
+ // o.DestWriter: writer,
62
+ // })
43
63
func LoggerTo (destWriter io.Writer , development bool ) logr.Logger {
44
64
return zapr .NewLogger (RawLoggerTo (destWriter , development ))
45
65
}
46
66
47
67
// RawLoggerTo returns a new zap.Logger configured with KubeAwareEncoder
48
68
// which logs to a given destination
69
+ //
70
+ // Deprecated, use NewRaw() and the functional opts pattern instead:
71
+ //
72
+ // NewRaw(func(o *Options){
73
+ // o.Development: development,
74
+ // })
49
75
func RawLoggerTo (destWriter io.Writer , development bool , opts ... zap.Option ) * zap.Logger {
50
- // this basically mimics New<type>Config, but with a custom sink
51
- sink := zapcore .AddSync (destWriter )
52
-
53
- var enc zapcore.Encoder
54
- var lvl zap.AtomicLevel
55
- if development {
56
- encCfg := zap .NewDevelopmentEncoderConfig ()
57
- enc = zapcore .NewConsoleEncoder (encCfg )
58
- lvl = zap .NewAtomicLevelAt (zap .DebugLevel )
59
- opts = append (opts , zap .Development (), zap .AddStacktrace (zap .ErrorLevel ))
76
+ o := func (o * Options ) {
77
+ o .DestWritter = destWriter
78
+ o .Development = development
79
+ o .ZapOpts = opts
80
+ }
81
+ return NewRaw (o )
82
+ }
83
+
84
+ // Opts allows to manipulate Options
85
+ type Opts func (* Options )
86
+
87
+ // Options contains all possible settings
88
+ type Options struct {
89
+ // If Development is true, a Zap development config will be used
90
+ // (stacktraces on warnings, no sampling), otherwise a Zap production
91
+ // config will be used (stacktraces on errors, sampling).
92
+ Development bool
93
+ // The encoder to use, defaults to console when Development is true
94
+ // and JSON otherwise
95
+ Encoder zapcore.Encoder
96
+ // The destination to write to, defaults to os.Stderr
97
+ DestWritter io.Writer
98
+ // The level to use, defaults to Debug when Development is true and
99
+ // Info otherwise
100
+ Level * zap.AtomicLevel
101
+ // StacktraceLevel is the level at and above which stacktraces will
102
+ // be recorded for all messages. Defaults to Warn when Development
103
+ // is true and Error otherwise
104
+ StacktraceLevel * zap.AtomicLevel
105
+ // Raw zap.Options to configure on the underlying zap logger
106
+ ZapOpts []zap.Option
107
+ }
108
+
109
+ // addDefaults adds defaults to the Options
110
+ func (o * Options ) addDefaults () {
111
+ if o .DestWritter == nil {
112
+ o .DestWritter = os .Stderr
113
+ }
114
+
115
+ if o .Development {
116
+ if o .Encoder == nil {
117
+ encCfg := zap .NewDevelopmentEncoderConfig ()
118
+ o .Encoder = zapcore .NewConsoleEncoder (encCfg )
119
+ }
120
+ if o .Level == nil {
121
+ lvl := zap .NewAtomicLevelAt (zap .DebugLevel )
122
+ o .Level = & lvl
123
+ }
124
+ if o .StacktraceLevel == nil {
125
+ lvl := zap .NewAtomicLevelAt (zap .WarnLevel )
126
+ o .StacktraceLevel = & lvl
127
+ }
128
+ o .ZapOpts = append (o .ZapOpts , zap .Development ())
129
+
60
130
} else {
61
- encCfg := zap .NewProductionEncoderConfig ()
62
- enc = zapcore .NewJSONEncoder (encCfg )
63
- lvl = zap .NewAtomicLevelAt (zap .InfoLevel )
64
- opts = append (opts , zap .AddStacktrace (zap .WarnLevel ),
131
+ if o .Encoder == nil {
132
+ encCfg := zap .NewProductionEncoderConfig ()
133
+ o .Encoder = zapcore .NewJSONEncoder (encCfg )
134
+ }
135
+ if o .Level == nil {
136
+ lvl := zap .NewAtomicLevelAt (zap .InfoLevel )
137
+ o .Level = & lvl
138
+ }
139
+ if o .StacktraceLevel == nil {
140
+ lvl := zap .NewAtomicLevelAt (zap .ErrorLevel )
141
+ o .StacktraceLevel = & lvl
142
+ }
143
+ o .ZapOpts = append (o .ZapOpts ,
65
144
zap .WrapCore (func (core zapcore.Core ) zapcore.Core {
66
145
return zapcore .NewSampler (core , time .Second , 100 , 100 )
67
146
}))
68
147
}
69
- opts = append (opts , zap .AddCallerSkip (1 ), zap .ErrorOutput (sink ))
70
- log := zap .New (zapcore .NewCore (& KubeAwareEncoder {Encoder : enc , Verbose : development }, sink , lvl ))
71
- log = log .WithOptions (opts ... )
148
+
149
+ o .ZapOpts = append (o .ZapOpts , zap .AddStacktrace (o .StacktraceLevel ))
150
+ }
151
+
152
+ // NewRaw returns a new zap.Logger configured with the passed Opts
153
+ // or their defaults. It uses KubeAwareEncoder which adds Type
154
+ // information and Namespace/Name to the log.
155
+ func NewRaw (opts ... Opts ) * zap.Logger {
156
+ o := & Options {}
157
+ for _ , opt := range opts {
158
+ opt (o )
159
+ }
160
+ o .addDefaults ()
161
+
162
+ // this basically mimics New<type>Config, but with a custom sink
163
+ sink := zapcore .AddSync (o .DestWritter )
164
+
165
+ o .ZapOpts = append (o .ZapOpts , zap .AddCallerSkip (1 ), zap .ErrorOutput (sink ))
166
+ log := zap .New (zapcore .NewCore (& KubeAwareEncoder {Encoder : o .Encoder , Verbose : o .Development }, sink , * o .Level ))
167
+ log = log .WithOptions (o .ZapOpts ... )
72
168
return log
73
169
}
0 commit comments