-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
logger.go
270 lines (235 loc) · 8.44 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
package logger
import (
"fmt"
"io"
"log"
"os"
"github.com/fatih/color"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/smartcontractkit/chainlink/core/config/envvar"
"github.com/smartcontractkit/chainlink/core/static"
"github.com/smartcontractkit/chainlink/core/utils"
)
// LogsFile describes the logs file name
const LogsFile = "chainlink_debug.log"
func init() {
err := zap.RegisterSink("pretty", prettyConsoleSink(os.Stderr))
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("LOG_COLOR") != "true" {
InitColor(false)
}
}
//go:generate mockery --name Logger --output . --filename logger_mock_test.go --inpackage --case=underscore
// 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("b") // logger=a.b
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{})
// ErrorIf logs the error if present.
ErrorIf(err error, msg string)
// ErrorIfClosing calls c.Close() and logs any returned error along with name.
ErrorIfClosing(c io.Closer, name string)
// 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
// Recover reports recovered panics; this is useful because it avoids
// double-reporting to sentry
Recover(panicErr interface{})
}
// 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 {
return verShaName(static.Version, static.Sha)
}
func verShaName(ver, sha string) string {
if sha == "" {
sha = "unset"
} else if len(sha) > 7 {
sha = sha[:7]
}
if ver == "" {
ver = "unset"
}
return fmt.Sprintf("%s@%s", ver, sha)
}
// NewLogger returns a new Logger configured from environment variables, and logs any parsing errors.
// Tests should use TestLogger.
func NewLogger() (Logger, func() error) {
var c Config
var parseErrs []string
var warnings []string
var invalid string
c.LogLevel, invalid = envvar.LogLevel.Parse()
if invalid != "" {
parseErrs = append(parseErrs, invalid)
}
c.Dir = os.Getenv("LOG_FILE_DIR")
if c.Dir == "" {
var invalid2 string
c.Dir, invalid2 = envvar.RootDir.Parse()
if invalid2 != "" {
parseErrs = append(parseErrs, invalid2)
}
}
c.JsonConsole, invalid = envvar.JSONConsole.Parse()
if invalid != "" {
parseErrs = append(parseErrs, invalid)
}
var fileMaxSize utils.FileSize
fileMaxSize, invalid = envvar.LogFileMaxSize.Parse()
if invalid != "" {
parseErrs = append(parseErrs, invalid)
}
if fileMaxSize <= 0 {
c.FileMaxSizeMB = 0 // disabled
} else if fileMaxSize < utils.MB {
c.FileMaxSizeMB = 1 // 1Mb is the minimum accepted by logging backend
warnings = append(warnings, fmt.Sprintf("LogFileMaxSize %s is too small: using default %s", fileMaxSize, utils.FileSize(utils.MB)))
} else {
c.FileMaxSizeMB = int(fileMaxSize / utils.MB)
}
if c.DebugLogsToDisk() {
var (
fileMaxAge int64
maxBackups int64
)
fileMaxAge, invalid = envvar.LogFileMaxAge.Parse()
c.FileMaxAgeDays = int(fileMaxAge)
if invalid != "" {
parseErrs = append(parseErrs, invalid)
}
maxBackups, invalid = envvar.LogFileMaxBackups.Parse()
c.FileMaxBackups = int(maxBackups)
if invalid != "" {
parseErrs = append(parseErrs, invalid)
}
}
c.UnixTS, invalid = envvar.LogUnixTS.Parse()
if invalid != "" {
parseErrs = append(parseErrs, invalid)
}
l, close := c.New()
for _, msg := range parseErrs {
l.Error(msg)
}
for _, msg := range warnings {
l.Warn(msg)
}
return l.Named(verShaNameStatic()), close
}
type Config struct {
LogLevel zapcore.Level
Dir string
JsonConsole bool
UnixTS bool
FileMaxSizeMB int
FileMaxAgeDays int
FileMaxBackups int // files
}
// New returns a new Logger with pretty printing to stdout, prometheus counters, and sentry forwarding.
// Tests should use TestLogger.
func (c *Config) New() (Logger, func() error) {
cfg := newZapConfigProd(c.JsonConsole, c.UnixTS)
cfg.Level.SetLevel(c.LogLevel)
l, close, err := zapDiskLoggerConfig{
local: *c,
diskStats: utils.NewDiskStatsProvider(),
diskPollConfig: newDiskPollConfig(diskPollInterval),
}.newLogger(cfg)
if err != nil {
log.Fatal(err)
}
l = newSentryLogger(l)
return newPrometheusLogger(l), close
}
// 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))
}
// 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
}