This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimplog.go
202 lines (166 loc) · 5.26 KB
/
simplog.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
package simplog
import (
"context"
"sync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type (
// Options are the options for the logger.
Options struct {
// Debug enables debug logging.
Debug bool
// IsServer indicates whether the logger is used by a server or a client.
IsServer bool
// DisableStacktrace disables the stacktrace.
DisableStacktrace bool
}
symbols = struct {
fromZapLevel map[zapcore.Level]string
mu sync.Mutex
}
)
const (
defaultDebugSymbol = "🐞"
defaultInfoSymbol = "💡"
defaultWarningSymbol = "⚠️ "
defaultErrorSymbol = "🔥"
defaultFatalSymbol = "💀"
defaultPanicSymbol = "🚨"
defaultDPanicSymbol = "🚨"
)
var (
defaultOptions = &Options{
Debug: false,
IsServer: false,
DisableStacktrace: true,
}
// activeSymbols is used to store the active symbols. It is used to allow for changing the symbols at runtime. It
// is used by the visualLevelEncoder to encode the level to a human-readable prefix.
activeSymbols = symbols{
fromZapLevel: map[zapcore.Level]string{
zapcore.DebugLevel: defaultDebugSymbol,
zapcore.InfoLevel: defaultInfoSymbol,
zapcore.WarnLevel: defaultWarningSymbol,
zapcore.ErrorLevel: defaultErrorSymbol,
zapcore.FatalLevel: defaultFatalSymbol,
zapcore.PanicLevel: defaultPanicSymbol,
zapcore.DPanicLevel: defaultDPanicSymbol,
},
}
)
// visualLevelEncoder is a zapcore.Encoder that encodes a zapcore.Level to a human-readable prefix.
func visualLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(activeSymbols.fromZapLevel[level])
}
func productionCLIEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
FunctionKey: zapcore.OmitKey,
LevelKey: "L",
MessageKey: "M",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: visualLevelEncoder,
ConsoleSeparator: " ",
}
}
func newLogger(name string, opts *Options) *zap.SugaredLogger {
if opts == nil {
opts = defaultOptions
}
var config zap.Config
// In debug mode, client and server applications use the same configuration.
if opts.Debug {
config = zap.NewDevelopmentConfig()
} else {
config = zap.NewProductionConfig()
// Special case for client production logging. Servers should use the default production config - json encoding
if !opts.IsServer {
config.Encoding = "console"
config.EncoderConfig = productionCLIEncoderConfig()
}
}
// Optionally disable stacktrace.
if opts.DisableStacktrace {
config.DisableStacktrace = true
}
logger, err := config.Build()
if err != nil {
logger = zap.NewNop()
}
// Add the name to the logger.
logger = logger.Named(name)
return logger.Sugar()
}
func defaultLogger() *zap.SugaredLogger {
return newLogger("simplog-default", defaultOptions)
}
// NewWithOptions returns a new logger with the given options. If the options are nil, the default logger is returned.
func NewWithOptions(opts *Options) *zap.SugaredLogger {
return newLogger("simplog", opts)
}
// NewClientLogger returns a new logger that's meant to be used by client-type applications. It uses a human-readable
// format.
func NewClientLogger(debug bool) *zap.SugaredLogger {
return NewWithOptions(&Options{
Debug: debug,
IsServer: false,
DisableStacktrace: true,
})
}
// NewServerLogger returns a new logger that's meant to be used by servers. It uses structured logging when run in
// production, and a human-readable format when run in development.
func NewServerLogger(debug bool) *zap.SugaredLogger {
return NewWithOptions(&Options{
Debug: debug,
IsServer: true,
DisableStacktrace: true,
})
}
// As recommended by 'revive' linter.
type contextKey string
var loggerKey contextKey = "simplog"
// WithLogger returns a new context.Context with the given logger.
func WithLogger(ctx context.Context, logger *zap.SugaredLogger) context.Context {
return context.WithValue(ctx, loggerKey, logger)
}
// FromContext returns the logger from the given context. If the context does not contain a logger, the default logger
// is returned. If the context is nil, the default logger is returned.
func FromContext(ctx context.Context) *zap.SugaredLogger {
if logger, ok := ctx.Value(loggerKey).(*zap.SugaredLogger); ok {
return logger
}
return defaultLogger()
}
func setSymbol(level zapcore.Level, symbol string) {
activeSymbols.mu.Lock()
activeSymbols.fromZapLevel[level] = symbol
activeSymbols.mu.Unlock()
}
// SetDebugSymbol sets the debug symbol.
func SetDebugSymbol(symbol string) {
setSymbol(zapcore.DebugLevel, symbol)
}
// SetInfoSymbol sets the info symbol.
func SetInfoSymbol(symbol string) {
setSymbol(zapcore.InfoLevel, symbol)
}
// SetWarnSymbol sets the warning symbol.
func SetWarnSymbol(symbol string) {
setSymbol(zapcore.WarnLevel, symbol)
}
// SetErrorSymbol sets the error symbol.
func SetErrorSymbol(symbol string) {
setSymbol(zapcore.ErrorLevel, symbol)
}
// SetFatalSymbol sets the fatal symbol.
func SetFatalSymbol(symbol string) {
setSymbol(zapcore.FatalLevel, symbol)
}
// SetPanicSymbol sets the panic symbol.
func SetPanicSymbol(symbol string) {
setSymbol(zapcore.PanicLevel, symbol)
}
// SetDPanicSymbol sets the dpanic symbol.
func SetDPanicSymbol(symbol string) {
setSymbol(zapcore.DPanicLevel, symbol)
}