-
Notifications
You must be signed in to change notification settings - Fork 210
/
log.go
184 lines (148 loc) · 4.45 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
// Package log provides the both file and console (general) logging capabilities
// to spacemesh modules such as app and identity.
package log
import (
"context"
"io"
"os"
"sync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// mainLoggerName is a name of the global logger.
const mainLoggerName = "00000.defaultLogger"
// should we format out logs in json.
var jsonLog = false
// where logs go by default.
var logwriter io.Writer
// default encoders.
var defaultEncoder = zap.NewDevelopmentEncoderConfig()
// Level is an alias to zapcore.Level.
type Level = zapcore.Level
// DefaultLevel returns the zapcore level of logging.
func DefaultLevel() Level {
return zapcore.InfoLevel
}
// Logger is an interface for our logging API.
type Logger interface {
Info(format string, args ...any)
Debug(format string, args ...any)
Error(format string, args ...any)
Warning(format string, args ...any)
With() FieldLogger
WithContext(context.Context) Log
WithName(string) Log
}
func encoder() zapcore.Encoder {
if jsonLog {
return zapcore.NewJSONEncoder(defaultEncoder)
}
return zapcore.NewConsoleEncoder(defaultEncoder)
}
// AppLog is the local app singleton logger.
var (
mu sync.RWMutex
AppLog Log
)
// GetLogger gets logger.
func GetLogger() Log {
mu.RLock()
defer mu.RUnlock()
return AppLog
}
// SetLogger sets logger.
func SetLogger(logger Log) {
mu.Lock()
defer mu.Unlock()
AppLog = logger
}
// SetupGlobal overwrites global logger.
func SetupGlobal(logger Log) {
SetLogger(NewFromLog(logger.logger.Named(mainLoggerName)))
}
func init() {
logwriter = os.Stdout
// create a basic temp os.Stdout logger
initLogging()
}
func initLogging() {
SetLogger(NewDefault(mainLoggerName))
}
// JSONLog turns JSON format on or off.
func JSONLog(b bool) {
jsonLog = b
// Need to reinitialize
initLogging()
}
// NewNop creates silent logger.
func NewNop() Log {
return NewFromLog(zap.NewNop())
}
// NewWithLevel creates a logger with a fixed level and with a set of (optional) hooks.
func NewWithLevel(module string, level zap.AtomicLevel, hooks ...func(zapcore.Entry) error) Log {
consoleSyncer := zapcore.AddSync(logwriter)
enc := encoder()
core := zapcore.NewCore(enc, consoleSyncer, level)
log := zap.New(zapcore.RegisterHooks(core, hooks...)).Named(module)
return NewFromLog(log)
}
// RegisterHooks wraps provided loggers with hooks.
func RegisterHooks(lg Log, hooks ...func(zapcore.Entry) error) Log {
core := zapcore.RegisterHooks(lg.logger.Core(), hooks...)
return NewFromLog(zap.New(core))
}
// NewDefault creates a Log with the default log level.
func NewDefault(module string) Log {
return NewWithLevel(module, zap.NewAtomicLevelAt(DefaultLevel()))
}
// NewFromLog creates a Log from an existing zap-compatible log.
func NewFromLog(l *zap.Logger) Log {
return Log{logger: l}
}
// public wrappers abstracting away logging lib impl
// Info prints formatted info level log message.
func Info(msg string, args ...any) {
GetLogger().Info(msg, args...)
}
// Debug prints formatted debug level log message.
func Debug(msg string, args ...any) {
GetLogger().Debug(msg, args...)
}
// Error prints formatted error level log message.
func Error(msg string, args ...any) {
GetLogger().Error(msg, args...)
}
// Warning prints formatted warning level log message.
func Warning(msg string, args ...any) {
GetLogger().Warning(msg, args...)
}
// Fatal prints formatted error level log message.
func Fatal(msg string, args ...any) {
GetLogger().Fatal(msg, args...)
}
// With returns a FieldLogger which you can append fields to.
func With() FieldLogger {
return FieldLogger{GetLogger().logger, GetLogger().name}
}
// Event returns a field logger with the Event field set to true.
func Event() FieldLogger {
return GetLogger().Event()
}
// Panic writes the log message and then panics.
func Panic(msg string, args ...any) {
GetLogger().Panic(msg, args...)
}
type (
// ObjectMarshaller is an alias to zapcore.ObjectMarshaller.
ObjectMarshaller = zapcore.ObjectMarshaler
// ObjectMarshallerFunc is an alias to zapcore.ObjectMarshallerFunc.
ObjectMarshallerFunc = zapcore.ObjectMarshalerFunc
// ObjectEncoder is an alias to zapcore.ObjectEncoder.
ObjectEncoder = zapcore.ObjectEncoder
// ArrayMarshaler is an alias to zapcore.ArrayMarshaller.
ArrayMarshaler = zapcore.ArrayMarshaler
// ArrayMarshalerFunc is an alias to zapcore.ArrayMarshallerFunc.
ArrayMarshalerFunc = zapcore.ArrayMarshalerFunc
// ArrayEncoder is an alias to zapcore.ArrayEncoder.
ArrayEncoder = zapcore.ArrayEncoder
)