-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.go
70 lines (60 loc) · 1.18 KB
/
log.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
package main
import (
"fmt"
"log"
"strconv"
)
type LogLevel int
const (
logLevelVerbose LogLevel = 0
logLevelInfo LogLevel = 1
logLevelError LogLevel = 5
logLevelFatal LogLevel = 7
)
func (l *LogLevel) UnmarshalJSON(bytes []byte) (err error) {
if len(bytes) == 0 {
*l = logLevelVerbose
return
}
s, err := strconv.Unquote(string(bytes))
if err != nil {
return
}
if len(s) == 0 {
*l = logLevelVerbose
return
}
switch s {
case "verbose":
*l = logLevelVerbose
case "info":
*l = logLevelInfo
case "error":
*l = logLevelError
case "fatal":
*l = logLevelFatal
default:
err = fmt.Errorf("invalid log level: %s", s)
}
return
}
func logVerbose(fmt string, args ...interface{}) {
if config.LogLevel <= logLevelVerbose {
log.Printf("[verbose] "+fmt, args...)
}
}
func logInfo(fmt string, args ...interface{}) {
if config.LogLevel <= logLevelInfo {
log.Printf("[info] "+fmt, args...)
}
}
func logError(fmt string, args ...interface{}) {
if config.LogLevel <= logLevelError {
log.Printf("[error] "+fmt, args...)
}
}
func logFatal(fmt string, args ...interface{}) {
if config.LogLevel <= logLevelFatal {
log.Fatalf("[fatal] "+fmt, args...)
}
}