-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathlevels.go
More file actions
78 lines (65 loc) · 2.2 KB
/
levels.go
File metadata and controls
78 lines (65 loc) · 2.2 KB
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
package log
import "go.uber.org/zap/zapcore"
// LogLevel represents a log severity level. Use the package variables as an
// enum.
type LogLevel zapcore.Level
// DefaultName is the subsystem name that identifies the default log level.
const DefaultName = ""
// String returns the name of a LogLevel.
func (lvl LogLevel) String() string {
return zapcore.Level(lvl).String()
}
var (
LevelDebug = LogLevel(zapcore.DebugLevel)
LevelInfo = LogLevel(zapcore.InfoLevel)
LevelWarn = LogLevel(zapcore.WarnLevel)
LevelError = LogLevel(zapcore.ErrorLevel)
LevelDPanic = LogLevel(zapcore.DPanicLevel)
LevelPanic = LogLevel(zapcore.PanicLevel)
LevelFatal = LogLevel(zapcore.FatalLevel)
)
// Parse parses a string-based level and returns the corresponding LogLevel. An
// error is returned of the string is not the name of a supported LogLevel.
func Parse(name string) (LogLevel, error) {
var lvl zapcore.Level
err := lvl.Set(name)
return LogLevel(lvl), err
}
// LevelFromString parses a string-based level and returns the corresponding
// LogLevel.
//
// This function is maintained for v1 compatibility only and will be removed in a
// future version. New code should use Parse instead.
func LevelFromString(level string) (LogLevel, error) {
return Parse(level)
}
// DefaultLevel returns the current default LogLevel.
func DefaultLevel() LogLevel {
loggerMutex.RLock()
lvl := defaultLevel
loggerMutex.RUnlock()
return lvl
}
// SubsystemLevelName returns the current log level name for a given subsystem.
// An empty name, "", returns the default LogLevel name.
func SubsystemLevelName(subsys string) (string, error) {
if subsys == DefaultName {
return DefaultLevel().String(), nil
}
lvl, ok := levels[subsys]
if !ok {
return "", ErrNoSuchLogger
}
return lvl.Level().String(), nil
}
// SubsystemLevelNames returns a map of all facility names to their current log
// levels as strings. The map includes the default log level identified by the
// defaultName string as the map key.
func SubsystemLevelNames() map[string]string {
result := make(map[string]string, len(levels)+1)
result[DefaultName] = DefaultLevel().String()
for name, level := range levels {
result[name] = level.Level().String()
}
return result
}