forked from michenriksen/aquatone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
85 lines (68 loc) · 1.48 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package core
import (
"fmt"
"os"
"sync"
"github.com/fatih/color"
)
const (
FATAL = 5
ERROR = 4
WARN = 3
IMPORTANT = 2
INFO = 1
DEBUG = 0
)
var LogColors = map[int]*color.Color{
FATAL: color.New(color.FgRed).Add(color.Bold),
ERROR: color.New(color.FgRed),
WARN: color.New(color.FgYellow),
IMPORTANT: color.New(color.Bold),
DEBUG: color.New(color.FgCyan).Add(color.Faint),
}
type Logger struct {
sync.Mutex
debug bool
silent bool
}
func (l *Logger) SetSilent(s bool) {
l.silent = s
}
func (l *Logger) SetDebug(d bool) {
l.debug = d
}
func (l *Logger) Log(level int, format string, args ...interface{}) {
l.Lock()
defer l.Unlock()
if level == DEBUG && !l.debug {
return
} else if level < ERROR && l.silent {
return
}
if c, ok := LogColors[level]; ok {
c.Printf(format, args...)
} else {
fmt.Printf(format, args...)
}
if level == FATAL {
os.Exit(1)
}
}
func (l *Logger) Fatal(format string, args ...interface{}) {
l.Log(FATAL, format, args...)
}
func (l *Logger) Error(format string, args ...interface{}) {
l.Log(ERROR, format, args...)
}
func (l *Logger) Warn(format string, args ...interface{}) {
l.Log(WARN, format, args...)
}
func (l *Logger) Important(format string, args ...interface{}) {
l.Log(IMPORTANT, format, args...)
}
func (l *Logger) Info(format string, args ...interface{}) {
l.Log(INFO, format, args...)
}
func (l *Logger) Debug(format string, args ...interface{}) {
l.Log(DEBUG, format, args...)
}