-
-
Notifications
You must be signed in to change notification settings - Fork 357
/
log.go
189 lines (163 loc) · 3.37 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
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
package log
import (
"fmt"
"io"
"os"
"strings"
"time"
)
var (
// CurrentLevel describe the current log level. Only entries with a level
// inferior or equal to the current level are printed.
CurrentLevel = WarnLevel
outpout io.Writer = os.Stderr
timeLayout = "2006/01/02 15:04:05"
)
// Log colors
var (
DefaultColor = "\033[00m"
InfoColor = "\033[94m"
ErrorColor = "\033[91m"
WarnColor = "\033[93m"
DebugColor = "\033[90m"
)
// Constants that describe log level.
const (
InfoLevel Level = iota
ErrorLevel
WarnLevel
DebugLevel
)
// Level represents a log level.
type Level int
func (l Level) String() string {
switch l {
case InfoLevel:
return "INFO"
case ErrorLevel:
return "ERROR"
case WarnLevel:
return "WARN"
default:
return "DEBUG"
}
}
func (l Level) color() string {
switch l {
case InfoLevel:
return InfoColor
case ErrorLevel:
return ErrorColor
case WarnLevel:
return WarnColor
default:
return DebugColor
}
}
// Info logs an info message.
func Info(v ...interface{}) Entry {
return Log(Entry{
Level: InfoLevel,
Message: sprint(v...),
})
}
// Infof logs an info message according to the given format.
func Infof(format string, v ...interface{}) Entry {
return Log(Entry{
Level: InfoLevel,
Message: fmt.Sprintf(format, v...),
})
}
// Error logs an error message.
func Error(v ...interface{}) Entry {
return Log(Entry{
Level: ErrorLevel,
Message: sprint(v...),
})
}
// Errorf logs an error message according to the given format.
func Errorf(format string, v ...interface{}) Entry {
return Log(Entry{
Level: ErrorLevel,
Message: fmt.Sprintf(format, v...),
})
}
// Warn logs an warning message.
func Warn(v ...interface{}) Entry {
return Log(Entry{
Level: WarnLevel,
Message: sprint(v...),
})
}
// Warnf logs an warning message according to the given format.
func Warnf(format string, v ...interface{}) Entry {
return Log(Entry{
Level: WarnLevel,
Message: fmt.Sprintf(format, v...),
})
}
// Debug logs an debug message.
func Debug(v ...interface{}) Entry {
return Log(Entry{
Level: DebugLevel,
Message: sprint(v...),
})
}
// Debugf logs an debug message according to the given format.
func Debugf(format string, v ...interface{}) Entry {
return Log(Entry{
Level: DebugLevel,
Message: fmt.Sprintf(format, v...),
})
}
// Log logs the given entry.
func Log(e Entry) Entry {
e.printMessage()
for k, v := range e.Tags {
e.printTag(k, v)
}
return e
}
// Entry represents a log entry.
type Entry struct {
Level Level
Message string
Tags map[string]string
}
// T appends and logs the tag described by the given key/value.
func (e Entry) T(key string, value interface{}) Entry {
if e.Tags == nil {
e.Tags = make(map[string]string)
}
v := fmt.Sprintf("%+v", value)
e.Tags[key] = v
e.printTag(key, v)
return e
}
// Panic call panic with the entry message.
func (e Entry) Panic() {
panic(e.Message)
}
func (e Entry) printMessage() {
if e.Level > CurrentLevel {
return
}
fmt.Fprintf(outpout, "%s%s%s %s %s‣%s %s\n",
e.Level.color(),
e.Level,
DefaultColor,
time.Now().Format(timeLayout),
e.Level.color(),
DefaultColor,
e.Message,
)
}
func (e Entry) printTag(k, v string) {
if e.Level > CurrentLevel {
return
}
fmt.Fprintf(outpout, " %s: %s\n", k, v)
}
func sprint(v ...interface{}) string {
return strings.TrimSuffix(fmt.Sprintln(v...), "\n")
}