-
Notifications
You must be signed in to change notification settings - Fork 110
/
logging_level.go
77 lines (65 loc) · 2.21 KB
/
logging_level.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
package config
import (
"sync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.viam.com/rdk/logging"
)
var globalLogger struct {
// These variables are initialized once at startup. No need for special synchronization.
logger logging.Logger
cmdLineDebugFlag bool
// These variables can be changed while the `viam-server` is running. Additionally, every time one
// of these is changed, we re-evaluate the log level. This mutex synchronizes the reads and writes
// that can concurrently happen.
mu sync.Mutex
fileConfigDebugFlag bool
cloudConfigDebugFlag bool
}
// InitLoggingSettings initializes the global logging settings.
func InitLoggingSettings(logger logging.Logger, cmdLineDebugFlag bool) {
globalLogger.logger = logger
globalLogger.cmdLineDebugFlag = cmdLineDebugFlag
if cmdLineDebugFlag {
logging.GlobalLogLevel.SetLevel(zapcore.DebugLevel)
logger.SetLevel(logging.DEBUG)
} else {
logging.GlobalLogLevel.SetLevel(zapcore.InfoLevel)
logger.SetLevel(logging.INFO)
}
globalLogger.logger.Info("Log level initialized: ", logging.GlobalLogLevel.Level())
}
// UpdateFileConfigDebug is used to update the debug flag whenever a file-based viam config is
// refreshed.
func UpdateFileConfigDebug(fileDebug bool) {
globalLogger.mu.Lock()
defer globalLogger.mu.Unlock()
globalLogger.fileConfigDebugFlag = fileDebug
refreshLogLevelInLock()
}
// UpdateCloudConfigDebug is used to update the debug flag whenever a cloud-based viam config
// is refreshed.
func UpdateCloudConfigDebug(cloudDebug bool) {
globalLogger.mu.Lock()
defer globalLogger.mu.Unlock()
globalLogger.cloudConfigDebugFlag = cloudDebug
refreshLogLevelInLock()
}
func refreshLogLevelInLock() {
var newLevel zapcore.Level
if globalLogger.cmdLineDebugFlag ||
globalLogger.fileConfigDebugFlag ||
globalLogger.cloudConfigDebugFlag {
// If anything wants debug logs, set the level to `Debug`.
newLevel = zap.DebugLevel
} else {
// If none of the command line, file config or cloud config ask for debug, use the `Info` log
// level.
newLevel = zap.InfoLevel
}
if logging.GlobalLogLevel.Level() == newLevel {
return
}
globalLogger.logger.Info("New log level: ", newLevel)
logging.GlobalLogLevel.SetLevel(newLevel)
}