-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
302 lines (266 loc) · 8.11 KB
/
log.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
/*
* MIT License
*
* Copyright (c) 2022-2023 Tochemey
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package zapl
import (
"context"
"fmt"
"io"
"os"
"github.com/tochemey/gopack/log"
"github.com/tochemey/gopack/requestid"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// DefaultLogger represents the default Log to use
// This Log wraps zap under the hood
var DefaultLogger = New(log.DebugLevel, os.Stdout, os.Stderr)
// DiscardLogger is used not log anything
var DiscardLogger = New(log.InfoLevel, io.Discard)
// Info logs to INFO level.
func Info(v ...any) {
DefaultLogger.Info(v...)
}
// Infof logs to INFO level
func Infof(format string, v ...any) {
DefaultLogger.Infof(format, v...)
}
// Warning logs to the WARNING level.
func Warning(v ...any) {
DefaultLogger.Warn(v...)
}
// Warningf logs to the WARNING level.
func Warningf(format string, v ...any) {
DefaultLogger.Warnf(format, v...)
}
// Error logs to the ERROR level.
func Error(v ...any) {
DefaultLogger.Error(v...)
}
// Errorf logs to the ERROR level.
func Errorf(format string, v ...any) {
DefaultLogger.Errorf(format, v...)
}
// Fatal logs to the FATAL level followed by a call to os.Exit(1).
func Fatal(v ...any) {
DefaultLogger.Fatal(v...)
}
// Fatalf logs to the FATAL level followed by a call to os.Exit(1).
func Fatalf(format string, v ...any) {
DefaultLogger.Fatalf(format, v...)
}
// Panic logs to the PANIC level followed by a call to panic().
func Panic(v ...any) {
DefaultLogger.Panic(v...)
}
// Panicf logs to the PANIC level followed by a call to panic().
func Panicf(format string, v ...any) {
DefaultLogger.Panicf(format, v...)
}
// WithContext returns the Logger associated with the ctx.
// This will set the traceid, requestid and spanid in case there are
// in the context
func WithContext(ctx context.Context) log.Logger {
return DefaultLogger.WithContext(ctx)
}
// Log implements Logger interface with the underlying zap as
// the underlying logging library
type Log struct {
*zap.Logger
}
// enforce compilation error
var _ log.Logger = &Log{}
// New creates an instance of Log
func New(level log.Level, writers ...io.Writer) *Log {
// create the zap Log configuration
cfg := zap.NewProductionConfig()
// create the zap log core
var core zapcore.Core
// create the list of writers
syncWriters := make([]zapcore.WriteSyncer, len(writers))
for i, writer := range writers {
syncWriters[i] = zapcore.AddSync(writer)
}
// set the log level
switch level {
case log.InfoLevel:
core = zapcore.NewCore(
zapcore.NewJSONEncoder(cfg.EncoderConfig),
zap.CombineWriteSyncers(syncWriters...),
zapcore.InfoLevel,
)
case log.DebugLevel:
core = zapcore.NewCore(
zapcore.NewJSONEncoder(cfg.EncoderConfig),
zap.CombineWriteSyncers(syncWriters...),
zapcore.DebugLevel,
)
case log.WarningLevel:
core = zapcore.NewCore(
zapcore.NewJSONEncoder(cfg.EncoderConfig),
zap.CombineWriteSyncers(syncWriters...),
zapcore.WarnLevel,
)
case log.ErrorLevel:
core = zapcore.NewCore(
zapcore.NewJSONEncoder(cfg.EncoderConfig),
zap.CombineWriteSyncers(syncWriters...),
zapcore.ErrorLevel,
)
case log.PanicLevel:
core = zapcore.NewCore(
zapcore.NewJSONEncoder(cfg.EncoderConfig),
zap.CombineWriteSyncers(syncWriters...),
zapcore.PanicLevel,
)
case log.FatalLevel:
core = zapcore.NewCore(
zapcore.NewJSONEncoder(cfg.EncoderConfig),
zap.CombineWriteSyncers(syncWriters...),
zapcore.FatalLevel,
)
default:
core = zapcore.NewCore(
zapcore.NewJSONEncoder(cfg.EncoderConfig),
zap.CombineWriteSyncers(syncWriters...),
zapcore.DebugLevel,
)
}
// get the zap Log
zapLogger := zap.New(core)
// create the instance of Log and returns it
return &Log{zapLogger}
}
// Debug starts a message with debug level
func (l *Log) Debug(v ...any) {
defer l.Logger.Sync()
l.Logger.Debug(fmt.Sprint(v...))
}
// Debugf starts a message with debug level
func (l *Log) Debugf(format string, v ...any) {
defer l.Logger.Sync()
l.Logger.Debug(fmt.Sprintf(format, v...))
}
// Panic starts a new message with panic level. The panic() function
// is called which stops the ordinary flow of a goroutine.
func (l *Log) Panic(v ...any) {
defer l.Logger.Sync()
l.Logger.Panic(fmt.Sprint(v...))
}
// Panicf starts a new message with panic level. The panic() function
// is called which stops the ordinary flow of a goroutine.
func (l *Log) Panicf(format string, v ...any) {
defer l.Logger.Sync()
l.Logger.Panic(fmt.Sprintf(format, v...))
}
// Fatal starts a new message with fatal level. The os.Exit(1) function
// is called which terminates the program immediately.
func (l *Log) Fatal(v ...any) {
defer l.Logger.Sync()
l.Logger.Fatal(fmt.Sprint(v...))
}
// Fatalf starts a new message with fatal level. The os.Exit(1) function
// is called which terminates the program immediately.
func (l *Log) Fatalf(format string, v ...any) {
defer l.Logger.Sync()
l.Logger.Fatal(fmt.Sprintf(format, v...))
}
// Error starts a new message with error level.
func (l *Log) Error(v ...any) {
defer l.Logger.Sync()
l.Logger.Error(fmt.Sprint(v...))
}
// Errorf starts a new message with error level.
func (l *Log) Errorf(format string, v ...any) {
defer l.Logger.Sync()
l.Logger.Error(fmt.Sprintf(format, v...))
}
// Warn starts a new message with warn level
func (l *Log) Warn(v ...any) {
defer l.Logger.Sync()
l.Logger.Warn(fmt.Sprint(v...))
}
// Warnf starts a new message with warn level
func (l *Log) Warnf(format string, v ...any) {
defer l.Logger.Sync()
l.Logger.Warn(fmt.Sprintf(format, v...))
}
// Info starts a message with info level
func (l *Log) Info(v ...any) {
defer l.Logger.Sync()
l.Logger.Info(fmt.Sprint(v...))
}
// Infof starts a message with info level
func (l *Log) Infof(format string, v ...any) {
defer l.Logger.Sync()
l.Logger.Info(fmt.Sprintf(format, v...))
}
// LogLevel returns the log level that is used
func (l *Log) LogLevel() log.Level {
switch l.Level() {
case zapcore.FatalLevel:
return log.FatalLevel
case zapcore.PanicLevel:
return log.PanicLevel
case zapcore.ErrorLevel:
return log.ErrorLevel
case zapcore.InfoLevel:
return log.InfoLevel
case zapcore.DebugLevel:
return log.DebugLevel
case zapcore.WarnLevel:
return log.WarningLevel
default:
return log.InvalidLevel
}
}
// WithContext returns the Logger associated with the ctx.
// This will set the traceid, requestid and spanid in case there are
// in the context
func (l *Log) WithContext(ctx context.Context) log.Logger {
// define the zap core fields
var fields []zap.Field
// grab the request id from the context
requestID := requestid.FromContext(ctx)
// set the request id when it is defined
if requestID != "" {
fields = append(fields, zap.String("request_id", requestID))
}
// set the span and trace id when defined
if otSpan := trace.SpanFromContext(ctx); otSpan != nil {
// get the trace id
traceID := otSpan.SpanContext().TraceID().String()
// grab the span id
spanID := otSpan.SpanContext().SpanID().String()
fields = append(fields,
zap.String("trace_id", traceID),
zap.String("span_id", spanID),
)
}
// set the fields when set
if len(fields) > 0 {
l.Logger.With(fields...)
}
return l
}