-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache.go
74 lines (64 loc) · 1.27 KB
/
cache.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
package golog
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/fatih/color"
)
type msgLog struct {
logPath string
// Prev string // 深度对于的路径
Msg string // 日志信息
Level level // 日志级别
create time.Time // 创建日志的时间
Ctime string
// deep int // 向外的深度, Upfunc 才会用到
Color []color.Attribute // 颜色
Line string // 行号
out bool // 文件还是控制台
path string
name string
size int64 // 文件大小
format string
Hostname string
Label map[string]string
}
var cache chan msgLog
var exit chan bool
func init() {
cache = make(chan msgLog, 100000)
exit = make(chan bool)
go write()
}
func clean(ctx context.Context, dir, name string) {
for {
select {
case <-time.After(time.Duration(cleanTime) * time.Hour * 24):
fs, err := ioutil.ReadDir(dir)
if err != nil {
continue
}
for _, f := range fs {
if strings.Contains(f.Name(), name) {
os.Remove(filepath.Join(logPath, f.Name()))
}
}
case <-ctx.Done():
return
}
}
}
func write() {
for c := range cache {
c.control()
}
exit <- true
}
func Sync() {
// 等待日志写完
close(cache)
<-exit
}