-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
245 lines (200 loc) · 5.24 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
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
package MyLog
import (
"fmt"
"io"
"log"
"os"
"strings"
"github.com/fatih/color"
)
type BitSet uint8
const (
LgVerbose BitSet = 1 << iota // set verbose logging
LgDebug // set debug logging
LgColor // set color mode
LgBuffer // enables log buffer
LgStandard = 0
)
// color funcs
var bold = color.New(color.Bold).SprintFunc()
var red = color.New(color.FgRed).SprintFunc()
var yellow = color.New(color.FgYellow).SprintFunc()
var green = color.New(color.FgGreen).SprintFunc()
// Log is a type for structured message logging
type Log struct {
stdVar *log.Logger
infoVar *log.Logger
debugVar *log.Logger
warningVar *log.Logger
errorVar *log.Logger
panicVar *log.Logger
bufferData []string
modeRegister BitSet
}
// LogInit is a member function for Log
// Inits all logging to given file handle except panic
// Default mode is "silent" and "no color"
func (l *Log) Init(stdOut, stdErr io.Writer) {
stdFlags := log.Ldate | log.Ltime | log.Lmsgprefix
l.stdVar = log.New(stdOut, " ", stdFlags)
l.infoVar = log.New(stdOut, "INFO: ", stdFlags)
l.warningVar = log.New(stdOut, "WARN: ", stdFlags)
l.debugVar = log.New(stdErr, "DEBUG: ", stdFlags)
l.errorVar = log.New(stdErr, "ERROR: ", stdFlags)
l.panicVar = log.New(os.Stderr, "PANIC: ", stdFlags)
l.modeRegister = LgStandard
}
func (l *Log) SetFlags(flags int) {
l.stdVar.SetFlags(flags)
l.infoVar.SetFlags(flags)
l.warningVar.SetFlags(flags)
l.debugVar.SetFlags(flags)
l.errorVar.SetFlags(flags)
l.panicVar.SetFlags(flags)
}
func (l *Log) SetColorPrefix() {
if l.modeHas(LgColor) {
l.infoVar.SetPrefix(green("INFO: "))
l.warningVar.SetPrefix(yellow("WARN: "))
l.debugVar.SetPrefix(red("DEBUG: "))
l.errorVar.SetPrefix(red("ERROR: "))
l.panicVar.SetPrefix(red("PANIC: "))
}
}
func (l *Log) SetNoPrefix() {
l.stdVar.SetPrefix("")
l.infoVar.SetPrefix("")
l.warningVar.SetPrefix("")
l.debugVar.SetPrefix("")
l.errorVar.SetPrefix("")
l.panicVar.SetPrefix("")
}
func (l *Log) SetOutput(stdOut, stdErr io.Writer) {
l.stdVar.SetOutput(stdOut)
l.infoVar.SetOutput(stdOut)
l.warningVar.SetOutput(stdOut)
l.debugVar.SetOutput(stdErr)
l.errorVar.SetOutput(stdErr)
l.panicVar.SetOutput(stdErr)
}
// internal mode handling functions
func (l *Log) modeSet(flag BitSet) {
l.modeRegister = l.modeRegister | flag
}
func (l *Log) modeClear(flag BitSet) {
l.modeRegister = l.modeRegister &^ flag
}
func (l *Log) modeToggle(flag BitSet) {
l.modeRegister = l.modeRegister ^ flag
}
func (l *Log) modeHas(flag BitSet) bool {
return l.modeRegister&flag != 0
}
// Exposed mode handling functions
func (l *Log) SetInteractive() {
l.SetFlags(log.Lmsgprefix)
l.SetColorPrefix()
}
func (l *Log) EnableBuffer() {
l.modeSet(LgBuffer)
}
func (l *Log) DisableBuffer() {
l.modeClear(LgBuffer)
}
func (l *Log) SetMode(f BitSet) {
l.modeSet(f)
}
func (l *Log) ClearMode(f BitSet) {
l.modeClear(f)
}
func (l *Log) ToggleMode(f BitSet) {
l.modeToggle(f)
}
func (l *Log) GetMode() BitSet {
return l.modeRegister
}
func (l *Log) HasMode(f BitSet) bool {
return l.modeHas(f)
}
func (l *Log) SetModeBool(f BitSet, b bool) {
if b {
l.modeSet(f)
} else {
l.modeClear(f)
}
}
// Buffer Handling
func (l *Log) AddBuffer(format string, v ...interface{}) {
if l.modeHas(LgBuffer) {
l.bufferData = append(l.bufferData, fmt.Sprintf(format, v...))
}
}
func (l *Log) GetBuffer() string {
return strings.Join(l.bufferData, "\n")
}
// Intrinsic functions
func (l *Log) log(format string, v ...interface{}) {
l.stdVar.Printf(format, v...)
l.AddBuffer(format, v...)
}
func (l *Log) stdbold(format string, v ...interface{}) {
l.infoVar.Printf(bold(format), v...)
l.AddBuffer(format, v...)
}
func (l *Log) info(format string, v ...interface{}) {
l.infoVar.Printf(green(format), v...)
l.AddBuffer(format, v...)
}
func (l *Log) infobold(format string, v ...interface{}) {
l.infoVar.Printf(bold(green(format)), v...)
l.AddBuffer(format, v...)
}
func (l *Log) warn(format string, v ...interface{}) {
l.warningVar.Printf(yellow(format), v...)
l.AddBuffer(format, v...)
}
func (l *Log) debug(format string, v ...interface{}) {
l.debugVar.Printf(red(format), v...)
l.AddBuffer(format, v...)
}
func (l *Log) error(format string, v ...interface{}) {
l.errorVar.Printf(red(format), v...)
l.AddBuffer(format, v...)
}
// User functions
func (l *Log) Panic(format string, v ...interface{}) {
l.panicVar.Printf(red(format), v...)
}
func (l *Log) Standard(format string, v ...interface{}) {
l.log(format, v...)
}
func (l *Log) Bold(format string, v ...interface{}) {
l.stdbold(format, v...)
}
func (l *Log) StandardInfo(format string, v ...interface{}) {
l.info(format, v...)
}
func (l *Log) BoldInfo(format string, v ...interface{}) {
l.infobold(format, v...)
}
func (l *Log) Verbose(format string, v ...interface{}) {
if l.modeHas(LgVerbose) {
l.log(format, v...)
}
}
func (l *Log) VerboseInfo(format string, v ...interface{}) {
if l.modeHas(LgVerbose) {
l.info(format, v...)
}
}
func (l *Log) Debug(format string, v ...interface{}) {
if l.modeHas(LgDebug) {
l.debug(format, v...)
}
}
func (l *Log) Warn(format string, v ...interface{}) {
l.warn(format, v...)
}
func (l *Log) Error(format string, v ...interface{}) {
l.error(format, v...)
}