forked from dmuth/google-go-log4go
/
log4go.go
321 lines (250 loc) · 5.51 KB
/
log4go.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package log4go
import "fmt"
import "strings"
import "regexp"
import "time"
import "github.com/wsxiaoys/terminal/color"
const (
ErrorLevel = iota
WarnLevel = iota
InfoLevel = iota
DebugLevel = iota
TraceLevel = iota
)
//
// Our current logging level
//
var _level int
//
// Our starting timestamp
//
var startTime time.Time
var beenhere bool
//
// Do we want to display timestamps?
//
// Since I can't initialize a boolean to true, I'm going to make this an
// int with the following values:
// 0 - uninitialized (defaults to true)
// 1 - false
// 2 - true
//
// I'm not sure if this is the best method, as I am still learning Go,
// but it'll work for now!
//
var displayTime int
//
// Same thing with color. Do we want it?
//
// 0 - uninitialized (defaults to true)
// 1 - false
// 2 - true
//
var useColor int
/**
* Set our logging level.
* @param {int} The level to set to.
*/
func SetLevel(level int) {
_level = level
}
/**
* Set our logging level based on a string.
* @param {string} level The level to set. This is case-insensitive.
*/
func SetLevelString(level string) {
level = strings.ToLower(level)
if (level == "error") {
SetLevel(ErrorLevel)
} else if (level == "warn") {
SetLevel(WarnLevel)
} else if (level == "info") {
SetLevel(InfoLevel)
} else if (level == "debug") {
SetLevel(DebugLevel)
} else if (level == "trace") {
SetLevel(TraceLevel)
}
} // End of SetLevelString()
/**
* Retrieve our current logging level.
* @return {int} The current level
*/
func Level() int {
return(_level)
}
/**
* Get our display time value
* @return {bool} True if we are displaying timestamps in messages
*/
func DisplayTime() bool {
if (displayTime == 0 || displayTime == 2) {
return(true)
}
return(false)
}
/**
* Set our display time setting
* @param {bool} display True if we want to display timestamps in messages
*/
func SetDisplayTime(display bool) {
if (display) {
displayTime = 2
} else {
displayTime = 1
}
}
/**
* Get our use color value
* @return {bool} True if we are using color in messages
*/
func UseColor() bool {
if (useColor == 0 || useColor == 2) {
return(true)
}
return(false)
}
/**
* Set our use color settings
* @param {bool} use_color True if we want to use color in messages
*/
func SetUseColor(use_color bool) {
if (use_color) {
useColor = 2
} else {
useColor = 1
}
}
/**
* Log an error
*/
func Error(message string) {
print(ErrorLevel, "ERROR: " + message);
}
/**
* This and all of the other "LevelF()" functions are wrappers for Printf.
*/
func Errorf(message string, v ...interface{}) {
printf(ErrorLevel, "ERROR: " + message, v...)
}
/**
* Log a warning
*/
func Warn(message string) {
print(WarnLevel, "WARN: " + message)
}
/**
* This and all of the other "LevelF()" functions are wrappers for Printf.
*/
func Warnf(message string, v ...interface{}) {
printf(WarnLevel, "WARN: " + message, v...)
}
/**
* Log info
*/
func Info(message string) {
print(InfoLevel, "INFO: " + message)
}
/**
* This and all of the other "LevelF()" functions are wrappers for Printf.
*/
func Infof(message string, v ...interface{}) {
printf(InfoLevel, "INFO: " + message, v...)
}
/**
* Log debugigng
*/
func Debug(message string) {
print(DebugLevel, "DEBUG: " + message)
}
/**
* This and all of the other "LevelF()" functions are wrappers for Printf.
*/
func Debugf(message string, v ...interface{}) {
printf(DebugLevel, "DEBUG: " + message, v...)
}
/**
* Log a trace
*/
func Trace(message string) {
print (TraceLevel, "TRACE: " + message)
}
/**
* This and all of the other "LevelF()" functions are wrappers for Printf.
*/
func Tracef(message string, v ...interface{}) {
printf(TraceLevel, "TRACE: " + message, v...)
}
/**
* Central function for printing
*/
func print(level int, message string) {
if (!beenhere) {
startTime = time.Now()
beenhere = true
}
if (_level >= level) {
//
// Escape newlines and carriage returns
//
message = strings.Replace(message, "\n", "\\n", -1)
message = strings.Replace(message, "\r", "\\r", -1)
//
// Escape anything else that is below ASCII 32 (space)
// This is done because backspace (ASCII 8) is especially evil,
// as an attrack could inject backspaces to hide their tracks
//
re, _ := regexp.Compile("([\x00-\x1f])")
message = re.ReplaceAllStringFunc(message, func(in string) string {
return(fmt.Sprintf("[0x%x]", in))
})
//
// If displayTime is uninitialized, default to true
//
if (displayTime == 0 || displayTime == 2) {
now := time.Now()
elapsed := now.Sub(startTime)
if (UseColor()) {
color_string := getColor(level)
color.Printf(color_string + "[%s] %s\n", elapsed, message)
} else {
fmt.Printf("[%s] %s\n", elapsed, message)
}
} else {
if (UseColor()) {
color_string := getColor(level)
color.Println(color_string + message)
} else {
fmt.Println(message)
}
}
}
} // End of print()
/**
* Get a color code for a specific log level.
*
* @param {integer} level The reporting level
* @return {string} The string for the color code to return.
*/
func getColor(level int) (retval string) {
retval = ""
if (level == ErrorLevel) {
retval = "@{!r}"
} else if (level == WarnLevel) {
retval = "@{!y}"
} else if (level == InfoLevel) {
retval = "@{!g}"
} else if (level == DebugLevel) {
retval = "@{!c}"
} else if (level == TraceLevel) {
retval = "@{!b}"
}
return(retval)
} // End of getcolor()
/**
* Fancy wrapper for Printf.
*/
func printf(level int, message string, v ...interface{}) {
message_out := fmt.Sprintf(message, v...)
print(level, message_out)
}