@@ -24,7 +24,6 @@ import (
24
24
nsmlog "github.com/networkservicemesh/sdk/pkg/tools/log"
25
25
)
26
26
27
-
28
27
// Several different logr backends can be tested by remove/add comment
29
28
// below.
30
29
@@ -142,7 +141,6 @@ import (
142
141
143
142
"github.com/go-logr/zapr"
144
143
"go.uber.org/zap"
145
- "go.uber.org/zap/buffer"
146
144
"go.uber.org/zap/zapcore"
147
145
)
148
146
@@ -190,7 +188,6 @@ func newLogger(level string) logr.Logger {
190
188
//zc.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder (almost works)
191
189
zc .EncoderConfig .EncodeTime = timeEncoder
192
190
zc .EncoderConfig .EncodeLevel = adpLevelEncoder
193
- zc .Encoding = "ADP"
194
191
z , err := zc .Build ()
195
192
if err != nil {
196
193
zc .Encoding = "json"
@@ -199,122 +196,11 @@ func newLogger(level string) logr.Logger {
199
196
panic (fmt .Sprintf ("Can't create a zap logger (%v)?" , err ))
200
197
}
201
198
}
202
- return zapr .NewLogger (z .With (zap .String ("version" , "1.1.0" )))
203
- }
204
-
205
- // We just want to add "extra_data: { ... }" for additional fields
206
- // but I found no other way than to define an own zapcore.Encoder.
207
- // Everything except the EncodeEntry() is passed to the JSONEncoder.
208
- type adpEncoder struct {
209
- jsonEncoder zapcore.Encoder
210
- }
211
-
212
- func (e * adpEncoder ) Clone () zapcore.Encoder {
213
- return & adpEncoder {jsonEncoder : e .jsonEncoder .Clone ()}
214
- }
215
-
216
- // https://stackoverflow.com/questions/53737435/how-to-prepend-int-to-slice
217
- func prependField (x []zapcore.Field , y zapcore.Field ) []zapcore.Field {
218
- x = append (x , zapcore.Field {})
219
- copy (x [1 :], x )
220
- x [0 ] = y
221
- return x
222
- }
223
- func (e * adpEncoder ) EncodeEntry (
224
- entry zapcore.Entry , f []zapcore.Field ) (* buffer.Buffer , error ) {
225
- if len (f ) == 0 {
226
- return e .jsonEncoder .EncodeEntry (entry , f )
227
- }
228
- ns := zapcore.Field {
229
- Key : "extra_data" ,
230
- Type : zapcore .NamespaceType ,
231
- }
232
- return e .jsonEncoder .EncodeEntry (entry , prependField (f , ns ))
233
- }
234
- func (e * adpEncoder ) AddArray (key string , marshaler zapcore.ArrayMarshaler ) error {
235
- return e .jsonEncoder .AddArray (key , marshaler )
236
- }
237
- func (e * adpEncoder ) AddObject (key string , marshaler zapcore.ObjectMarshaler ) error {
238
- return e .jsonEncoder .AddObject (key , marshaler )
239
- }
240
- func (e * adpEncoder ) AddBinary (key string , value []byte ) {
241
- e .jsonEncoder .AddBinary (key , value )
242
- }
243
- func (e * adpEncoder ) AddByteString (key string , value []byte ) {
244
- e .jsonEncoder .AddByteString (key , value )
245
- }
246
- func (e * adpEncoder ) AddBool (key string , value bool ) {
247
- e .jsonEncoder .AddBool (key , value )
248
- }
249
- func (e * adpEncoder ) AddComplex128 (key string , value complex128 ) {
250
- e .jsonEncoder .AddComplex128 (key , value )
251
- }
252
- func (e * adpEncoder ) AddComplex64 (key string , value complex64 ) {
253
- e .jsonEncoder .AddComplex64 (key , value )
254
- }
255
- func (e * adpEncoder ) AddDuration (key string , value time.Duration ) {
256
- e .jsonEncoder .AddDuration (key , value )
257
- }
258
- func (e * adpEncoder ) AddFloat64 (key string , value float64 ) {
259
- e .jsonEncoder .AddFloat64 (key , value )
260
- }
261
- func (e * adpEncoder ) AddFloat32 (key string , value float32 ) {
262
- e .jsonEncoder .AddFloat32 (key , value )
263
- }
264
- func (e * adpEncoder ) AddInt (key string , value int ) {
265
- e .jsonEncoder .AddInt (key , value )
266
- }
267
- func (e * adpEncoder ) AddInt64 (key string , value int64 ) {
268
- e .jsonEncoder .AddInt64 (key , value )
269
- }
270
- func (e * adpEncoder ) AddInt32 (key string , value int32 ) {
271
- e .jsonEncoder .AddInt32 (key , value )
272
- }
273
- func (e * adpEncoder ) AddInt16 (key string , value int16 ) {
274
- e .jsonEncoder .AddInt16 (key , value )
275
- }
276
- func (e * adpEncoder ) AddInt8 (key string , value int8 ) {
277
- e .jsonEncoder .AddInt8 (key , value )
278
- }
279
- func (e * adpEncoder ) AddString (key , value string ) {
280
- e .jsonEncoder .AddString (key , value )
281
- }
282
- func (e * adpEncoder ) AddTime (key string , value time.Time ) {
283
- e .jsonEncoder .AddTime (key , value )
284
- }
285
- func (e * adpEncoder ) AddUint (key string , value uint ) {
286
- e .jsonEncoder .AddUint (key , value )
287
- }
288
- func (e * adpEncoder ) AddUint64 (key string , value uint64 ) {
289
- e .jsonEncoder .AddUint64 (key , value )
290
- }
291
- func (e * adpEncoder ) AddUint32 (key string , value uint32 ) {
292
- e .jsonEncoder .AddUint32 (key , value )
293
- }
294
- func (e * adpEncoder ) AddUint16 (key string , value uint16 ) {
295
- e .jsonEncoder .AddUint16 (key , value )
296
- }
297
- func (e * adpEncoder ) AddUint8 (key string , value uint8 ) {
298
- e .jsonEncoder .AddUint8 (key , value )
299
- }
300
- func (e * adpEncoder ) AddUintptr (key string , value uintptr ) {
301
- e .jsonEncoder .AddUintptr (key , value )
302
- }
303
- func (e * adpEncoder ) AddReflected (key string , value interface {}) error {
304
- return e .jsonEncoder .AddReflected (key , value )
305
- }
306
- func (e * adpEncoder ) OpenNamespace (key string ) {
307
- e .jsonEncoder .OpenNamespace (key )
199
+ return zapr .NewLogger (z .With (
200
+ zap .String ("version" , "1.0.0" ), zap .Namespace ("extra_data" )))
308
201
}
309
202
310
203
func init () {
311
- _ = zap .RegisterEncoder (
312
- "ADP" , func (cfg zapcore.EncoderConfig ) (zapcore.Encoder , error ) {
313
- //return zapcore.NewJSONEncoder(cfg), nil
314
- return & adpEncoder {
315
- jsonEncoder : zapcore .NewJSONEncoder (cfg ),
316
- }, nil
317
- })
318
204
Logger = newLogger ("" ).WithName ("Meridio" )
319
205
}
320
206
@@ -341,17 +227,19 @@ func Fatal(logger logr.Logger, msg string, keysAndValues ...interface{}) {
341
227
// NSMLogger return a logger to use for NSM logging.
342
228
func NSMLogger (logger logr.Logger ) nsmlog.Logger {
343
229
// Get the zap logger
344
- if underlier , ok := logger .GetSink ().(zapr.Underlier ); ok {
345
- z := underlier .GetUnderlying ()
346
- return & nsmLogger { z : z , s : z .Sugar () }
347
- } else {
230
+ z := ZapLogger (logger )
231
+ if z == nil {
348
232
panic ("NSMLogger: Can't get the Zap logger" )
349
233
}
234
+ nsmz := z .With (zap .String ("subsystem" , "NSM" ))
235
+ return & nsmLogger {z : nsmz , s : nsmz .Sugar ()}
350
236
}
237
+
351
238
type nsmLogger struct {
352
239
z * zap.Logger
353
240
s * zap.SugaredLogger
354
241
}
242
+
355
243
func (l * nsmLogger ) Info (v ... interface {}) {
356
244
l .s .Info (v ... )
357
245
}
@@ -393,15 +281,14 @@ func (l *nsmLogger) Tracef(format string, v ...interface{}) {
393
281
}
394
282
}
395
283
func (l * nsmLogger ) Object (k , v interface {}) {
396
- l .s .Info (k , v )
284
+ l .z .Info ("Object" , zap . Any ( fmt . Sprintf ( "%v" , k ) , v ) )
397
285
}
398
286
func (l * nsmLogger ) WithField (key , value interface {}) nsmlog.Logger {
399
287
return & nsmLogger {
400
288
z : l .z .With (zap .Any (fmt .Sprintf ("%v" , key ), value )),
401
289
}
402
290
}
403
291
404
-
405
292
// Logger The global logger
406
293
var Logger logr.Logger
407
294
@@ -419,8 +306,8 @@ func FromContextOrGlobal(ctx context.Context) logr.Logger {
419
306
// the global Logger is set.
420
307
func New (name , level string ) logr.Logger {
421
308
logger := newLogger (level ).WithName (name )
422
- once .Do (func () {Logger = logger })
309
+ once .Do (func () { Logger = logger })
423
310
return logger
424
311
}
425
- var once sync.Once
426
312
313
+ var once sync.Once
0 commit comments