-
Notifications
You must be signed in to change notification settings - Fork 110
/
logging.go
96 lines (82 loc) · 2.93 KB
/
logging.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
// Package logging package contains functionality for viam-server logging.
package logging
import (
"sync"
"testing"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
var (
globalMu sync.RWMutex
globalLogger = NewDebugLogger("startup")
// GlobalLogLevel should be used whenever a zap logger is created that wants to obey the debug
// flag from the CLI or robot config.
GlobalLogLevel = zap.NewAtomicLevelAt(zap.InfoLevel)
)
// ReplaceGlobal replaces the global loggers.
func ReplaceGlobal(logger Logger) {
globalMu.Lock()
globalLogger = logger
globalMu.Unlock()
}
// Global returns the global logger.
func Global() Logger {
return globalLogger
}
// NewZapLoggerConfig returns a new default logger config.
func NewZapLoggerConfig() zap.Config {
// from https://github.com/uber-go/zap/blob/2314926ec34c23ee21f3dd4399438469668f8097/config.go#L135
// but disable stacktraces, use same keys as prod, and color levels.
return zap.Config{
Level: GlobalLogLevel,
Encoding: "console",
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
DisableStacktrace: true,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
}
// NewLogger returns a new logger that outputs Info+ logs to stdout in UTC.
func NewLogger(name string) Logger {
const inUTC = true
return &impl{name, NewAtomicLevelAt(INFO), inUTC, []Appender{NewStdoutAppender()}}
}
// NewDebugLogger returns a new logger that outputs Debug+ logs to stdout in UTC.
func NewDebugLogger(name string) Logger {
const inUTC = true
return &impl{name, NewAtomicLevelAt(DEBUG), inUTC, []Appender{NewStdoutAppender()}}
}
// NewBlankLogger returns a new logger that outputs Debug+ logs in UTC, but without any
// pre-existing appenders/outputs.
func NewBlankLogger(name string) Logger {
const inUTC = true
return &impl{name, NewAtomicLevelAt(DEBUG), inUTC, []Appender{}}
}
// NewTestLogger returns a new logger that outputs Debug+ logs to stdout in local time.
func NewTestLogger(tb testing.TB) Logger {
logger, _ := NewObservedTestLogger(tb)
return logger
}
// NewObservedTestLogger is like NewTestLogger but also saves logs to an in memory observer.
func NewObservedTestLogger(tb testing.TB) (Logger, *observer.ObservedLogs) {
const inUTC = false
logger := &impl{"", NewAtomicLevelAt(DEBUG), inUTC, []Appender{}}
logger.AddAppender(NewStdoutTestAppender())
observerCore, observedLogs := observer.New(zap.LevelEnablerFunc(zapcore.DebugLevel.Enabled))
logger.AddAppender(observerCore)
return logger, observedLogs
}