/
util.go
291 lines (272 loc) · 6.2 KB
/
util.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package zztlog
import (
"bufio"
"github.com/gookit/color"
"io"
"log"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
type logHandler struct {
m sync.Mutex
out io.Writer
buf []byte
isInit bool
currentLine int64
newFileName string
}
type BaseConfig struct {
LogConfig LogConfig `json:"log_config"`
}
type LogConfig struct {
TimeFormat string `json:"time_format"`
Prefix string `json:"prefix"`
FileOutput bool `json:"file_output"`
CmdOutput bool `json:"cmd_output"`
FileAllPathOutput bool `json:"file_all_path_output"`
FuncNameOutput bool `json:"func_name_output"`
MaxFileLine int64 `json:"max_file_line"`
MaxSizeM int64 `json:"max_size_m"`
SaveFileName string `json:"save_file_name"`
ErrorOutput bool `json:"error_output"`
FatalOutput bool `json:"fatal_output"`
WarnOutput bool `json:"warn_output"`
InfoOutput bool `json:"info_output"`
DebugOutput bool `json:"debug_output"`
ColourOutput bool `json:"colour_output"`
}
func (l *logHandler) output(name, s string) {
mutex := sync.Mutex{}
mutex.Lock()
defer mutex.Unlock()
if !l.isInit {
l.initConfig()
}
if !l.handlerOutput(name) {
return
}
l.out = os.Stdout
l.m.Unlock()
ptr, file, line, ok := runtime.Caller(3)
l.m.Lock()
if !ok {
file = "/???"
line = 0
}
//判断文件路径显示
var f string
if config.LogConfig.FileAllPathOutput {
f = file
} else {
fileList := strings.Split(file, "/")
f = fileList[len(fileList)-1]
}
if config.LogConfig.Prefix != "" {
l.buf = append(l.buf, config.LogConfig.Prefix+" "...)
}
l.buf = append(l.buf, "["...)
l.buf = append(l.buf, name...)
l.buf = append(l.buf, "] "...)
nowTime := time.Now().Format(config.LogConfig.TimeFormat + " ")
l.buf = append(l.buf, nowTime...)
l.buf = append(l.buf, f...)
l.buf = append(l.buf, ":"...)
l.buf = append(l.buf, strconv.Itoa(line)...)
if config.LogConfig.FuncNameOutput {
funcName := runtime.FuncForPC(ptr).Name()
funcName = path.Base(funcName)
l.buf = append(l.buf, " ["...)
l.buf = append(l.buf, funcName...)
l.buf = append(l.buf, "]"...)
}
l.buf = append(l.buf, " "+s...)
if len(s) == 0 || s[len(s)-1] != '\n' {
l.buf = append(l.buf, '\n')
}
//判断是否写入文件还是cmd
if config.LogConfig.CmdOutput && config.LogConfig.FileOutput {
l.outputCmd(name)
l.outputFile()
} else if config.LogConfig.FileOutput {
l.outputFile()
} else {
l.outputCmd(name)
}
l.buf = nil
return
}
func (l *logHandler) initConfig() {
config.LogConfig.WarnOutput = true
config.LogConfig.InfoOutput = true
config.LogConfig.FatalOutput = true
config.LogConfig.ErrorOutput = true
config.LogConfig.DebugOutput = true
config.LogConfig.CmdOutput = true
config.LogConfig.TimeFormat = "2006-01-02 15:04:05"
}
func (l *logHandler) handlerOutput(name string) bool {
switch name {
case err:
if !config.LogConfig.ErrorOutput {
return false
}
case debug:
if !config.LogConfig.DebugOutput {
return false
}
case fatal:
if !config.LogConfig.FatalOutput {
return false
}
case info:
if !config.LogConfig.InfoOutput {
return false
}
case warn:
if !config.LogConfig.WarnOutput {
return false
}
}
return true
}
func (l *logHandler) outputCmd(name string) {
var temp []byte
if config.LogConfig.ColourOutput {
switch name {
case debug:
temp = []byte(color.Cyan.Sprint(string(l.buf)))
case err:
temp = []byte(color.Magenta.Sprint(string(l.buf)))
case fatal:
temp = []byte(color.Red.Sprint(string(l.buf)))
case info:
temp = []byte(color.Green.Sprint(string(l.buf)))
case warn:
temp = []byte(color.Yellow.Sprint(string(l.buf)))
}
} else {
temp = l.buf
}
_, err := l.out.Write(temp)
if err != nil {
log.Println(err)
}
}
func (l *logHandler) outputFile() {
fileName := "zztlog.log"
if config.LogConfig.SaveFileName != "" {
fileName = config.LogConfig.SaveFileName
}
if l.newFileName != "" {
fileName = l.newFileName
}
fileInfo, e := os.Stat(fileName)
var setSize int64
var fileSize int64
if !os.IsNotExist(e) {
l.currentLine = fileLineNumber(fileName)
fileSize = fileInfo.Size()
} else {
fileSize = -1
l.currentLine++
}
// 文件大小初始化
if config.LogConfig.MaxSizeM != 0 {
setSize = config.LogConfig.MaxSizeM * 1024 * 1024
} else {
setSize = 10 * 1024 * 1024
}
paths := filepath.Dir(fileName)
if config.LogConfig.MaxFileLine != 0 {
l.fileCutting(l.currentLine, config.LogConfig.MaxFileLine, fileName, paths)
if l.currentLine >= config.LogConfig.MaxFileLine {
l.currentLine = 0
}
return
}
l.fileCutting(fileSize, setSize, fileName, paths)
}
// 文件切割
func (l *logHandler) fileCutting(a, b int64, fileName, paths string) {
m := sync.Mutex{}
m.Lock()
defer m.Unlock()
// 如果目录不存在,创建
if paths != "" {
if _, err := os.Stat(paths); os.IsNotExist(err) {
if err = os.MkdirAll(paths, os.ModePerm); err != nil {
log.Panic(err)
return
}
}
}
if a >= b {
// 创建新的文件
fileName = time.Now().Format(config.LogConfig.TimeFormat) + ".log"
if paths != "" {
fileName = paths + string(os.PathSeparator) + fileName
}
l.newFileName = fileName
createFile(fileName)
} else {
createFile(fileName)
}
}
func fileLineNumber(path string) int64 {
file, err := os.Open(path)
if err != nil {
log.Panic(err)
return 0
}
defer file.Close()
fileScanner := bufio.NewScanner(file)
var lineCount int64
for fileScanner.Scan() {
lineCount++
}
return lineCount
}
func createFile(fileName string) {
file, err := os.OpenFile(fileName, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
log.Panic(err)
return
}
defer file.Close()
_, err = file.Write(l.buf)
if err != nil {
log.Panic(err)
return
}
}
func (l *logHandler) debug(s string) {
l.m.Lock()
defer l.m.Unlock()
l.output(debug, s)
}
func (l *logHandler) info(s string) {
l.m.Lock()
defer l.m.Unlock()
l.output(info, s)
}
func (l *logHandler) warn(s string) {
l.m.Lock()
defer l.m.Unlock()
l.output(warn, s)
}
func (l *logHandler) fatal(s string) {
l.m.Lock()
defer l.m.Unlock()
l.output(fatal, s)
}
func (l *logHandler) error(s string) {
l.m.Lock()
defer l.m.Unlock()
l.output(err, s)
}