-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathlogger.go
303 lines (258 loc) · 9.35 KB
/
logger.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
303
package logger
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"github.com/fatih/color"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
common "github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink/v2/core/static"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
// logsFile describes the logs file name
const logsFile = "chainlink_debug.log"
// Create a standard error writer to avoid test issues around os.Stderr being
// reassigned when verbose logging is enabled
type stderrWriter struct{}
func (sw stderrWriter) Write(p []byte) (n int, err error) {
return os.Stderr.Write(p)
}
func (sw stderrWriter) Close() error {
return nil // never close stderr
}
func (sw stderrWriter) Sync() error {
return os.Stderr.Sync()
}
func init() {
err := zap.RegisterSink("pretty", prettyConsoleSink(stderrWriter{}))
if err != nil {
log.Fatalf("failed to register pretty printer %+v", err)
}
err = registerOSSinks()
if err != nil {
log.Fatalf("failed to register os specific sinks %+v", err)
}
if os.Getenv("CL_LOG_COLOR") != "true" {
InitColor(false)
}
}
var _ common.Logger = (Logger)(nil)
// Logger is the main interface of this package.
// It implements uber/zap's SugaredLogger interface and adds conditional logging helpers.
//
// Loggers should be injected (and usually Named as well): e.g. lggr.Named("<service name>")
//
// Tests
// - Tests should use a TestLogger, with NewLogger being reserved for actual
// runtime and limited direct testing.
//
// Levels
// - Fatal: Logs and then calls os.Exit(1). Be careful about using this since it does NOT unwind the stack and may exit uncleanly.
// - Panic: Unrecoverable error. Example: invariant violation, programmer error
// - Critical: Requires quick action from the node op, obviously these should happen extremely rarely. Example: failed to listen on TCP port
// - Error: Something bad happened, and it was clearly on the node op side. No need for immediate action though. Example: database write timed out
// - Warn: Something bad happened, not clear who/what is at fault. Node ops should have a rough look at these once in a while to see whether anything stands out. Example: connection to peer was closed unexpectedly. observation timed out.
// - Info: High level information. First level we’d expect node ops to look at. Example: entered new epoch with leader, made an observation with value, etc.
// - Debug: Useful for forensic debugging, but we don't expect nops to look at this. Example: Got a message, dropped a message, ...
// - Trace: Only included if compiled with the trace tag. For example: go test -tags trace ...
//
// Node Operator Docs: https://docs.chain.link/docs/configuration-variables/#log_level
type Logger interface {
// With creates a new Logger with the given arguments
With(args ...interface{}) Logger
// Named creates a new Logger sub-scoped with name.
// Names are inherited and dot-separated.
// a := l.Named("A") // logger=A
// b := a.Named("A") // logger=A.B
// Names are generally `MixedCaps`, without spaces, like Go names.
Named(name string) Logger
// SetLogLevel changes the log level for this and all connected Loggers.
SetLogLevel(zapcore.Level)
Trace(args ...interface{})
Debug(args ...interface{})
Info(args ...interface{})
Warn(args ...interface{})
Error(args ...interface{})
Critical(args ...interface{})
Panic(args ...interface{})
// Fatal logs and then calls os.Exit(1)
// Be careful about using this since it does NOT unwind the stack and may
// exit uncleanly
Fatal(args ...interface{})
Tracef(format string, values ...interface{})
Debugf(format string, values ...interface{})
Infof(format string, values ...interface{})
Warnf(format string, values ...interface{})
Errorf(format string, values ...interface{})
Criticalf(format string, values ...interface{})
Panicf(format string, values ...interface{})
Fatalf(format string, values ...interface{})
Tracew(msg string, keysAndValues ...interface{})
Debugw(msg string, keysAndValues ...interface{})
Infow(msg string, keysAndValues ...interface{})
Warnw(msg string, keysAndValues ...interface{})
Errorw(msg string, keysAndValues ...interface{})
Criticalw(msg string, keysAndValues ...interface{})
Panicw(msg string, keysAndValues ...interface{})
Fatalw(msg string, keysAndValues ...interface{})
// Sync flushes any buffered log entries.
// Some insignificant errors are suppressed.
Sync() error
// Helper creates a new logger with the number of callers skipped by caller annotation increased by skip.
// This allows wrappers and helpers to point higher up the stack (like testing.T.Helper()).
Helper(skip int) Logger
// Name returns the fully qualified name of the logger.
Name() string
}
// newZapConfigProd returns a new production zap.Config.
func newZapConfigProd(jsonConsole bool, unixTS bool) zap.Config {
config := newZapConfigBase()
if !unixTS {
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
}
if !jsonConsole {
config.OutputPaths = []string{"pretty://console"}
}
return config
}
func verShaNameStatic() string {
sha, ver := static.Short()
return fmt.Sprintf("%s@%s", ver, sha)
}
// NewLogger returns a new Logger with default configuration.
// Tests should use TestLogger.
func NewLogger() (Logger, func() error) {
var c Config
return c.New()
}
type Config struct {
LogLevel zapcore.Level
Dir string
JsonConsole bool
UnixTS bool
FileMaxSizeMB int
FileMaxAgeDays int
FileMaxBackups int // files
diskSpaceAvailableFn diskSpaceAvailableFn
diskPollConfig zapDiskPollConfig
// This is for tests only
testDiskLogLvlChan chan zapcore.Level
}
// New returns a new Logger with pretty printing to stdout and prometheus counters.
// Tests should use TestLogger.
func (c *Config) New() (Logger, func() error) {
if c.diskSpaceAvailableFn == nil {
c.diskSpaceAvailableFn = diskSpaceAvailable
}
if !c.diskPollConfig.isSet() {
c.diskPollConfig = newDiskPollConfig(diskPollInterval)
}
cfg := newZapConfigProd(c.JsonConsole, c.UnixTS)
cfg.Level.SetLevel(c.LogLevel)
var (
l Logger
closeLogger func() error
err error
)
if !c.DebugLogsToDisk() {
l, closeLogger, err = newDefaultLogger(cfg, c.UnixTS)
} else {
l, closeLogger, err = newRotatingFileLogger(cfg, *c)
}
if err != nil {
log.Fatal(err)
}
l = newPrometheusLogger(l)
l = l.With("version", verShaNameStatic())
return l, closeLogger
}
// DebugLogsToDisk returns whether debug logs should be stored in disk
func (c Config) DebugLogsToDisk() bool {
return c.FileMaxSizeMB > 0
}
// RequiredDiskSpace returns the required disk space in order to allow debug logs to be stored in disk
func (c Config) RequiredDiskSpace() utils.FileSize {
return utils.FileSize(c.FileMaxSizeMB * utils.MB * (c.FileMaxBackups + 1))
}
func (c *Config) DiskSpaceAvailable(path string) (utils.FileSize, error) {
if c.diskSpaceAvailableFn == nil {
c.diskSpaceAvailableFn = diskSpaceAvailable
}
return c.diskSpaceAvailableFn(path)
}
func (c Config) LogsFile() string {
return filepath.Join(c.Dir, logsFile)
}
// InitColor explicitly sets the global color.NoColor option.
// Not safe for concurrent use. Only to be called from init().
func InitColor(c bool) {
color.NoColor = !c
}
// newZapConfigBase returns a zap.NewProductionConfig with sampling disabled and a modified level encoder.
func newZapConfigBase() zap.Config {
cfg := zap.NewProductionConfig()
cfg.Sampling = nil
cfg.EncoderConfig.EncodeLevel = encodeLevel
return cfg
}
func newDefaultLogger(zcfg zap.Config, unixTS bool) (Logger, func() error, error) {
core, coreCloseFn, err := newDefaultLoggingCore(zcfg, unixTS)
if err != nil {
return nil, nil, err
}
l, loggerCloseFn, err := newLoggerForCore(zcfg, core)
if err != nil {
coreCloseFn()
return nil, nil, err
}
return l, func() error {
coreCloseFn()
loggerCloseFn()
return nil
}, nil
}
func newLoggerForCore(zcfg zap.Config, core zapcore.Core) (*zapLogger, func(), error) {
errSink, closeFn, err := zap.Open(zcfg.ErrorOutputPaths...)
if err != nil {
return nil, nil, err
}
return &zapLogger{
level: zcfg.Level,
SugaredLogger: zap.New(core, zap.ErrorOutput(errSink), zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel)).Sugar(),
}, closeFn, nil
}
func newDefaultLoggingCore(zcfg zap.Config, unixTS bool) (zapcore.Core, func(), error) {
encoder := zapcore.NewJSONEncoder(makeEncoderConfig(unixTS))
sink, closeOut, err := zap.Open(zcfg.OutputPaths...)
if err != nil {
return nil, nil, err
}
if zcfg.Level == (zap.AtomicLevel{}) {
return nil, nil, errors.New("missing Level")
}
filteredLogLevels := zap.LevelEnablerFunc(zcfg.Level.Enabled)
core := zapcore.NewCore(encoder, sink, filteredLogLevels)
return core, closeOut, nil
}
func newDiskCore(diskLogLevel zap.AtomicLevel, local Config) (zapcore.Core, error) {
diskUsage, err := local.DiskSpaceAvailable(local.Dir)
if err != nil || diskUsage < local.RequiredDiskSpace() {
diskLogLevel.SetLevel(disabledLevel)
}
var (
encoder = zapcore.NewConsoleEncoder(makeEncoderConfig(local.UnixTS))
sink = zapcore.AddSync(&lumberjack.Logger{
Filename: local.logFileURI(),
MaxSize: local.FileMaxSizeMB,
MaxAge: local.FileMaxAgeDays,
MaxBackups: local.FileMaxBackups,
Compress: true,
})
allLogLevels = zap.LevelEnablerFunc(diskLogLevel.Enabled)
)
return zapcore.NewCore(encoder, sink, allLogLevels), nil
}