forked from codetainerapp/codetainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
192 lines (158 loc) · 3.64 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
190
191
192
package mlog
import (
"bytes"
"fmt"
"io"
"os"
"time"
)
type Level uint8
const (
// PanicLevel 0
PanicLevel Level = iota
// FatalLevel 1
FatalLevel
// ErrorLevel 2
ErrorLevel
// WarnLevel 3
WarnLevel
// InfoLevel 4
InfoLevel
// DebugLevel 5
DebugLevel
)
var (
// DebugPrefix allows you to change its styling
DebugPrefix = "[\033[32mDEBUG\033[0m]"
// InfoPrefix allows you to change its styling
InfoPrefix = "[\033[34m~INFO\033[0m]"
// WarnPrefix allows you to change its styling
WarnPrefix = "[\033[33m!WARN\033[0m]"
// ErrorPrefix allows you to change its styling
ErrorPrefix = "[\033[31mERROR\033[0m]"
// FatalPrefix allows you to change its styling
FatalPrefix = "[\033[31mFATAL\033[0m]"
// PanicPrefix allows you to change its styling
PanicPrefix = "[\033[31mPANIC\033[0m]"
)
// Logger holds logging configurations
type Logger struct {
Level Level
Out io.Writer
Prefix string
Time bool
TimeFormat string
}
// NewLogger will initialize a new Logger struct
func New() *Logger {
return &Logger{
Out: os.Stdout,
Level: InfoLevel,
Prefix: "",
Time: false,
TimeFormat: "15:04:05",
}
}
// SetLevel allows you to have the current log level
func (log *Logger) SetLevel(l Level) {
log.Level = l
}
func (log *Logger) write(format string) {
data := bytes.NewBuffer([]byte(format))
_, err := io.Copy(log.Out, data)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err)
}
}
func (log *Logger) output(prefix string, args ...interface{}) {
log.write(fmt.Sprintf("%s %s %s %s\n",
log.Prefix,
time.Now().Format(log.TimeFormat),
prefix,
fmt.Sprint(args...),
))
}
func (log *Logger) outputf(prefix, format string, args ...interface{}) {
log.write(fmt.Sprintf("%s %s %s %s\n",
log.Prefix,
time.Now().Format(log.TimeFormat),
prefix,
fmt.Sprintf(format, args...),
))
}
// Debug output
func (log *Logger) Debug(args ...interface{}) {
if log.Level >= DebugLevel {
log.output(DebugPrefix, args...)
}
}
// Debugf output
func (log *Logger) Debugf(format string, args ...interface{}) {
if log.Level >= DebugLevel {
log.outputf(DebugPrefix, format, args...)
}
}
// Info output
func (log *Logger) Info(args ...interface{}) {
if log.Level >= InfoLevel {
log.output(InfoPrefix, args...)
}
}
// Infof output
func (log *Logger) Infof(format string, args ...interface{}) {
if log.Level >= InfoLevel {
log.outputf(InfoPrefix, format, args...)
}
}
// Warn output
func (log *Logger) Warn(args ...interface{}) {
if log.Level >= WarnLevel {
log.output(WarnPrefix, args...)
}
}
// Warnf output
func (log *Logger) Warnf(format string, args ...interface{}) {
if log.Level >= WarnLevel {
log.outputf(WarnPrefix, format, args...)
}
}
// Error output
func (log *Logger) Error(args ...interface{}) {
if log.Level >= ErrorLevel {
log.output(ErrorPrefix, args...)
}
}
// Errorf output
func (log *Logger) Errorf(format string, args ...interface{}) {
if log.Level >= ErrorLevel {
log.outputf(ErrorPrefix, format, args...)
}
}
// Fatal output
func (log *Logger) Fatal(args ...interface{}) {
if log.Level >= FatalLevel {
log.output(FatalPrefix, args...)
os.Exit(-1)
}
}
// Fatalf output
func (log *Logger) Fatalf(format string, args ...interface{}) {
if log.Level >= FatalLevel {
log.outputf(FatalPrefix, format, args...)
os.Exit(-1)
}
}
// Panic output
func (log *Logger) Panic(args ...interface{}) {
if log.Level >= PanicLevel {
log.output(PanicPrefix, args...)
os.Exit(-1)
}
}
// Panicf output
func (log *Logger) Panicf(format string, args ...interface{}) {
if log.Level >= PanicLevel {
log.outputf(PanicPrefix, format, args...)
os.Exit(-1)
}
}