-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
166 lines (143 loc) · 2.83 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
package log
import (
"fmt"
"os"
"sync"
"time"
)
// Level ...
type Level int
const (
debug Level = 0
info Level = 1
warn Level = 2
err Level = 3
fatal Level = 4
)
// Logger ...
type Logger struct {
filename string
mutex *sync.Mutex
level Level
timeformat string
}
// NewLogger ...
func NewLogger(filename string, timeformat string) *Logger {
l := &Logger{
filename: filename,
mutex: &sync.Mutex{},
level: debug,
timeformat: timeformat,
}
return l
}
// TimeFormat ...
func (l *Logger) TimeFormat(timeformat string) {
l.timeformat = timeformat
}
// SetLevel ...
func (l *Logger) SetLevel(level string) {
switch level {
case "DEBUG":
l.level = debug
break
case "INFO":
l.level = info
break
case "WARN":
l.level = warn
break
case "ERROR":
l.level = err
break
case "FATAL":
l.level = fatal
break
default:
l.level = debug
}
}
// Debug ...
func (l *Logger) Debug(msg string) {
l.log(debug, msg)
}
// Debugf ...
func (l *Logger) Debugf(msg string, values ...interface{}) {
message := fmt.Sprintf(msg, values...)
l.log(debug, message)
}
// Info ...
func (l *Logger) Info(msg string) {
l.log(info, msg)
}
// Infof ...
func (l *Logger) Infof(msg string, values ...interface{}) {
message := fmt.Sprintf(msg, values...)
l.log(info, message)
}
// Warn ...
func (l *Logger) Warn(msg string) {
l.log(warn, msg)
}
// Warnf ...
func (l *Logger) Warnf(msg string, values ...interface{}) {
message := fmt.Sprintf(msg, values...)
l.log(warn, message)
}
// Error ...
func (l *Logger) Error(msg string) {
l.log(err, msg)
}
// Errorf ...
func (l *Logger) Errorf(msg string, values ...interface{}) {
message := fmt.Sprintf(msg, values...)
l.log(err, message)
}
// Fatal ...
func (l *Logger) Fatal(msg string) {
l.log(fatal, msg)
}
// Fatalf ...
func (l *Logger) Fatalf(msg string, values ...interface{}) {
message := fmt.Sprintf(msg, values...)
l.log(fatal, message)
}
func (l *Logger) log(level Level, msg string) {
if level < l.level {
return
}
l.mutex.Lock()
defer l.mutex.Unlock()
fmtMsg := fmt.Sprintf("%s %s: %s%s\n", time.Now().Format(l.timeformat), level.toString(), msg)
logFile, err := os.OpenFile(l.filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
if err != nil {
os.Stderr.WriteString((fmt.Sprintf("go-log: %s%s\n", fmtMsg)))
return
}
defer func() {
err := logFile.Close()
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("Closing failed on log file: %s\n", l.filename))
return
}
}()
_, err = logFile.WriteString(fmtMsg)
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("Writing failed to log file: %s\n", l.filename))
return
}
}
// toString ...
func (level Level) toString() string {
levels := [...]string{
"DEBUG",
"INFO",
"WARN",
"ERROR",
"FATAL",
}
if level < debug || level > fatal {
return levels[debug]
}
return levels[level]
}