-
Notifications
You must be signed in to change notification settings - Fork 52
/
log.go
233 lines (187 loc) · 4.57 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
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
package log
import (
"bytes"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"github.com/elastos/Elastos.ELA.Utility/elalog"
)
const (
Red = "1;31"
Green = "1;32"
Yellow = "1;33"
Pink = "1;35"
Cyan = "1;36"
)
func Color(code, msg string) string {
return fmt.Sprintf("\033[%sm%s\033[m", code, msg)
}
const (
debugLog uint8 = iota
infoLog
warnLog
errorLog
fatalLog
disableLog
)
var (
levels = []string{
debugLog: Color(Green, "[DBG]"),
infoLog: Color(Pink, "[INF]"),
warnLog: Color(Yellow, "[WRN]"),
errorLog: Color(Red, "[ERR]"),
fatalLog: Color(Red, "[FAT]"),
disableLog: "DISABLED",
}
Stdout = os.Stdout
)
const (
OutputPath = "./Logs/" // The log files output path
calldepth = 2
KBSize = int64(1024)
MBSize = KBSize * 1024
GBSize = MBSize * 1024
defaultPerLogFileSize = 20 * MBSize
defaultLogsFolderSize = 5 * GBSize
)
func GetGID() uint64 {
var buf [64]byte
b := buf[:runtime.Stack(buf[:], false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}
var logger *Logger
func levelName(level uint8) string {
if int(level) >= len(levels) {
return fmt.Sprintf("LEVEL%d", level)
}
return levels[int(level)]
}
type Logger struct {
level uint8 // The log print level
logger *log.Logger
}
func NewLogger(level uint8, maxPerLogSizeMb, maxLogsSizeMb int64) *Logger {
var perLogFileSize = defaultPerLogFileSize
var logsFolderSize = defaultLogsFolderSize
if maxPerLogSizeMb != 0 {
perLogFileSize = maxPerLogSizeMb * MBSize
}
if maxLogsSizeMb != 0 {
logsFolderSize = maxLogsSizeMb * MBSize
}
writer := elalog.NewFileWriter(OutputPath, perLogFileSize, logsFolderSize)
return &Logger{
level: level,
logger: log.New(io.MultiWriter(os.Stdout, writer), "",
log.Ldate|log.Lmicroseconds),
}
}
func Init(level uint8, maxPerLogSizeMb, maxLogsSizeMb int64) {
logger = NewLogger(level, maxPerLogSizeMb, maxLogsSizeMb)
}
func (l *Logger) SetPrintLevel(level uint8) {
l.level = level
}
func (l *Logger) Output(level uint8, a ...interface{}) {
if l.level <= level {
gidStr := strconv.FormatUint(GetGID(), 10)
a = append([]interface{}{levelName(level), "GID", gidStr + ","}, a...)
l.logger.Output(calldepth, fmt.Sprintln(a...))
}
}
func (l *Logger) Outputf(level uint8, format string, v ...interface{}) {
if l.level <= level {
v = append([]interface{}{levelName(level), "GID", GetGID()}, v...)
l.logger.Output(calldepth, fmt.Sprintf("%s %s %d, "+format+"\n", v...))
}
}
func (l *Logger) Debug(a ...interface{}) {
if l.level > debugLog {
return
}
pc, file, line, ok := runtime.Caller(calldepth)
if !ok {
return
}
fn := runtime.FuncForPC(pc)
a = append([]interface{}{fn.Name(), filepath.Base(file) + ":" + strconv.Itoa(line)}, a...)
l.Output(debugLog, a...)
}
func (l *Logger) Debugf(format string, a ...interface{}) {
if l.level > debugLog {
return
}
pc, file, line, ok := runtime.Caller(calldepth)
if !ok {
return
}
fn := runtime.FuncForPC(pc)
a = append([]interface{}{fn.Name(), filepath.Base(file), line}, a...)
l.Outputf(debugLog, format, a...)
}
func (l *Logger) Info(a ...interface{}) {
l.Output(infoLog, a...)
}
func (l *Logger) Infof(format string, a ...interface{}) {
l.Outputf(infoLog, format, a...)
}
func (l *Logger) Warn(a ...interface{}) {
l.Output(warnLog, a...)
}
func (l *Logger) Warnf(format string, a ...interface{}) {
l.Outputf(warnLog, format, a...)
}
func (l *Logger) Error(a ...interface{}) {
if l.level <= errorLog {
l.Output(errorLog, a...)
}
}
func (l *Logger) Errorf(format string, a ...interface{}) {
l.Outputf(errorLog, format, a...)
}
func (l *Logger) Fatal(a ...interface{}) {
l.Output(fatalLog, a...)
}
func (l *Logger) Fatalf(format string, a ...interface{}) {
l.Outputf(fatalLog, format, a...)
}
func Debug(a ...interface{}) {
logger.Debug(a...)
}
func Debugf(format string, a ...interface{}) {
logger.Debugf("%s %s:%d "+format, a...)
}
func Info(a ...interface{}) {
logger.Info(a...)
}
func Warn(a ...interface{}) {
logger.Warn(a...)
}
func Error(a ...interface{}) {
logger.Error(a...)
}
func Fatal(a ...interface{}) {
logger.Fatal(a...)
}
func Infof(format string, a ...interface{}) {
logger.Infof(format, a...)
}
func Warnf(format string, a ...interface{}) {
logger.Warnf(format, a...)
}
func Errorf(format string, a ...interface{}) {
logger.Errorf(format, a...)
}
func Fatalf(format string, a ...interface{}) {
logger.Fatalf(format, a...)
}
func SetPrintLevel(level uint8) {
logger.SetPrintLevel(level)
}