-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.go
More file actions
134 lines (114 loc) · 2.52 KB
/
Copy pathtask.go
File metadata and controls
134 lines (114 loc) · 2.52 KB
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
package golog
import (
"sync"
"time"
)
type task struct {
cache chan msgLog
exit chan struct{}
wg *sync.WaitGroup
handlerWg *sync.WaitGroup
}
var t *task
func init() {
t = &task{
cache: make(chan msgLog, 1000),
exit: make(chan struct{}),
wg: &sync.WaitGroup{},
handlerWg: &sync.WaitGroup{},
}
// exit = make(chan bool)
// 增加1024字节的缓存, 也就是假设每一条日志的最大长度是1024
go t.write()
}
func (t *task) write() {
cl := msgLog{
// buf: bytes.NewBuffer(nil),
// now: time.Now(),
}
// go SecondCache()
ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop() // 主函数退出前停止 Ticker,防止 goroutine 泄漏
for {
select {
case <-ticker.C:
// 不管有没有写满, 200毫秒必须
if len(cl.Msg) > 0 {
t.control(cl)
cl.Msg = ""
cl.day = 0
}
case c, ok := <-t.cache:
// fmt.Println("--------------", c.Msg)
if !ok {
// 关闭通道 的操作
if len(cl.Msg) > 0 {
t.control(cl)
cl.Msg = ""
cl.day = 0
}
t.exit <- struct{}{}
return
}
if c.out {
// 控制台才添加颜色, 否则不添加颜色
c.Color = GetColor(c.Level)
}
c.Msg = c.format(c.Level, c.Ctime, c.Line, c.Msg)
if c.out {
// 有带颜色日志要实时打印
t.control(c)
cl.Msg = ""
cl.day = 0
continue
}
if cl.day != 0 && c.Ctime.Day() != cl.day {
t.control(cl)
cl.Msg = ""
cl.day = 0
}
cl.dir = c.dir
cl.out = c.out
cl.name = c.name
cl.day = c.Ctime.Day()
cl.everyDay = c.everyDay
cl.Ctime = c.Ctime
cl.size = c.size
cl.Color = c.Color
cl.Msg += c.Msg
if len(cl.Msg) < BLOCKSIZE {
// 缓存没满就继续
continue
}
t.control(cl)
cl.Msg = ""
cl.day = 0
}
}
}
var _expireClean time.Duration = time.Hour * 24 * 7
// 设置清理时间 默认365天
func SetExpireDuration(d time.Duration) {
if d > 0 {
_expireClean = d
}
}
func Sync() {
// 等待所有通道写完日志写完, 可以不写,
// time.Sleep(1 * time.Millisecond * 300)
t.handlerWg.Wait()
t.wg.Wait()
close(t.cache)
<-t.exit
}
func (l *Log) Sync() {
// 等待所有通道写完日志写完, 可以不写,
// time.Sleep(1 * time.Millisecond * 300)
// if !checkName(l.Name) {
// return
// }
l.task.handlerWg.Wait()
l.task.wg.Wait()
close(l.task.cache)
<-l.task.exit
}