-
Notifications
You must be signed in to change notification settings - Fork 1
/
spanlogger.go
168 lines (137 loc) · 5.11 KB
/
spanlogger.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
package tracing
import (
"context"
"github.com/opentracing/opentracing-go"
tag "github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log"
"github.com/osstotalsoft/bifrost/log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"time"
)
//SpanLoggerFactory is a wrapper over zap.Logger with OpenTracing
func SpanLoggerFactory(logger *zap.Logger) log.Factory {
return func(ctx context.Context) log.Logger {
if ctx != nil {
if span := opentracing.SpanFromContext(ctx); span != nil {
// TODO for Jaeger span extract trace/span IDs as fields
return spanLogger{logger, span}
}
}
return log.ZapLoggerFactory(logger)(nil)
}
}
type spanLogger struct {
logger *zap.Logger
span opentracing.Span
}
func (sl spanLogger) Info(msg string, fields ...zapcore.Field) {
sl.logToSpan("info", msg, fields...)
sl.logger.Info(msg, fields...)
}
func (sl spanLogger) Error(msg string, fields ...zapcore.Field) {
sl.logToSpan("error", msg, fields...)
//tag.Error.Set(sl.span, true)
sl.logger.Error(msg, fields...)
}
func (sl spanLogger) Fatal(msg string, fields ...zapcore.Field) {
sl.logToSpan("fatal", msg, fields...)
tag.Error.Set(sl.span, true)
sl.logger.Fatal(msg, fields...)
}
// Debug logs a fatal error msg with fields
func (sl spanLogger) Debug(msg string, fields ...zapcore.Field) {
sl.logToSpan("debug", msg, fields...)
sl.logger.Debug(msg, fields...)
}
// Warn logs a fatal error msg with fields
func (sl spanLogger) Warn(msg string, fields ...zapcore.Field) {
sl.logToSpan("warn", msg, fields...)
sl.logger.Warn(msg, fields...)
}
// Panic logs a fatal error msg with fields
func (sl spanLogger) Panic(msg string, fields ...zapcore.Field) {
sl.logToSpan("panic", msg, fields...)
tag.Error.Set(sl.span, true)
sl.logger.Panic(msg, fields...)
}
// With creates a child logger, and optionally adds some context fields to that logger.
func (sl spanLogger) With(fields ...zapcore.Field) log.Logger {
return spanLogger{logger: sl.logger.With(fields...)}
}
func (sl spanLogger) logToSpan(level string, msg string, fields ...zapcore.Field) {
// TODO rather than always converting the fields, we could wrap them into a lazy logger
fa := fieldAdapter(make([]otlog.Field, 0, 2+len(fields)))
fa = append(fa, otlog.String("event", msg))
fa = append(fa, otlog.String("level", level))
for _, field := range fields {
field.AddTo(&fa)
}
sl.span.LogFields(fa...)
}
type fieldAdapter []otlog.Field
func (fa *fieldAdapter) AddBool(key string, value bool) {
*fa = append(*fa, otlog.Bool(key, value))
}
func (fa *fieldAdapter) AddFloat64(key string, value float64) {
*fa = append(*fa, otlog.Float64(key, value))
}
func (fa *fieldAdapter) AddFloat32(key string, value float32) {
*fa = append(*fa, otlog.Float64(key, float64(value)))
}
func (fa *fieldAdapter) AddInt(key string, value int) {
*fa = append(*fa, otlog.Int(key, value))
}
func (fa *fieldAdapter) AddInt64(key string, value int64) {
*fa = append(*fa, otlog.Int64(key, value))
}
func (fa *fieldAdapter) AddInt32(key string, value int32) {
*fa = append(*fa, otlog.Int64(key, int64(value)))
}
func (fa *fieldAdapter) AddInt16(key string, value int16) {
*fa = append(*fa, otlog.Int64(key, int64(value)))
}
func (fa *fieldAdapter) AddInt8(key string, value int8) {
*fa = append(*fa, otlog.Int64(key, int64(value)))
}
func (fa *fieldAdapter) AddUint(key string, value uint) {
*fa = append(*fa, otlog.Uint64(key, uint64(value)))
}
func (fa *fieldAdapter) AddUint64(key string, value uint64) {
*fa = append(*fa, otlog.Uint64(key, value))
}
func (fa *fieldAdapter) AddUint32(key string, value uint32) {
*fa = append(*fa, otlog.Uint64(key, uint64(value)))
}
func (fa *fieldAdapter) AddUint16(key string, value uint16) {
*fa = append(*fa, otlog.Uint64(key, uint64(value)))
}
func (fa *fieldAdapter) AddUint8(key string, value uint8) {
*fa = append(*fa, otlog.Uint64(key, uint64(value)))
}
func (fa *fieldAdapter) AddUintptr(key string, value uintptr) {}
func (fa *fieldAdapter) AddArray(key string, marshaler zapcore.ArrayMarshaler) error { return nil }
func (fa *fieldAdapter) AddComplex128(key string, value complex128) {}
func (fa *fieldAdapter) AddComplex64(key string, value complex64) {}
func (fa *fieldAdapter) AddObject(key string, value zapcore.ObjectMarshaler) error { return nil }
func (fa *fieldAdapter) AddReflected(key string, value interface{}) error { return nil }
func (fa *fieldAdapter) OpenNamespace(key string) {}
func (fa *fieldAdapter) AddDuration(key string, value time.Duration) {
// TODO inefficient
*fa = append(*fa, otlog.String(key, value.String()))
}
func (fa *fieldAdapter) AddTime(key string, value time.Time) {
// TODO inefficient
*fa = append(*fa, otlog.String(key, value.String()))
}
func (fa *fieldAdapter) AddBinary(key string, value []byte) {
*fa = append(*fa, otlog.Object(key, value))
}
func (fa *fieldAdapter) AddByteString(key string, value []byte) {
*fa = append(*fa, otlog.Object(key, value))
}
func (fa *fieldAdapter) AddString(key, value string) {
if key != "" && value != "" {
*fa = append(*fa, otlog.String(key, value))
}
}