-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlog.go
265 lines (213 loc) · 5.03 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
package asyncLog
import (
"log"
"os"
"strings"
"sync"
"time"
)
type asyncLogType struct {
files map[string]*LogFile // 下标是文件名
}
type LogFile struct {
filename string // 原始文件名(包含完整目录)
flag int // 默认为log.LstdFlags
// 同步设置
sync struct {
cycle time.Duration // 同步数据到文件的周期,默认为1秒
beginTime time.Time // 开始同步的时间,判断同步的耗时
status syncStatus // 同步状态
}
// 日志的等级
level Priority
// 缓存
cache struct {
use bool // 是否使用缓存
data []string // 缓存数据
}
// 写log时的互斥锁
writeMutex sync.Mutex
// 文件切割
logRotate struct {
rotate LogRotate // 默认按小时切割
file *os.File // 文件操作对象
suffix string // 切割后的文件名后缀
mutex sync.Mutex // 文件名锁
}
}
// log同步的状态
type syncStatus int
const (
statusInit syncStatus = iota // 初始状态
statusDoing // 同步中
statusDone // 同步已经完成
)
// 日志切割的方式
type LogRotate int
const (
RotateHour LogRotate = iota // 按小时切割
RotateDate // 按日期切割
)
const (
// 写日志时前缀的时间格式
logTimeFormat string = "2006-01-02 15:04:05"
// 文件写入mode
fileOpenMode = 0666
// 文件Flag
fileFlag = os.O_WRONLY | os.O_CREATE | os.O_APPEND
// 换行符
newlineChar = "\n"
// 缓存切片的初始容量
cacheInitCap = 64
)
// 异步日志变量
var asyncLog *asyncLogType
func init() {
asyncLog = &asyncLogType{
files: make(map[string]*LogFile),
}
timer := time.NewTicker(time.Second)
go func() {
for {
select {
case <-timer.C:
//now := time.Now()
for _, file := range asyncLog.files {
if file.sync.status != statusDoing {
go file.flush()
}
}
}
}
}()
}
func NewLogFile(filename string) *LogFile {
if lf, ok := asyncLog.files[filename]; ok {
return lf
}
lf := &LogFile{
filename: filename,
flag: log.LstdFlags,
}
// 默认按小时切割文件
lf.logRotate.rotate = RotateHour
// 默认开启缓存
lf.cache.use = true
// TODO 同步的时间周期,缓存开启才有效
lf.sync.cycle = time.Second
asyncLog.files[filename] = lf
return lf
}
func (lf *LogFile) SetFlags(flag int) {
lf.flag = flag
}
func (lf *LogFile) SetRotate(rotate LogRotate) {
lf.logRotate.rotate = rotate
}
func (lf *LogFile) SetUseCache(useCache bool) {
lf.cache.use = useCache
}
// 写缓存
func (lf *LogFile) Write(msg string) error {
if lf.flag == log.LstdFlags {
msg = time.Now().Format(logTimeFormat) + " " + msg + newlineChar
} else {
msg = msg + newlineChar
}
if lf.cache.use {
lf.writeMutex.Lock()
lf.cache.data = append(lf.cache.data, msg)
lf.writeMutex.Unlock()
return nil
}
return lf.directWrite(msg)
}
//*********************** 以下是私有函数 ************************************
// 同步缓存到文件中
func (lf *LogFile) flush() error {
lf.sync.status = statusDoing
defer func() {
lf.sync.status = statusDone
}()
// 写入log文件
file, err := lf.openFileNoCache()
if err != nil {
panic(err)
}
defer file.Close()
// 获取缓存数据
lf.writeMutex.Lock()
cache := lf.cache.data
lf.cache.data = make([]string, 0, cacheInitCap)
lf.writeMutex.Unlock()
if len(cache) == 0 {
return nil
}
_, err = file.WriteString(strings.Join(cache, ""))
if err != nil {
// 重试
_, err = file.WriteString(strings.Join(cache, ""))
if err != nil {
panic(err)
}
}
return nil
}
// 获取文件名的后缀
func (lf *LogFile) getFilenameSuffix() string {
if lf.logRotate.rotate == RotateDate {
return time.Now().Format("20060102")
}
return time.Now().Format("2006010215")
}
// 直接写入日志文件
func (lf *LogFile) directWrite(msg string) error {
file, err := lf.openFile()
//file, err := lf.openFileNoCache()
if err != nil {
panic(err)
}
//defer file.Close()
lf.writeMutex.Lock()
_, err = file.WriteString(msg)
lf.writeMutex.Unlock()
return err
}
// 打开日志文件
func (lf *LogFile) openFile() (*os.File, error) {
suffix := lf.getFilenameSuffix()
lf.logRotate.mutex.Lock()
defer lf.logRotate.mutex.Unlock()
if suffix == lf.logRotate.suffix {
return lf.logRotate.file, nil
}
logFilename := lf.filename + "." + suffix
file, err := os.OpenFile(logFilename, fileFlag, fileOpenMode)
if err != nil {
// 重试
file, err = os.OpenFile(logFilename, fileFlag, fileOpenMode)
if err != nil {
return file, err
}
}
// 关闭旧的文件
if lf.logRotate.file != nil {
lf.logRotate.file.Close()
}
lf.logRotate.file = file
lf.logRotate.suffix = suffix
return file, nil
}
// 打开日志文件(不缓存句柄)
func (lf *LogFile) openFileNoCache() (*os.File, error) {
logFilename := lf.filename + "." + lf.getFilenameSuffix()
file, err := os.OpenFile(logFilename, fileFlag, fileOpenMode)
if err != nil {
// 重试
file, err = os.OpenFile(logFilename, fileFlag, fileOpenMode)
if err != nil {
return file, err
}
}
return file, nil
}