/
report.go
162 lines (133 loc) · 4.2 KB
/
report.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
// Copyright 2021 Matrix Origin
//
// 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 logutil
import (
"context"
"io"
"sync/atomic"
"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
)
var gLogConfigs atomic.Value
// EnableLog to get log config's !DisableLog
func EnableLog() bool {
return !gLogConfigs.Load().(*LogConfig).DisableLog
}
func EnableStoreDB() bool {
return !gLogConfigs.Load().(*LogConfig).DisableStore
}
func setGlobalLogConfig(cfg *LogConfig) {
gLogConfigs.Store(cfg)
}
func getGlobalLogConfig() *LogConfig {
return gLogConfigs.Load().(*LogConfig)
}
type ZapSink struct {
enc zapcore.Encoder
out zapcore.WriteSyncer
}
// zapReporter should be trace.ReportZap
var zapReporter atomic.Value
// contextField should be trace.ContextField function
var contextField atomic.Value
type TraceReporter struct {
ReportZap reportZapFunc
ContextField contextFieldFunc
}
type reportZapFunc func(zapcore.Encoder, zapcore.Entry, []zapcore.Field) (*buffer.Buffer, error)
type contextFieldFunc func(context.Context) zap.Field
func noopReportZap(zapcore.Encoder, zapcore.Entry, []zapcore.Field) (*buffer.Buffer, error) {
return buffer.NewPool().Get(), nil
}
func noopContextField(context.Context) zap.Field { return zap.String("span", "{}") }
func SetLogReporter(r *TraceReporter) {
if r.ReportZap != nil {
zapReporter.Store(r.ReportZap)
}
if r.ContextField != nil {
contextField.Store(r.ContextField)
}
}
func GetReportZapFunc() reportZapFunc {
return zapReporter.Load().(reportZapFunc)
}
func GetContextFieldFunc() contextFieldFunc {
return contextField.Load().(contextFieldFunc)
}
var _ zapcore.Encoder = (*TraceLogEncoder)(nil)
type TraceLogEncoder struct {
zapcore.Encoder
spanContextField zap.Field
discardable bool
}
func (e *TraceLogEncoder) Clone() zapcore.Encoder {
return &TraceLogEncoder{
Encoder: e.Encoder.Clone(),
}
}
var SpanFieldKey atomic.Value
func (e *TraceLogEncoder) AddObject(key string, val zapcore.ObjectMarshaler) error {
if key == SpanFieldKey.Load().(string) {
//e.sp = obj.(*trace.SpanContext)
e.spanContextField = zap.Object(key, val)
return nil
}
return e.Encoder.AddObject(key, val)
}
// AddBool implements zapcore.ObjectEncoder, hook zapcore.Core's With(...) api
func (e *TraceLogEncoder) AddBool(key string, value bool) {
if key == MOInternalFiledKeyDiscardable {
e.discardable = true
}
}
func (e *TraceLogEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
if e.spanContextField.Key == SpanFieldKey.Load().(string) {
fields = append(fields, e.spanContextField)
}
if e.discardable {
fields = append(fields, Discardable())
}
for _, v := range fields {
if v.Type == zapcore.BoolType && v.Key == MOInternalFiledKeyNoopReport {
return e.Encoder.EncodeEntry(entry, fields[:0])
}
}
return GetReportZapFunc()(e.Encoder, entry, fields)
}
const MOInternalFiledKeyNoopReport = "MOInternalFiledKeyNoopReport"
// MOInternalFiledKeyDiscardable mark
const MOInternalFiledKeyDiscardable = "MODiscardable"
func newTraceLogEncoder() *TraceLogEncoder {
// default like zap.NewProductionEncoderConfig(), but clean core-elems ENCODE
e := &TraceLogEncoder{
Encoder: zapcore.NewJSONEncoder(
zapcore.EncoderConfig{
StacktraceKey: "",
SkipLineEnding: true,
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.EpochTimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}),
}
return e
}
func getTraceLogSinks() (zapcore.Encoder, zapcore.WriteSyncer) {
return newTraceLogEncoder(), zapcore.AddSync(io.Discard)
}
func init() {
SpanFieldKey.Store("__MO_NOT_SET_SPAN")
}