-
Notifications
You must be signed in to change notification settings - Fork 23
/
level.go
93 lines (84 loc) · 2.64 KB
/
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package logging
import (
"fmt"
"sync"
)
// Type definition for log level
type LogLevelType uint8
// Default levels and level names, these can be replaced with any positive set
// of values having corresponding names. There is a pseudo-level, NOTSET, which
// is only really there as a lower limit for user-defined levels. Handlers and
// loggers are initialized with NOTSET so that they will log all messages, even
// at user-defined levels.
const (
LevelCritical LogLevelType = 50
LevelFatal LogLevelType = LevelCritical
LevelError LogLevelType = 40
LevelWarning LogLevelType = 30
LevelWarn LogLevelType = LevelWarning
LevelInfo LogLevelType = 20
LevelDebug LogLevelType = 10
LevelTrace LogLevelType = 5
LevelNotset LogLevelType = 0
)
var (
levelToNames = map[LogLevelType]string{
LevelFatal: "FATAL",
LevelError: "ERROR",
LevelWarn: "WARN",
LevelInfo: "INFO",
LevelDebug: "DEBUG",
LevelTrace: "TRACE",
LevelNotset: "NOTSET",
}
nameToLevels = map[string]LogLevelType{
"FATAL": LevelFatal,
"ERROR": LevelError,
"WARN": LevelWarn,
"INFO": LevelInfo,
"DEBUG": LevelDebug,
"TRACE": LevelTrace,
"NOTSET": LevelNotset,
}
levelLock sync.RWMutex
)
// Print the name of corresponding log level.
func (level LogLevelType) String() string {
return GetLevelName(level)
}
// Return the textual representation of specified logging level.
// If the level is not registered by calling AddLevel(), ok would be false.
func getLevelName(level LogLevelType) (name string, ok bool) {
levelLock.RLock()
defer levelLock.RUnlock()
levelName, ok := levelToNames[level]
return levelName, ok
}
// Return the textual representation of specified logging level.
// If the level is one of the predefined levels (LevelFatal, LevelError,
// LevelWarn, LevelInfo, LevelDebug) then you get the corresponding string.
// If you have registered level with name using AddLevel() then the name you
// associated with level is returned.
// Otherwise, the string "Level %d"(%d is level value) is returned.
func GetLevelName(level LogLevelType) (name string) {
name, ok := getLevelName(level)
if !ok {
return fmt.Sprintf("Level %d", uint8(level))
}
return name
}
// Return the level of specicial level name.
func GetNameLevel(name string) (level LogLevelType, ok bool) {
levelLock.RLock()
defer levelLock.RUnlock()
level, ok = nameToLevels[name]
return level, ok
}
// Associate levelName with level.
// This is used when converting levels to test during message formatting.
func AddLevel(level LogLevelType, levelName string) {
levelLock.Lock()
defer levelLock.Unlock()
levelToNames[level] = levelName
nameToLevels[levelName] = level
}