-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathotelzap.go
732 lines (624 loc) · 23.8 KB
/
otelzap.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
package otelzap
import (
"context"
"fmt"
"math"
"reflect"
"runtime"
"strconv"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/uptrace/opentelemetry-go-extra/otelutil"
)
const numAttr = 5
var (
logSeverityKey = attribute.Key("log.severity")
logMessageKey = attribute.Key("log.message")
logTemplateKey = attribute.Key("log.template")
)
// Logger is a thin wrapper for zap.Logger that adds Ctx method.
type Logger struct {
*zap.Logger
skipCaller *zap.Logger
withTraceID bool
minLevel zapcore.Level
errorStatusLevel zapcore.Level
caller bool
stackTrace bool
// extraFields contains a number of zap.Fields that are added to every log entry
extraFields []zap.Field
}
func New(logger *zap.Logger, opts ...Option) *Logger {
l := &Logger{
Logger: logger,
skipCaller: logger.WithOptions(zap.AddCallerSkip(1)),
minLevel: zap.WarnLevel,
errorStatusLevel: zap.ErrorLevel,
caller: true,
}
for _, opt := range opts {
opt(l)
}
return l
}
// WithOptions clones the current Logger, applies the supplied Options,
// and returns the resulting Logger. It's safe to use concurrently.
func (l *Logger) WithOptions(opts ...zap.Option) *Logger {
extraFields := []zap.Field{}
// zap.New side effect is extracting fields from .WithOptions(zap.Fields(...))
zap.New(&fieldExtractorCore{extraFields: &extraFields}, opts...)
clone := *l
clone.Logger = l.Logger.WithOptions(opts...)
clone.skipCaller = l.skipCaller.WithOptions(opts...)
clone.extraFields = append(clone.extraFields, extraFields...)
return &clone
}
// Sugar wraps the Logger to provide a more ergonomic, but slightly slower,
// API. Sugaring a Logger is quite inexpensive, so it's reasonable for a
// single application to use both Loggers and SugaredLoggers, converting
// between them on the boundaries of performance-sensitive code.
func (l *Logger) Sugar() *SugaredLogger {
return &SugaredLogger{
SugaredLogger: l.Logger.Sugar(),
skipCaller: l.skipCaller.Sugar(),
l: l,
}
}
// Clone clones the current logger applying the supplied options.
func (l *Logger) Clone(opts ...Option) *Logger {
clone := *l
for _, opt := range opts {
opt(&clone)
}
return &clone
}
// Ctx returns a new logger with the context.
func (l *Logger) Ctx(ctx context.Context) LoggerWithCtx {
return LoggerWithCtx{
ctx: ctx,
l: l,
}
}
func (l *Logger) DebugContext(ctx context.Context, msg string, fields ...zapcore.Field) {
fields = l.logFields(ctx, zap.DebugLevel, msg, fields)
l.skipCaller.Debug(msg, fields...)
}
func (l *Logger) InfoContext(ctx context.Context, msg string, fields ...zapcore.Field) {
fields = l.logFields(ctx, zap.InfoLevel, msg, fields)
l.skipCaller.Info(msg, fields...)
}
func (l *Logger) WarnContext(ctx context.Context, msg string, fields ...zapcore.Field) {
fields = l.logFields(ctx, zap.WarnLevel, msg, fields)
l.skipCaller.Warn(msg, fields...)
}
func (l *Logger) ErrorContext(ctx context.Context, msg string, fields ...zapcore.Field) {
fields = l.logFields(ctx, zap.ErrorLevel, msg, fields)
l.skipCaller.Error(msg, fields...)
}
func (l *Logger) DPanicContext(ctx context.Context, msg string, fields ...zapcore.Field) {
fields = l.logFields(ctx, zap.DPanicLevel, msg, fields)
l.skipCaller.DPanic(msg, fields...)
}
func (l *Logger) PanicContext(ctx context.Context, msg string, fields ...zapcore.Field) {
fields = l.logFields(ctx, zap.PanicLevel, msg, fields)
l.skipCaller.Panic(msg, fields...)
}
func (l *Logger) FatalContext(ctx context.Context, msg string, fields ...zapcore.Field) {
fields = l.logFields(ctx, zap.FatalLevel, msg, fields)
l.skipCaller.Fatal(msg, fields...)
}
func (l *Logger) logFields(
ctx context.Context, lvl zapcore.Level, msg string, fields []zapcore.Field,
) []zapcore.Field {
if lvl < l.minLevel {
return fields
}
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
return fields
}
attrs := make([]attribute.KeyValue, 0, numAttr+len(fields)+len(l.extraFields))
for _, f := range fields {
if f.Type == zapcore.NamespaceType {
// should this be a prefix?
continue
}
attrs = appendField(attrs, f)
}
for _, f := range l.extraFields {
if f.Type == zapcore.NamespaceType {
// should this be a prefix?
continue
}
attrs = appendField(attrs, f)
}
l.log(span, lvl, msg, attrs)
if l.withTraceID {
traceID := span.SpanContext().TraceID().String()
fields = append(fields, zap.String("trace_id", traceID))
}
return fields
}
func (l *Logger) log(
span trace.Span, lvl zapcore.Level, msg string, attrs []attribute.KeyValue,
) {
attrs = append(attrs, logSeverityKey.String(levelString(lvl)))
attrs = append(attrs, logMessageKey.String(msg))
if l.caller {
if fn, file, line, ok := runtimeCaller(4); ok {
if fn != "" {
attrs = append(attrs, semconv.CodeFunctionKey.String(fn))
}
if file != "" {
attrs = append(attrs, semconv.CodeFilepathKey.String(file))
attrs = append(attrs, semconv.CodeLineNumberKey.Int(line))
}
}
}
if l.stackTrace {
stackTrace := make([]byte, 2048)
n := runtime.Stack(stackTrace, false)
attrs = append(attrs, semconv.ExceptionStacktraceKey.String(string(stackTrace[0:n])))
}
span.AddEvent("log", trace.WithAttributes(attrs...))
if lvl >= l.errorStatusLevel {
span.SetStatus(codes.Error, msg)
}
}
func runtimeCaller(skip int) (fn, file string, line int, ok bool) {
rpc := make([]uintptr, 1)
n := runtime.Callers(skip+1, rpc[:])
if n < 1 {
return
}
frame, _ := runtime.CallersFrames(rpc).Next()
return frame.Function, frame.File, frame.Line, frame.PC != 0
}
//------------------------------------------------------------------------------
// LoggerWithCtx is a wrapper for Logger that also carries a context.Context.
type LoggerWithCtx struct {
ctx context.Context
l *Logger
}
// Context returns logger's context.
func (l LoggerWithCtx) Context() context.Context {
return l.ctx
}
// Logger returns the underlying logger.
func (l LoggerWithCtx) Logger() *Logger {
return l.l
}
// ZapLogger returns the underlying zap logger.
func (l LoggerWithCtx) ZapLogger() *zap.Logger {
return l.l.Logger
}
// Sugar returns a sugared logger with the context.
func (l LoggerWithCtx) Sugar() SugaredLoggerWithCtx {
return SugaredLoggerWithCtx{
ctx: l.ctx,
s: l.l.Sugar(),
}
}
// WithOptions clones the current Logger, applies the supplied Options,
// and returns the resulting Logger. It's safe to use concurrently.
func (l LoggerWithCtx) WithOptions(opts ...zap.Option) LoggerWithCtx {
return LoggerWithCtx{
ctx: l.ctx,
l: l.l.WithOptions(opts...),
}
}
// Clone clones the current logger applying the supplied options.
func (l LoggerWithCtx) Clone(opts ...Option) LoggerWithCtx {
return LoggerWithCtx{
ctx: l.ctx,
l: l.l.Clone(opts...),
}
}
// Debug logs a message at DebugLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (l LoggerWithCtx) Debug(msg string, fields ...zapcore.Field) {
fields = l.l.logFields(l.ctx, zap.DebugLevel, msg, fields)
l.l.skipCaller.Debug(msg, fields...)
}
// Info logs a message at InfoLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (l LoggerWithCtx) Info(msg string, fields ...zapcore.Field) {
fields = l.l.logFields(l.ctx, zap.InfoLevel, msg, fields)
l.l.skipCaller.Info(msg, fields...)
}
// Warn logs a message at WarnLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (l LoggerWithCtx) Warn(msg string, fields ...zapcore.Field) {
fields = l.l.logFields(l.ctx, zap.WarnLevel, msg, fields)
l.l.skipCaller.Warn(msg, fields...)
}
// Error logs a message at ErrorLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (l LoggerWithCtx) Error(msg string, fields ...zapcore.Field) {
fields = l.l.logFields(l.ctx, zap.ErrorLevel, msg, fields)
l.l.skipCaller.Error(msg, fields...)
}
// DPanic logs a message at DPanicLevel. The message includes any fields
// passed at the log site, as well as any fields accumulated on the logger.
//
// If the logger is in development mode, it then panics (DPanic means
// "development panic"). This is useful for catching errors that are
// recoverable, but shouldn't ever happen.
func (l LoggerWithCtx) DPanic(msg string, fields ...zapcore.Field) {
fields = l.l.logFields(l.ctx, zap.DPanicLevel, msg, fields)
l.l.skipCaller.DPanic(msg, fields...)
}
// Panic logs a message at PanicLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then panics, even if logging at PanicLevel is disabled.
func (l LoggerWithCtx) Panic(msg string, fields ...zapcore.Field) {
fields = l.l.logFields(l.ctx, zap.PanicLevel, msg, fields)
l.l.skipCaller.Panic(msg, fields...)
}
// Fatal logs a message at FatalLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then calls os.Exit(1), even if logging at FatalLevel is
// disabled.
func (l LoggerWithCtx) Fatal(msg string, fields ...zapcore.Field) {
fields = l.l.logFields(l.ctx, zap.FatalLevel, msg, fields)
l.l.skipCaller.Fatal(msg, fields...)
}
//------------------------------------------------------------------------------
// A SugaredLogger wraps the base Logger functionality in a slower, but less
// verbose, API. Any Logger can be converted to a SugaredLogger with its Sugar
// method.
//
// Unlike the Logger, the SugaredLogger doesn't insist on structured logging.
// For each log level, it exposes three methods: one for loosely-typed
// structured logging, one for println-style formatting, and one for
// printf-style formatting. For example, SugaredLoggers can produce InfoLevel
// output with Infow ("info with" structured context), Info, or Infof.
type SugaredLogger struct {
*zap.SugaredLogger
skipCaller *zap.SugaredLogger
l *Logger
}
// Desugar unwraps a SugaredLogger, exposing the original Logger. Desugaring
// is quite inexpensive, so it's reasonable for a single application to use
// both Loggers and SugaredLoggers, converting between them on the boundaries
// of performance-sensitive code.
func (s *SugaredLogger) Desugar() *Logger {
return s.l
}
// With adds a variadic number of fields to the logging context. It accepts a
// mix of strongly-typed Field objects and loosely-typed key-value pairs. When
// processing pairs, the first element of the pair is used as the field key
// and the second as the field value.
//
// For example,
// sugaredLogger.With(
// "hello", "world",
// "failure", errors.New("oh no"),
// Stack(),
// "count", 42,
// "user", User{Name: "alice"},
// )
// is the equivalent of
// unsugared.With(
// String("hello", "world"),
// String("failure", "oh no"),
// Stack(),
// Int("count", 42),
// Object("user", User{Name: "alice"}),
// )
//
// Note that the keys in key-value pairs should be strings. In development,
// passing a non-string key panics. In production, the logger is more
// forgiving: a separate error is logged, but the key-value pair is skipped
// and execution continues. Passing an orphaned key triggers similar behavior:
// panics in development and errors in production.
func (s *SugaredLogger) With(args ...interface{}) *SugaredLogger {
return &SugaredLogger{
SugaredLogger: s.SugaredLogger.With(args...),
l: s.l,
}
}
// Ctx returns a new sugared logger with the context.
func (s *SugaredLogger) Ctx(ctx context.Context) SugaredLoggerWithCtx {
return SugaredLoggerWithCtx{
ctx: ctx,
s: s,
}
}
// Debugf uses fmt.Sprintf to log a templated message.
func (s *SugaredLogger) DebugfContext(ctx context.Context, template string, args ...interface{}) {
s.logArgs(ctx, zap.DebugLevel, template, args)
s.Debugf(template, args...)
}
// Infof uses fmt.Sprintf to log a templated message.
func (s *SugaredLogger) InfofContext(ctx context.Context, template string, args ...interface{}) {
s.logArgs(ctx, zap.InfoLevel, template, args)
s.Infof(template, args...)
}
// Warnf uses fmt.Sprintf to log a templated message.
func (s *SugaredLogger) WarnfContext(ctx context.Context, template string, args ...interface{}) {
s.logArgs(ctx, zap.WarnLevel, template, args)
s.Warnf(template, args...)
}
// Errorf uses fmt.Sprintf to log a templated message.
func (s *SugaredLogger) ErrorfContext(ctx context.Context, template string, args ...interface{}) {
s.logArgs(ctx, zap.ErrorLevel, template, args)
s.Errorf(template, args...)
}
// DPanicf uses fmt.Sprintf to log a templated message. In development, the
// logger then panics. (See DPanicLevel for details.)
func (s *SugaredLogger) DPanicfContext(ctx context.Context, template string, args ...interface{}) {
s.logArgs(ctx, zap.DPanicLevel, template, args)
s.DPanicf(template, args...)
}
// Panicf uses fmt.Sprintf to log a templated message, then panics.
func (s *SugaredLogger) PanicfContext(ctx context.Context, template string, args ...interface{}) {
s.logArgs(ctx, zap.PanicLevel, template, args)
s.Panicf(template, args...)
}
// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
func (s *SugaredLogger) FatalfContext(ctx context.Context, template string, args ...interface{}) {
s.logArgs(ctx, zap.FatalLevel, template, args)
s.Fatalf(template, args...)
}
func (s *SugaredLogger) logArgs(
ctx context.Context, lvl zapcore.Level, template string, args []interface{},
) {
if lvl < s.l.minLevel {
return
}
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
return
}
attrs := make([]attribute.KeyValue, 0, numAttr+1)
attrs = append(attrs, logTemplateKey.String(template))
s.l.log(span, lvl, fmt.Sprintf(template, args...), attrs)
}
// Infow logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (s *SugaredLogger) InfowContext(
ctx context.Context, msg string, keysAndValues ...interface{},
) {
keysAndValues = s.logKVs(ctx, zap.InfoLevel, msg, keysAndValues)
s.Infow(msg, keysAndValues...)
}
// Warnw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (s *SugaredLogger) WarnwContext(
ctx context.Context, msg string, keysAndValues ...interface{},
) {
keysAndValues = s.logKVs(ctx, zap.WarnLevel, msg, keysAndValues)
s.Warnw(msg, keysAndValues...)
}
// Errorw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (s *SugaredLogger) ErrorwContext(
ctx context.Context, msg string, keysAndValues ...interface{},
) {
keysAndValues = s.logKVs(ctx, zap.ErrorLevel, msg, keysAndValues)
s.Errorw(msg, keysAndValues...)
}
// DPanicw logs a message with some additional context. In development, the
// logger then panics. (See DPanicLevel for details.) The variadic key-value
// pairs are treated as they are in With.
func (s *SugaredLogger) DPanicwContext(
ctx context.Context, msg string, keysAndValues ...interface{},
) {
keysAndValues = s.logKVs(ctx, zap.DPanicLevel, msg, keysAndValues)
s.DPanicw(msg, keysAndValues...)
}
// Panicw logs a message with some additional context, then panics. The
// variadic key-value pairs are treated as they are in With.
func (s *SugaredLogger) PanicwContext(
ctx context.Context, msg string, keysAndValues ...interface{},
) {
keysAndValues = s.logKVs(ctx, zap.PanicLevel, msg, keysAndValues)
s.Panicw(msg, keysAndValues...)
}
// Fatalw logs a message with some additional context, then calls os.Exit. The
// variadic key-value pairs are treated as they are in With.
func (s *SugaredLogger) FatalwContext(
ctx context.Context, msg string, keysAndValues ...interface{},
) {
keysAndValues = s.logKVs(ctx, zap.FatalLevel, msg, keysAndValues)
s.Fatalw(msg, keysAndValues...)
}
func (s *SugaredLogger) logKVs(
ctx context.Context, lvl zapcore.Level, msg string, kvs []interface{},
) []interface{} {
if lvl < s.l.minLevel {
return kvs
}
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
return kvs
}
attrs := make([]attribute.KeyValue, 0, numAttr+len(kvs))
for i := 0; i < len(kvs); i += 2 {
if key, ok := kvs[i].(string); ok {
attrs = append(attrs, otelutil.Attribute(key, kvs[i+1]))
}
}
s.l.log(span, lvl, msg, attrs)
if s.l.withTraceID {
traceID := span.SpanContext().TraceID().String()
kvs = append(kvs, "trace_id", traceID)
}
return kvs
}
//------------------------------------------------------------------------------
type SugaredLoggerWithCtx struct {
ctx context.Context
s *SugaredLogger
}
// Desugar unwraps a SugaredLogger, exposing the original Logger. Desugaring
// is quite inexpensive, so it's reasonable for a single application to use
// both Loggers and SugaredLoggers, converting between them on the boundaries
// of performance-sensitive code.
func (s SugaredLoggerWithCtx) Desugar() *Logger {
return s.s.Desugar()
}
// Debugf uses fmt.Sprintf to log a templated message.
func (s SugaredLoggerWithCtx) Debugf(template string, args ...interface{}) {
s.s.logArgs(s.ctx, zap.DebugLevel, template, args)
s.s.skipCaller.Debugf(template, args...)
}
// Infof uses fmt.Sprintf to log a templated message.
func (s SugaredLoggerWithCtx) Infof(template string, args ...interface{}) {
s.s.logArgs(s.ctx, zap.InfoLevel, template, args)
s.s.skipCaller.Infof(template, args...)
}
// Warnf uses fmt.Sprintf to log a templated message.
func (s SugaredLoggerWithCtx) Warnf(template string, args ...interface{}) {
s.s.logArgs(s.ctx, zap.WarnLevel, template, args)
s.s.skipCaller.Warnf(template, args...)
}
// Errorf uses fmt.Sprintf to log a templated message.
func (s SugaredLoggerWithCtx) Errorf(template string, args ...interface{}) {
s.s.logArgs(s.ctx, zap.ErrorLevel, template, args)
s.s.skipCaller.Errorf(template, args...)
}
// DPanicf uses fmt.Sprintf to log a templated message. In development, the
// logger then panics. (See DPanicLevel for details.)
func (s SugaredLoggerWithCtx) DPanicf(template string, args ...interface{}) {
s.s.logArgs(s.ctx, zap.DPanicLevel, template, args)
s.s.skipCaller.DPanicf(template, args...)
}
// Panicf uses fmt.Sprintf to log a templated message, then panics.
func (s SugaredLoggerWithCtx) Panicf(template string, args ...interface{}) {
s.s.logArgs(s.ctx, zap.PanicLevel, template, args)
s.s.skipCaller.Panicf(template, args...)
}
// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
func (s SugaredLoggerWithCtx) Fatalf(template string, args ...interface{}) {
s.s.logArgs(s.ctx, zap.FatalLevel, template, args)
s.s.skipCaller.Fatalf(template, args...)
}
// Debugw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
//
// When debug-level logging is disabled, this is much faster than
// s.With(keysAndValues).Debug(msg)
func (s SugaredLoggerWithCtx) Debugw(msg string, keysAndValues ...interface{}) {
keysAndValues = s.s.logKVs(s.ctx, zap.DebugLevel, msg, keysAndValues)
s.s.skipCaller.Debugw(msg, keysAndValues...)
}
// Infow logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (s SugaredLoggerWithCtx) Infow(msg string, keysAndValues ...interface{}) {
keysAndValues = s.s.logKVs(s.ctx, zap.InfoLevel, msg, keysAndValues)
s.s.skipCaller.Infow(msg, keysAndValues...)
}
// Warnw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (s SugaredLoggerWithCtx) Warnw(msg string, keysAndValues ...interface{}) {
keysAndValues = s.s.logKVs(s.ctx, zap.WarnLevel, msg, keysAndValues)
s.s.skipCaller.Warnw(msg, keysAndValues...)
}
// Errorw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (s SugaredLoggerWithCtx) Errorw(msg string, keysAndValues ...interface{}) {
keysAndValues = s.s.logKVs(s.ctx, zap.ErrorLevel, msg, keysAndValues)
s.s.skipCaller.Errorw(msg, keysAndValues...)
}
// DPanicw logs a message with some additional context. In development, the
// logger then panics. (See DPanicLevel for details.) The variadic key-value
// pairs are treated as they are in With.
func (s SugaredLoggerWithCtx) DPanicw(msg string, keysAndValues ...interface{}) {
keysAndValues = s.s.logKVs(s.ctx, zap.DPanicLevel, msg, keysAndValues)
s.s.skipCaller.DPanicw(msg, keysAndValues...)
}
// Panicw logs a message with some additional context, then panics. The
// variadic key-value pairs are treated as they are in With.
func (s SugaredLoggerWithCtx) Panicw(msg string, keysAndValues ...interface{}) {
keysAndValues = s.s.logKVs(s.ctx, zap.PanicLevel, msg, keysAndValues)
s.s.skipCaller.Panicw(msg, keysAndValues...)
}
// Fatalw logs a message with some additional context, then calls os.Exit. The
// variadic key-value pairs are treated as they are in With.
func (s SugaredLoggerWithCtx) Fatalw(msg string, keysAndValues ...interface{}) {
keysAndValues = s.s.logKVs(s.ctx, zap.FatalLevel, msg, keysAndValues)
s.s.skipCaller.Fatalw(msg, keysAndValues...)
}
//------------------------------------------------------------------------------
func appendField(attrs []attribute.KeyValue, f zapcore.Field) []attribute.KeyValue {
switch f.Type {
case zapcore.BoolType:
attr := attribute.Bool(f.Key, f.Integer == 1)
return append(attrs, attr)
case zapcore.Int8Type, zapcore.Int16Type, zapcore.Int32Type, zapcore.Int64Type,
zapcore.Uint32Type, zapcore.Uint8Type, zapcore.Uint16Type, zapcore.Uint64Type,
zapcore.UintptrType:
attr := attribute.Int64(f.Key, f.Integer)
return append(attrs, attr)
case zapcore.Float32Type, zapcore.Float64Type:
attr := attribute.Float64(f.Key, math.Float64frombits(uint64(f.Integer)))
return append(attrs, attr)
case zapcore.Complex64Type:
s := strconv.FormatComplex(complex128(f.Interface.(complex64)), 'E', -1, 64)
attr := attribute.String(f.Key, s)
return append(attrs, attr)
case zapcore.Complex128Type:
s := strconv.FormatComplex(f.Interface.(complex128), 'E', -1, 128)
attr := attribute.String(f.Key, s)
return append(attrs, attr)
case zapcore.StringType:
attr := attribute.String(f.Key, f.String)
return append(attrs, attr)
case zapcore.BinaryType, zapcore.ByteStringType:
attr := attribute.String(f.Key, string(f.Interface.([]byte)))
return append(attrs, attr)
case zapcore.StringerType:
attr := attribute.String(f.Key, f.Interface.(fmt.Stringer).String())
return append(attrs, attr)
case zapcore.DurationType, zapcore.TimeType:
attr := attribute.Int64(f.Key, f.Integer)
return append(attrs, attr)
case zapcore.TimeFullType:
attr := attribute.Int64(f.Key, f.Interface.(time.Time).UnixNano())
return append(attrs, attr)
case zapcore.ErrorType:
err := f.Interface.(error)
typ := reflect.TypeOf(err).String()
attrs = append(attrs, semconv.ExceptionTypeKey.String(typ))
attrs = append(attrs, semconv.ExceptionMessageKey.String(err.Error()))
return attrs
case zapcore.ReflectType:
attr := otelutil.Attribute(f.Key, f.Interface)
return append(attrs, attr)
case zapcore.SkipType:
return attrs
case zapcore.ArrayMarshalerType:
var attr attribute.KeyValue
arrayEncoder := &bufferArrayEncoder{
stringsSlice: []string{},
}
err := f.Interface.(zapcore.ArrayMarshaler).MarshalLogArray(arrayEncoder)
if err != nil {
attr = attribute.String(f.Key+"_error", fmt.Sprintf("otelzap: unable to marshal array: %v", err))
} else {
attr = attribute.StringSlice(f.Key, arrayEncoder.stringsSlice)
}
return append(attrs, attr)
case zapcore.ObjectMarshalerType:
attr := attribute.String(f.Key+"_error", "otelzap: zapcore.ObjectMarshalerType is not implemented")
return append(attrs, attr)
default:
attr := attribute.String(f.Key+"_error", fmt.Sprintf("otelzap: unknown field type: %v", f))
return append(attrs, attr)
}
}
func levelString(lvl zapcore.Level) string {
if lvl == zapcore.DPanicLevel {
return "PANIC"
}
return lvl.CapitalString()
}