-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.go
executable file
·335 lines (278 loc) · 8.21 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//
// Package log implements Go to https://github.com/NiuStar/log.
//
package log
import (
"fmt"
"github.com/NiuStar/utils"
"io/ioutil"
"os"
//"runtime/debug"
"github.com/sirupsen/logrus"
"time"
)
var year int //上次写日志文件的年
var month string //上次写日志文件的月
var day int //上次写日志文件的日
var saveDays int = 30 //日志保留天数,默认不删除
//初始化,log日志保留几天,默认不删除
func Init(debug bool) {
if debug {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
logrus.SetFormatter(&logrus.JSONFormatter{})
createNewLogFile()
}
func SetSaveDays(days int) {
saveDays = days
clearOneWeekLog(saveDays)
go startTimer(saveDays)
}
func startTimer(days int) {
now := time.Now()
// 计算下一个零点
next := now.Add(time.Hour * 24)
next = time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location())
fmt.Println(utils.FormatTimeAll(next.Unix()))
second := time.Duration(next.Sub(now).Seconds())
fmt.Println("距离下一次清除log还有: ", next.Sub(now).Seconds(), " 秒")
timer(second, days)
}
func timer(seconds time.Duration, days int) {
timer := time.NewTicker(seconds * time.Second)
for {
select {
case <-timer.C:
{
go clearOneWeekLog(days)
timer2 := time.NewTicker(3600 * time.Second)
for {
select {
case <-timer2.C:
{
startTimer(days)
return
}
}
}
}
}
}
}
func clearOneWeekLog(days int) { //清除七天前那天的log
path := utils.GetCurrPath() + "log"
list := getNowFiles(path)
now := time.Now()
for _, val := range list {
value := val.(map[string]interface{})
times := value["modtime"].(time.Time)
fmt.Println(value["name"], times, times.Add(time.Hour*24*time.Duration(days-1)).Before(now))
if times.Add(time.Hour * 24 * time.Duration(days-1)).Before(now) {
name := value["name"].(string)
os.Remove(name)
}
}
}
//根据时间创建日志文件,每天创建不同的日志文件
func createNewLogFile() {
t1 := time.Now()
y, m, d := t1.Date()
if year != y || month != m.String() || d != day { //判断当前的年月日是否和记录的年月日相同,不同的话就换了一天
t3 := t1.Format("2006_01_02_15_04_05")
path := utils.GetCurrPath() + "log/log_" + t3 + ".txt"
fmt.Println(path)
logfile, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
fmt.Printf("%s\r\n", err.Error())
os.Exit(-1)
}
year = y
month = m.String()
day = d
logrus.SetOutput(logfile)
//os.Stdout = logfile
//os.Stderr = logfile
}
go startTimer(saveDays)
}
//监听当前线程的崩溃事件,拦截,写入日志文件
func InitListner(str string) {
if err := recover(); err != nil {
logrus.WithError(err.(error)).Error(str)
}
}
//获取指定目录下的所有文件,不进入下一级目录搜索,可以匹配后缀过滤。
func getNowFiles(dirPth string) []interface{} {
var nowFile []interface{}
dir, err := ioutil.ReadDir(dirPth)
if err != nil {
return nowFile
}
for _, fi := range dir {
if fi.IsDir() { // 忽略目录
continue
}
name := fi.Name()
value := make(map[string]interface{})
value["name"] = dirPth + "/" + name
value["modtime"] = fi.ModTime()
nowFile = append(nowFile, value)
}
//fmt.Println("getNowFiles end")
return nowFile
}
// Debug logs a message at level Debug on the standard logger.
func Debug(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Debug(list...)
}
// Print logs a message at level Info on the standard logger.
func Print(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Print(list...)
}
// Info logs a message at level Info on the standard logger.
func Info(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Info(args...)
}
// Warn logs a message at level Warn on the standard logger.
func Warn(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Warn(args...)
}
// Warning logs a message at level Warn on the standard logger.
func Warning(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Warning(args...)
}
// Error logs a message at level Error on the standard logger.
func Error(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Error(args...)
}
// Panic logs a message at level Panic on the standard logger.
func Panic(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Panic(args...)
}
// Fatal logs a message at level Fatal on the standard logger.
func Fatal(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Fatal(args...)
}
// Debugf logs a message at level Debug on the standard logger.
func Debugf(Tag string,format string, args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Debugf(format, args...)
}
// Printf logs a message at level Info on the standard logger.
func Printf(Tag string,format string, args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Printf(format, args...)
}
// Infof logs a message at level Info on the standard logger.
func Infof(Tag string,format string, args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Infof(format, args...)
}
// Warnf logs a message at level Warn on the standard logger.
func Warnf(Tag string,format string, args ...interface{}) {
logrus.Warnf(Tag + format, args...)
}
// Warningf logs a message at level Warn on the standard logger.
func Warningf(Tag string,format string, args ...interface{}) {
logrus.Warningf(Tag + format, args...)
}
// Errorf logs a message at level Error on the standard logger.
func Errorf(Tag string,format string, args ...interface{}) {
logrus.Errorf(Tag + format, args...)
}
// Panicf logs a message at level Panic on the standard logger.
func Panicf(Tag string,format string, args ...interface{}) {
logrus.Panicf(Tag + format, args...)
}
// Fatalf logs a message at level Fatal on the standard logger.
func Fatalf(Tag string,format string, args ...interface{}) {
logrus.Fatalf(Tag + format, args...)
}
// Debugln logs a message at level Debug on the standard logger.
func Debugln(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Debugln(list...)
}
// Println logs a message at level Info on the standard logger.
func Println(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Println(list...)
}
// Infoln logs a message at level Info on the standard logger.
func Infoln(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Infoln(list...)
}
// Warnln logs a message at level Warn on the standard logger.
func Warnln(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Warnln(list...)
}
// Warningln logs a message at level Warn on the standard logger.
func Warningln(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Warningln(list...)
}
// Errorln logs a message at level Error on the standard logger.
func Errorln(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Errorln(list...)
}
// Panicln logs a message at level Panic on the standard logger.
func Panicln(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Panicln(list...)
}
// Fatalln logs a message at level Fatal on the standard logger.
func Fatalln(Tag string,args ...interface{}) {
var list []interface{}
list = append(list, Tag)
list = append(list,args...)
logrus.Fatalln(list...)
}