forked from mgutz/logxi
/
logger.go
153 lines (114 loc) · 3.93 KB
/
logger.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package log
/*
http://en.wikipedia.org/wiki/Syslog
Code Severity Keyword
0 Emergency emerg (panic) System is unusable.
A "panic" condition usually affecting multiple apps/servers/sites. At this
level it would usually notify all tech staff on call.
1 Alert alert Action must be taken immediately.
Should be corrected immediately, therefore notify staff who can fix the
problem. An example would be the loss of a primary ISP connection.
2 Critical crit Critical conditions.
Should be corrected immediately, but indicates failure in a secondary
system, an example is a loss of a backup ISP connection.
3 Error err (error) Error conditions.
Non-urgent failures, these should be relayed to developers or admins; each
item must be resolved within a given time.
4 Warning warning (warn) Warning conditions.
Warning messages, not an error, but indication that an error will occur if
action is not taken, e.g. file system 85% full - each item must be resolved
within a given time.
5 Notice notice Normal but significant condition.
Events that are unusual but not error conditions - might be summarized in
an email to developers or admins to spot potential problems - no immediate
action required.
6 Informational info Informational messages.
Normal operational messages - may be harvested for reporting, measuring
throughput, etc. - no action required.
7 Debug debug Debug-level messages.
Info useful to developers for debugging the application, not useful during operations.
*/
const (
// LevelEnv chooses level from LOGXI environment variable or defaults
// to LevelInfo
LevelEnv = -10000
// LevelOff means logging is disabled for logger. This should always
// be first
LevelOff = -1000
// LevelEmergency is usually 0 but that is also the "zero" value
// for Go, which means whenever we do any lookup in string -> int
// map 0 is returned (not good).
LevelEmergency = -1
// LevelAlert means action must be taken immediately.
LevelAlert = 1
// LevelFatal means it should be corrected immediately, eg cannot connect to database.
LevelFatal = 2
// LevelCritical is alias for LevelFatal
LevelCritical = 2
// LevelError is a non-urgen failure to notify devlopers or admins
LevelError = 3
// LevelWarn indiates an error will occur if action is not taken, eg file system 85% full
LevelWarn = 4
// LevelNotice is normal but significant condition.
LevelNotice = 5
// LevelInfo is info level
LevelInfo = 6
// LevelDebug is debug level
LevelDebug = 7
// LevelTrace is trace level and displays file and line in terminal
LevelTrace = 10
// LevelAll is all levels
LevelAll = 1000
)
// FormatHappy uses HappyDevFormatter
const FormatHappy = "happy"
// FormatText uses TextFormatter
const FormatText = "text"
// FormatJSON uses JSONFormatter
const FormatJSON = "JSON"
// FormatEnv selects formatter based on LOGXI_FORMAT environment variable
const FormatEnv = ""
// LevelMap maps int enums to string level.
var LevelMap = map[int]string{
LevelFatal: "FTL",
LevelError: "ERR",
LevelWarn: "WRN",
LevelInfo: "INF",
LevelDebug: "DBG",
LevelTrace: "TRC",
}
// LevelMap maps int enums to string level.
var LevelAtoi = map[string]int{
"OFF": LevelOff,
"FTL": LevelFatal,
"ERR": LevelError,
"WRN": LevelWarn,
"INF": LevelInfo,
"DBG": LevelDebug,
"TRC": LevelTrace,
"ALL": LevelAll,
"off": LevelOff,
"fatal": LevelFatal,
"error": LevelError,
"warn": LevelWarn,
"info": LevelInfo,
"debug": LevelDebug,
"trace": LevelTrace,
"all": LevelAll,
}
// Logger is the interface for logging.
type Logger interface {
Trace(msg string, args ...interface{})
Debug(msg string, args ...interface{})
Info(msg string, args ...interface{})
Warn(msg string, args ...interface{}) error
Error(msg string, args ...interface{}) error
Fatal(msg string, args ...interface{})
Log(level int, msg string, args []interface{})
SetLevel(int)
IsTrace() bool
IsDebug() bool
IsInfo() bool
IsWarn() bool
// Error, Fatal not needed, those SHOULD always be logged
}