forked from go-ozzo/ozzo-log
-
Notifications
You must be signed in to change notification settings - Fork 2
/
target_file.go
332 lines (304 loc) · 7.5 KB
/
target_file.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
// Copyright 2015 Qiang Xue. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package log
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
)
// FileTarget writes filtered log messages to a file.
// FileTarget supports file rotation by keeping certain number of backup log files.
type FileTarget struct {
*Filter
// the log file name. When Rotate is true, log file name will be suffixed
// to differentiate different backup copies (e.g. app.log.1)
FileName string
// whether to enable file rotating at specific time interval or when maximum file size is reached.
Rotate bool
// how many log files should be kept when Rotate is true (the current log file is not included).
// This field is ignored when Rotate is false.
BackupCount int
// maximum number of bytes allowed for a log file. Zero means no limit.
// This field is ignored when Rotate is false.
MaxBytes int64
fd *os.File
currentBytes int64
errWriter io.Writer
close chan bool
timeFormat string
openedFile string
scaned bool
filePrefix string
fileSuffix string
mutex sync.Mutex
logFiles logFiles
}
// NewFileTarget creates a FileTarget.
// The new FileTarget takes these default options:
// MaxLevel: LevelDebug, Rotate: true, BackupCount: 10, MaxBytes: 1 << 20
// You must specify the FileName field.
func NewFileTarget() *FileTarget {
return &FileTarget{
Filter: &Filter{MaxLevel: LevelDebug},
Rotate: true,
BackupCount: DefaultFileBackupCount,
MaxBytes: DefaultFileMaxBytes,
close: make(chan bool),
}
}
// Open prepares FileTarget for processing log messages.
func (t *FileTarget) Open(errWriter io.Writer) (err error) {
t.mutex.Lock()
defer t.mutex.Unlock()
t.Filter.Init()
if len(t.FileName) == 0 {
return errors.New("FileTarget.FileName must be set")
}
t.timeFormat = ``
t.openedFile = ``
t.filePrefix, t.fileSuffix, t.timeFormat, t.FileName, err = DateFormatFilename(t.FileName)
if err != nil {
return
}
t.errWriter = errWriter
if t.Rotate {
if t.BackupCount < 0 {
return errors.New("FileTarget.BackupCount must be no less than 0")
}
if t.MaxBytes <= 0 {
return errors.New("FileTarget.MaxBytes must be no less than 0")
}
t.recordOldLogs()
}
return nil
}
type logFiles []*logFileInfo
type logFileInfo struct {
Path string
MTime int64
}
func (l *logFiles) Add(fpath string, mtime int64) {
*l = append(*l, &logFileInfo{
Path: fpath,
MTime: mtime,
})
}
func (l *logFiles) Len() int {
return len(*l)
}
func (l *logFiles) Less(i, j int) bool {
return (*l)[i].MTime < (*l)[j].MTime
}
func (l *logFiles) Swap(i, j int) {
(*l)[i], (*l)[j] = (*l)[j], (*l)[i]
}
func (t *FileTarget) recordOldLogs() {
if t.scaned {
return
}
t.scaned = true
files := &logFiles{}
err := filepath.Walk(filepath.Dir(t.filePrefix), func(f string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if f == t.filePrefix || strings.HasPrefix(info.Name(), `.`) {
return nil
}
if len(t.fileSuffix) > 0 && !strings.HasSuffix(f, t.fileSuffix) {
return nil
}
if strings.HasPrefix(f, t.filePrefix) {
files.Add(f, info.ModTime().UnixNano())
}
return nil
})
sort.Sort(files)
t.logFiles = *files
if err != nil {
fmt.Fprintf(t.errWriter, "%v\n", err)
} else if t.BackupCount > 0 {
for len(t.logFiles) > t.BackupCount {
if err = os.Remove(t.popFile().Path); err != nil {
fmt.Fprintf(t.errWriter, "%v\n", err)
break
}
}
}
}
func (t *FileTarget) popFile() *logFileInfo {
pathInfo := t.logFiles[0]
if len(t.logFiles) > 1 {
t.logFiles = t.logFiles[1:]
} else {
t.logFiles = t.logFiles[0:0]
}
return pathInfo
}
// Process saves an allowed log message into the log file.
func (t *FileTarget) Process(e *Entry) {
if e == nil {
t.closeFile()
t.close <- true
return
}
if !t.Allow(e) {
return
}
_, err := t.Write([]byte(e.String() + "\n"))
if err != nil {
fmt.Fprintf(t.errWriter, "FileTarge write error: %v\n", err)
}
if t.Rotate {
t.rotate()
}
}
func (t *FileTarget) Write(b []byte) (int, error) {
if t.fd == nil {
if err := t.createLogFile(t.getFileName()); err != nil {
return 0, err
}
}
n, err := t.fd.Write(b)
t.mutex.Lock()
t.currentBytes += int64(n)
t.mutex.Unlock()
return n, err
}
// Close closes the file target.
func (t *FileTarget) Close() {
<-t.close
t.closeFile()
}
func (t *FileTarget) getFileName() string {
if len(t.timeFormat) > 0 {
return fmt.Sprintf(t.FileName, time.Now().Format(t.timeFormat))
}
return t.FileName
}
func (t *FileTarget) rotate() {
fileName := t.getFileName()
if t.openedFile == fileName && t.currentBytes <= t.MaxBytes {
return
}
var err error
if t.BackupCount > 0 {
for i := len(t.logFiles) - t.BackupCount; i >= 0; i-- {
pathInfo := t.popFile()
if pathInfo.Path == fileName {
t.logFiles = append(t.logFiles, pathInfo)
if len(t.logFiles) < 2 {
break
}
pathInfo = t.popFile()
}
if err = os.Remove(pathInfo.Path); err != nil {
fmt.Fprintf(t.errWriter, "%v\n", err)
}
}
}
newPath := fileName
now := time.Now()
if t.openedFile == fileName { // 文件名没变但尺寸超过设定值
newPath = fileName + `.` + now.Format(`20060102150405.00000`)
err = os.Rename(t.openedFile, newPath)
if err != nil {
fmt.Fprintf(t.errWriter, "%v\n", err)
}
}
//println(`newPath:`, newPath)
t.logFiles = append(t.logFiles, &logFileInfo{Path: newPath, MTime: now.Unix()})
t.createLogFile(fileName)
}
func (t *FileTarget) closeFile() {
if t.fd != nil {
t.fd.Close()
t.fd = nil
}
}
func (t *FileTarget) createLogFile(fileName string) (err error) {
t.mutex.Lock()
defer t.mutex.Unlock()
t.closeFile()
t.currentBytes = 0
t.createDir(fileName)
t.fd, err = os.OpenFile(fileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
t.fd = nil
fmt.Fprintf(t.errWriter, "FileTarget was unable to create a log file: %v\n", err)
}
t.openedFile = fileName
return
}
func (t *FileTarget) createDir(fileName string) {
fdir := filepath.Dir(fileName)
if finf, err := os.Stat(fdir); err != nil || !finf.IsDir() {
err := os.MkdirAll(fdir, os.ModePerm)
if err != nil {
fmt.Fprintf(t.errWriter, "%v\n", err)
}
os.Chmod(fdir, os.ModePerm)
}
}
func DateFormatFilename(dfile string) (prefix string, suffix, dateformat string, filename string, err error) {
p := strings.Index(dfile, `{date:`)
prefix = dfile
if p > -1 {
fileName := dfile[0:p]
if p == 0 {
fileName = "./"
}
prefix = fileName
placeholder := dfile[p+6:]
p2 := strings.Index(placeholder, `}`)
var hs bool
switch fileName[len(fileName)-1] {
case '/', '\\':
hs = true
}
if fileName, err = filepath.Abs(fileName); err != nil {
return
}
if p2 > -1 {
dateformat = placeholder[0:p2]
suffix = placeholder[p2+1:]
switch filepath.Separator {
case '/':
dateformat = strings.Replace(dateformat, "\\", "/", -1)
suffix = strings.Replace(suffix, "\\", "/", -1)
case '\\':
dateformat = strings.Replace(dateformat, "/", "\\", -1)
suffix = strings.Replace(suffix, "/", "\\", -1)
}
if hs {
fileName = filepath.Join(fileName, `%v`+suffix)
} else {
fileName += `%v` + suffix
}
}
filename = fileName
if len(filename) == 0 {
err = errors.New("FileTarget.FileName must be set")
return
}
if prefix, err = filepath.Abs(prefix); err != nil {
return
}
} else {
if prefix, err = filepath.Abs(prefix); err != nil {
return
}
filename = prefix
}
return
}