-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlog.go
229 lines (194 loc) · 4.13 KB
/
mlog.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
package mlog
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/fatih/color"
)
// Log base struct
type Log struct {
timeFormat string
console bool
writer io.Writer
mu *sync.Mutex
}
var (
log *Log
)
func init() {
log = &Log{
timeFormat: "2006-01-02 15:04:05",
console: true,
mu: new(sync.Mutex),
}
}
// Get returns singleton log instance
func Get() *Log {
return log
}
// AddWriter adds a writer to log
func (l *Log) AddWriter(writer io.Writer) {
if writer == nil {
return
}
l.mu.Lock()
if l.writer == nil {
l.writer = writer
} else {
l.writer = io.MultiWriter(l.writer, writer)
}
l.mu.Unlock()
}
// SetTimeFormat sets the log timestamp layout
// see: https://golang.org/pkg/time/#Time.Format
// default format is YYYY-MM-dd HH:mm:ss
func (l *Log) SetTimeFormat(format string) {
l.mu.Lock()
l.timeFormat = format
l.mu.Unlock()
}
// SetFileWriter adds a *osFile as a writer
// path can be either relative or absolute
// if the file don't exist will be created
func (l *Log) SetFileWriter(path string) error {
if path == "" {
return errors.New("No file specified")
}
var err error
path, err = filepath.Abs(path)
if err != nil {
return err
}
var file *os.File
if _, err = os.Stat(path); err != nil {
if _, err = os.Stat(filepath.Dir(path)); err != nil {
err = os.MkdirAll(filepath.Dir(path), 0666)
if err != nil {
return err
}
}
file, err = os.Create(path)
if err != nil {
return err
}
err = file.Close()
if err != nil {
return err
}
}
file, err = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return err
}
l.AddWriter(file)
return nil
}
// EnableConsole enables the standard output
func (l *Log) EnableConsole() {
l.mu.Lock()
l.console = true
l.mu.Unlock()
}
// DisableConsole disables the standard output
func (l *Log) DisableConsole() {
l.mu.Lock()
l.console = false
l.mu.Unlock()
}
func (l *Log) out(level, format string, a []interface{}) {
if l.console || l.writer != nil {
nf := "%s [%s]: "
s := ""
date := time.Now().Format(l.timeFormat)
if len(a) == 0 {
nf += "%v"
s = fmt.Sprintf(nf, date, level, format)
} else {
nf += format
na := append([]interface{}{date, level}, a...)
s = fmt.Sprintf(nf, na...)
}
if l.console {
l.mu.Lock()
switch level {
case "DEBG":
color.HiCyan(s)
break
case "NOTI":
color.HiGreen(s)
break
case "INFO":
color.HiWhite(s)
break
case "WARN":
color.HiYellow(s)
break
case "ERR ":
color.HiRed(s)
break
case "CRIT":
color.HiMagenta(s)
break
}
l.mu.Unlock()
}
if !strings.HasSuffix(s, "\n") {
s += "\n"
}
if l.writer != nil {
l.mu.Lock()
_, _ = fmt.Fprint(l.writer, s)
l.mu.Unlock()
}
}
}
// Debug log
// Arguments are handled in the manner of fmt.Print
// a newline is appended if missing.
func (l *Log) Debug(format string, a ...interface{}) {
l.out("DEBG", format, a)
}
// Notice log
// Arguments are handled in the manner of fmt.Print
// a newline is appended if missing.
func (l *Log) Notice(format string, a ...interface{}) {
l.out("NOTI", format, a)
}
// Critical log and exit the program
// Arguments are handled in the manner of fmt.Print
// a newline is appended if missing.
func (l *Log) Critical(format string, a ...interface{}) {
l.out("CRIT", format, a)
time.Sleep(100 * time.Millisecond)
l.Warning("The program will exit in:")
for i := 5; i > 0; i-- {
l.Warning("%v seconds", i)
time.Sleep(1 * time.Second)
}
l.Info("Bye!")
time.Sleep(1 * time.Second)
os.Exit(1)
}
// Error log
// Arguments are handled in the manner of fmt.Print
// a newline is appended if missing.
func (l *Log) Error(format string, a ...interface{}) {
l.out("ERR ", format, a)
}
// Info log
// Arguments are handled in the manner of fmt.Print
// a newline is appended if missing.
func (l *Log) Info(format string, a ...interface{}) {
l.out("INFO", format, a)
}
// Warning log
// Arguments are handled in the manner of fmt.Print
// a newline is appended if missing.
func (l *Log) Warning(format string, a ...interface{}) {
l.out("WARN", format, a)
}