forked from YoungPioneers/blog4go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.go
124 lines (105 loc) · 3.4 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2015, huangjunwei <huangjunwei@youmi.net>. All rights reserved.
package blog4go
import (
"fmt"
"strings"
)
// LevelType type defined for logging level
// just use int
type LevelType int
const (
// level enum
// TRACE trace level
TRACE LevelType = iota
// DEBUG debug level
DEBUG
// INFO info level
INFO
// WARNING warn level
WARNING
// ERROR error level
ERROR
// CRITICAL critical level
CRITICAL
// UNKNOWN unknown level
UNKNOWN = "UNKNOWN"
// DefaultLevel default level for writers
DefaultLevel = TRACE
// PrefixFormat is the level format ahead every message
PrefixFormat = " level=\"%s\" " // pure format
// ColoredPrefixFormat is the colored level format adhead every message
ColoredPrefixFormat = " level=\"\x1b[%dm%s\x1b[0m\" " // colored format
// color enum used in formating color bytes
// NOCOLOR no color
NOCOLOR = 0
// RED red color
RED = 31
// GREEN green color
GREEN = 32
// YELLOW yellow color
YELLOW = 33
// BLUE blue color
BLUE = 34
// GRAY gray color
GRAY = 37
)
var (
// LevelStrings is string present for each level
LevelStrings = [...]string{"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"}
// StringLevels is map, level strings to levels
StringLevels = map[string]LevelType{"TRACE": TRACE, "DEBUG": DEBUG, "INFO": INFO, "WARN": WARNING, "ERROR": ERROR, "CRITICAL": CRITICAL}
// Levels is a slice consist of all levels
Levels = [...]LevelType{TRACE, DEBUG, INFO, WARNING, ERROR, CRITICAL}
// Prefix is preformatted level prefix string
// help reduce string formatted burden in realtime logging
Prefix = make(map[LevelType]string)
)
func init() {
initPrefix(false) // preformat level prefix string
}
// initPrefix is designed to preformat level prefix string for each level.
// colored decide whether preformat in colored format or not.
// if colored is true, preformat level prefix string in colored format
func initPrefix(colored bool) {
if colored {
Prefix[TRACE] = fmt.Sprintf(ColoredPrefixFormat, GRAY, TRACE.String())
Prefix[DEBUG] = fmt.Sprintf(ColoredPrefixFormat, GREEN, DEBUG.String())
Prefix[INFO] = fmt.Sprintf(ColoredPrefixFormat, BLUE, INFO.String())
Prefix[WARNING] = fmt.Sprintf(ColoredPrefixFormat, YELLOW, WARNING.String())
Prefix[ERROR] = fmt.Sprintf(ColoredPrefixFormat, RED, ERROR.String())
Prefix[CRITICAL] = fmt.Sprintf(ColoredPrefixFormat, RED, CRITICAL.String())
} else {
Prefix[TRACE] = fmt.Sprintf(PrefixFormat, TRACE.String())
Prefix[DEBUG] = fmt.Sprintf(PrefixFormat, DEBUG.String())
Prefix[INFO] = fmt.Sprintf(PrefixFormat, INFO.String())
Prefix[WARNING] = fmt.Sprintf(PrefixFormat, WARNING.String())
Prefix[ERROR] = fmt.Sprintf(PrefixFormat, ERROR.String())
Prefix[CRITICAL] = fmt.Sprintf(PrefixFormat, CRITICAL.String())
}
}
// valid determines whether a Level instance is valid or not
func (level LevelType) valid() bool {
if TRACE > level || CRITICAL < level {
return false
}
return true
}
// String return string format associate with a Level instance
func (level LevelType) String() string {
if !level.valid() {
return UNKNOWN
}
return LevelStrings[level]
}
// prefix return formatted prefix string associate with a Level instance
func (level LevelType) prefix() string {
return Prefix[level]
}
// LevelFromString return Level according to given string
func LevelFromString(str string) LevelType {
level, ok := StringLevels[strings.ToUpper(str)]
if !ok {
return LevelType(-1)
}
return level
}