-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.go
63 lines (56 loc) · 1.58 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
package log
import (
"fmt"
"math"
"unsafe"
ua "go.uber.org/atomic"
)
// A Level is a logging priority. Higher levels are more important.
type Level int8
// Define logging Levels. Default level is InfoLevel.
const (
// DebugLevel logs are typically voluminous, and are usually disabled in
// production.
DebugLevel Level = iota - 1
// InfoLevel is the default logging priority.
InfoLevel
// WarnLevel logs are more important than Info, but don't need individual
// human review.
WarnLevel
// ErrorLevel logs are high-priority. If an application is running smoothly,
// it shouldn't generate any error-level logs.
ErrorLevel
// ClosedLevel logs output nothing.
ClosedLevel = math.MaxInt8
)
var _levelNames = ua.NewUnsafePointer(unsafe.Pointer(&map[Level]string{
DebugLevel: "DEBUG",
InfoLevel: "INFO",
WarnLevel: "WARN",
ErrorLevel: "ERROR",
}))
// RegisterLevelName register the name of one level. If the level is already exists,
// the function will overwrite it. If the name given is empty, it register nothing,
// or it deregister a level name.
func RegisterLevelName(level Level, name string) {
for {
old := (*map[Level]string)(_levelNames.Load())
mp := make(map[Level]string, len(*old))
for l, n := range *old {
mp[l] = n
}
mp[level] = name
if _levelNames.CAS(unsafe.Pointer(old), unsafe.Pointer(&mp)) {
break
}
}
}
// String returns a lower-case ASCII representation of the log level.
func (l Level) String() string {
load := (*map[Level]string)(_levelNames.Load())
name := (*load)[l]
if len(name) == 0 {
return fmt.Sprintf("Level(%d)", l)
}
return name
}