-
Notifications
You must be signed in to change notification settings - Fork 3
/
level.go
77 lines (68 loc) · 1.6 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
package logger
import (
"fmt"
"github.com/valyala/bytebufferpool"
"strings"
)
// Log Severity level
//
// https://github.com/freebsd/freebsd/blob/master/sys/sys/syslog.h#L51
// From /usr/include/sys/syslog.h.
// These are the same on Linux, BSD, and OS X.
const (
EmergencyLevel Level = iota
AlertLevel
CriticalLevel
ErrorLevel
WarningLevel
NoticeLevel
InfoLevel
DebugLevel
)
var stringToLevel = map[string]Level{
"emergency": EmergencyLevel,
"alert": AlertLevel,
"critical": CriticalLevel,
"error": ErrorLevel,
"warning": WarningLevel,
"notice": NoticeLevel,
"info": InfoLevel,
"debug": DebugLevel,
}
var levelToString = map[Level]string{
EmergencyLevel: "emergency",
AlertLevel: "alert",
CriticalLevel: "critical",
ErrorLevel: "error",
WarningLevel: "warning",
NoticeLevel: "notice",
InfoLevel: "info",
DebugLevel: "debug",
}
// LevelString represent log Entry level as string
type LevelString string
// Level will return log Level for string or DebugLevel if unknown value
func (l LevelString) Level() Level {
if v, ok := stringToLevel[strings.ToLower(string(l))]; ok {
return v
}
return DebugLevel
}
// Level represent log Entry levelString
type Level int8
// String will return Level as string
func (l Level) String() string {
if v, ok := levelToString[l]; ok {
return v
}
return fmt.Sprintf("level(%d)", l)
}
// StringTo will return Level as string
func (l Level) StringTo(byteBuffer *bytebufferpool.ByteBuffer) {
if v, ok := levelToString[l]; ok {
byteBuffer.WriteString(v)
return
}
fmt.Fprintf(byteBuffer, "level(%d)", l)
return
}