/
spanlogger.go
148 lines (119 loc) · 4.61 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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log
import (
"time"
"github.com/opentracing/opentracing-go"
tag "github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
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...)
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...)
}
// With creates a child logger, and optionally adds some context fields to that logger.
func (sl spanLogger) With(fields ...zapcore.Field) Logger {
return spanLogger{logger: sl.logger.With(fields...), span: sl.span}
}
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([]log.Field, 0, 2+len(fields)))
fa = append(fa, log.String("event", msg))
fa = append(fa, log.String("level", level))
for _, field := range fields {
field.AddTo(&fa)
}
sl.span.LogFields(fa...)
}
type fieldAdapter []log.Field
func (fa *fieldAdapter) AddBool(key string, value bool) {
*fa = append(*fa, log.Bool(key, value))
}
func (fa *fieldAdapter) AddFloat64(key string, value float64) {
*fa = append(*fa, log.Float64(key, value))
}
func (fa *fieldAdapter) AddFloat32(key string, value float32) {
*fa = append(*fa, log.Float64(key, float64(value)))
}
func (fa *fieldAdapter) AddInt(key string, value int) {
*fa = append(*fa, log.Int(key, value))
}
func (fa *fieldAdapter) AddInt64(key string, value int64) {
*fa = append(*fa, log.Int64(key, value))
}
func (fa *fieldAdapter) AddInt32(key string, value int32) {
*fa = append(*fa, log.Int64(key, int64(value)))
}
func (fa *fieldAdapter) AddInt16(key string, value int16) {
*fa = append(*fa, log.Int64(key, int64(value)))
}
func (fa *fieldAdapter) AddInt8(key string, value int8) {
*fa = append(*fa, log.Int64(key, int64(value)))
}
func (fa *fieldAdapter) AddUint(key string, value uint) {
*fa = append(*fa, log.Uint64(key, uint64(value)))
}
func (fa *fieldAdapter) AddUint64(key string, value uint64) {
*fa = append(*fa, log.Uint64(key, value))
}
func (fa *fieldAdapter) AddUint32(key string, value uint32) {
*fa = append(*fa, log.Uint64(key, uint64(value)))
}
func (fa *fieldAdapter) AddUint16(key string, value uint16) {
*fa = append(*fa, log.Uint64(key, uint64(value)))
}
func (fa *fieldAdapter) AddUint8(key string, value uint8) {
*fa = append(*fa, log.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, log.String(key, value.String()))
}
func (fa *fieldAdapter) AddTime(key string, value time.Time) {
// TODO inefficient
*fa = append(*fa, log.String(key, value.String()))
}
func (fa *fieldAdapter) AddBinary(key string, value []byte) {
*fa = append(*fa, log.Object(key, value))
}
func (fa *fieldAdapter) AddByteString(key string, value []byte) {
*fa = append(*fa, log.Object(key, value))
}
func (fa *fieldAdapter) AddString(key, value string) {
if key != "" && value != "" {
*fa = append(*fa, log.String(key, value))
}
}