-
Notifications
You must be signed in to change notification settings - Fork 59
/
file.go
425 lines (366 loc) · 9.37 KB
/
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package go_logger
import (
"errors"
"github.com/phachon/go-logger/utils"
"os"
"path"
"reflect"
"strings"
"sync"
"time"
)
const FILE_ADAPTER_NAME = "file"
const (
FILE_SLICE_DATE_NULL = ""
FILE_SLICE_DATE_YEAR = "y"
FILE_SLICE_DATE_MONTH = "m"
FILE_SLICE_DATE_DAY = "d"
FILE_SLICE_DATE_HOUR = "h"
)
const (
FILE_ACCESS_LEVEL = 1000
)
// adapter file
type AdapterFile struct {
write map[int]*FileWriter
config *FileConfig
}
// file writer
type FileWriter struct {
lock sync.RWMutex
writer *os.File
startLine int64
startTime int64
filename string
}
func NewFileWrite(fn string) *FileWriter {
return &FileWriter{
filename: fn,
}
}
// file config
type FileConfig struct {
// log filename
Filename string
// level log filename
LevelFileName map[int]string
// max file size
MaxSize int64
// max file line
MaxLine int64
// file slice by date
// "y" Log files are cut through year
// "m" Log files are cut through mouth
// "d" Log files are cut through day
// "h" Log files are cut through hour
DateSlice string
// is json format
JsonFormat bool
// jsonFormat is false, please input format string
// if format is empty, default format "%millisecond_format% [%level_string%] %body%"
//
// Timestamp "%timestamp%"
// TimestampFormat "%timestamp_format%"
// Millisecond "%millisecond%"
// MillisecondFormat "%millisecond_format%"
// Level int "%level%"
// LevelString "%level_string%"
// Body string "%body%"
// File string "%file%"
// Line int "%line%"
// Function "%function%"
//
// example: format = "%millisecond_format% [%level_string%] %body%"
Format string
}
func (fc *FileConfig) Name() string {
return FILE_ADAPTER_NAME
}
var fileSliceDateMapping = map[string]int{
FILE_SLICE_DATE_YEAR: 0,
FILE_SLICE_DATE_MONTH: 1,
FILE_SLICE_DATE_DAY: 2,
FILE_SLICE_DATE_HOUR: 3,
}
func NewAdapterFile() LoggerAbstract {
return &AdapterFile{
write: map[int]*FileWriter{},
config: &FileConfig{},
}
}
// init
func (adapterFile *AdapterFile) Init(fileConfig Config) error {
if fileConfig.Name() != FILE_ADAPTER_NAME {
return errors.New("logger file adapter init error, config must FileConfig")
}
vc := reflect.ValueOf(fileConfig)
fc := vc.Interface().(*FileConfig)
adapterFile.config = fc
if fc.JsonFormat == false && fc.Format == "" {
fc.Format = defaultLoggerMessageFormat
}
if len(adapterFile.config.LevelFileName) == 0 {
if adapterFile.config.Filename == "" {
return errors.New("config Filename can't be empty!")
}
}
_, ok := fileSliceDateMapping[adapterFile.config.DateSlice]
if !ok {
return errors.New("config DateSlice must be one of the 'y', 'd', 'm','h'!")
}
// init FileWriter
if len(adapterFile.config.LevelFileName) > 0 {
fileWriters := map[int]*FileWriter{}
for level, filename := range adapterFile.config.LevelFileName {
_, ok := levelStringMapping[level]
if !ok {
return errors.New("config LevelFileName key level is illegal!")
}
fw := NewFileWrite(filename)
fw.initFile()
fileWriters[level] = fw
}
adapterFile.write = fileWriters
}
if adapterFile.config.Filename != "" {
fw := NewFileWrite(adapterFile.config.Filename)
fw.initFile()
adapterFile.write[FILE_ACCESS_LEVEL] = fw
}
return nil
}
// Write
func (adapterFile *AdapterFile) Write(loggerMsg *loggerMessage) error {
var accessChan = make(chan error, 1)
var levelChan = make(chan error, 1)
// access file write
if adapterFile.config.Filename != "" {
go func() {
accessFileWrite, ok := adapterFile.write[FILE_ACCESS_LEVEL]
if !ok {
accessChan <- nil
return
}
err := accessFileWrite.writeByConfig(adapterFile.config, loggerMsg)
if err != nil {
accessChan <- err
return
}
accessChan <- nil
}()
}
// level file write
if len(adapterFile.config.LevelFileName) != 0 {
go func() {
fileWrite, ok := adapterFile.write[loggerMsg.Level]
if !ok {
levelChan <- nil
return
}
err := fileWrite.writeByConfig(adapterFile.config, loggerMsg)
if err != nil {
levelChan <- err
return
}
levelChan <- nil
}()
}
var accessErr error
var levelErr error
if adapterFile.config.Filename != "" {
accessErr = <-accessChan
}
if len(adapterFile.config.LevelFileName) != 0 {
levelErr = <-levelChan
}
if accessErr != nil {
return accessErr.(error)
}
if levelErr != nil {
return levelErr.(error)
}
return nil
}
// Flush
func (adapterFile *AdapterFile) Flush() {
for _, fileWrite := range adapterFile.write {
fileWrite.writer.Close()
}
}
// Name
func (adapterFile *AdapterFile) Name() string {
return FILE_ADAPTER_NAME
}
// init file
func (fw *FileWriter) initFile() error {
//check file exits, otherwise create a file
ok, _ := utils.UtilFile.PathExists(fw.filename)
if ok == false {
err := utils.UtilFile.CreateFile(fw.filename)
if err != nil {
return err
}
}
// get start time
fw.startTime = time.Now().Unix()
// get file start lines
nowLines, err := utils.UtilFile.GetFileLines(fw.filename)
if err != nil {
return err
}
fw.startLine = nowLines
//get a file pointer
file, err := fw.getFileObject(fw.filename)
if err != nil {
return err
}
fw.writer = file
return nil
}
// write by config
func (fw *FileWriter) writeByConfig(config *FileConfig, loggerMsg *loggerMessage) error {
fw.lock.Lock()
defer fw.lock.Unlock()
if config.DateSlice != "" {
// file slice by date
err := fw.sliceByDate(config.DateSlice)
if err != nil {
return err
}
}
if config.MaxLine != 0 {
// file slice by line
err := fw.sliceByFileLines(config.MaxLine)
if err != nil {
return err
}
}
if config.MaxSize != 0 {
// file slice by size
err := fw.sliceByFileSize(config.MaxSize)
if err != nil {
return err
}
}
msg := ""
if config.JsonFormat == true {
//jsonByte, _ := json.Marshal(loggerMsg)
jsonByte, _ := loggerMsg.MarshalJSON()
msg = string(jsonByte) + "\r\n"
} else {
msg = loggerMessageFormat(config.Format, loggerMsg) + "\r\n"
}
fw.writer.Write([]byte(msg))
if config.MaxLine != 0 {
if config.JsonFormat == true {
fw.startLine += 1
} else {
fw.startLine += int64(strings.Count(msg, "\n"))
}
}
return nil
}
//slice file by date (y, m, d, h, i, s), rename file is file_time.log and recreate file
func (fw *FileWriter) sliceByDate(dataSlice string) error {
filename := fw.filename
filenameSuffix := path.Ext(filename)
startTime := time.Unix(fw.startTime, 0)
nowTime := time.Now()
oldFilename := ""
isHaveSlice := false
if (dataSlice == FILE_SLICE_DATE_YEAR) &&
(startTime.Year() != nowTime.Year()) {
isHaveSlice = true
oldFilename = strings.Replace(filename, filenameSuffix, "", 1) + "_" + startTime.Format("2006") + filenameSuffix
}
if (dataSlice == FILE_SLICE_DATE_MONTH) &&
(startTime.Format("200601") != nowTime.Format("200601")) {
isHaveSlice = true
oldFilename = strings.Replace(filename, filenameSuffix, "", 1) + "_" + startTime.Format("200601") + filenameSuffix
}
if (dataSlice == FILE_SLICE_DATE_DAY) &&
(startTime.Format("20060102") != nowTime.Format("20060102")) {
isHaveSlice = true
oldFilename = strings.Replace(filename, filenameSuffix, "", 1) + "_" + startTime.Format("20060102") + filenameSuffix
}
if (dataSlice == FILE_SLICE_DATE_HOUR) &&
(startTime.Format("2006010215") != startTime.Format("2006010215")) {
isHaveSlice = true
oldFilename = strings.Replace(filename, filenameSuffix, "", 1) + "_" + startTime.Format("2006010215") + filenameSuffix
}
if isHaveSlice == true {
//close file handle
fw.writer.Close()
err := os.Rename(fw.filename, oldFilename)
if err != nil {
return err
}
err = fw.initFile()
if err != nil {
return err
}
}
return nil
}
//slice file by line, if maxLine < fileLine, rename file is file_line_maxLine_time.log and recreate file
func (fw *FileWriter) sliceByFileLines(maxLine int64) error {
filename := fw.filename
filenameSuffix := path.Ext(filename)
startLine := fw.startLine
if startLine >= maxLine {
//close file handle
fw.writer.Close()
timeFlag := time.Now().Format("2006-01-02-15.04.05.9999")
oldFilename := strings.Replace(filename, filenameSuffix, "", 1) + "." + timeFlag + filenameSuffix
err := os.Rename(filename, oldFilename)
if err != nil {
return err
}
err = fw.initFile()
if err != nil {
return err
}
}
return nil
}
//slice file by size, if maxSize < fileSize, rename file is file_size_maxSize_time.log and recreate file
func (fw *FileWriter) sliceByFileSize(maxSize int64) error {
filename := fw.filename
filenameSuffix := path.Ext(filename)
nowSize, _ := fw.getFileSize(filename)
if nowSize >= maxSize {
//close file handle
fw.writer.Close()
timeFlag := time.Now().Format("2006-01-02-15.04.05.9999")
oldFilename := strings.Replace(filename, filenameSuffix, "", 1) + "." + timeFlag + filenameSuffix
err := os.Rename(filename, oldFilename)
if err != nil {
return err
}
err = fw.initFile()
if err != nil {
return err
}
}
return nil
}
//get file object
//params : filename
//return : *os.file, error
func (fw *FileWriter) getFileObject(filename string) (file *os.File, err error) {
file, err = os.OpenFile(filename, os.O_RDWR|os.O_APPEND, 0766)
return file, err
}
//get file size
//params : filename
//return : fileSize(byte int64), error
func (fw *FileWriter) getFileSize(filename string) (fileSize int64, err error) {
fileInfo, err := os.Stat(filename)
if err != nil {
return fileSize, err
}
return fileInfo.Size() / 1024, nil
}
func init() {
Register(FILE_ADAPTER_NAME, NewAdapterFile)
}