@@ -18,10 +18,10 @@ package zlog
18
18
import (
19
19
"bytes"
20
20
"fmt"
21
- "io "
21
+ "github.com/aceld/zinx/zutils "
22
22
"os"
23
+ "path/filepath"
23
24
"runtime"
24
- "strings"
25
25
"sync"
26
26
"time"
27
27
)
@@ -75,17 +75,9 @@ type ZinxLoggerCore struct {
75
75
// log tag bit (日志标记位)
76
76
flag int
77
77
78
- // the file descriptor for log output
79
- // (日志输出的文件描述符)
80
- out io.Writer
81
-
82
78
// the output buffer (输出的缓冲区)
83
79
buf bytes.Buffer
84
80
85
- // the output file currently bound to the log
86
- // (当前日志绑定的输出文件)
87
- file * os.File
88
-
89
81
// log isolation level
90
82
// (日志隔离级别)
91
83
isolationLevel int
@@ -94,21 +86,7 @@ type ZinxLoggerCore struct {
94
86
// (获取日志文件名和代码上述的runtime.Call 的函数调用层数)
95
87
calldDepth int
96
88
97
- // log file name
98
- // (日志文件名称)
99
- fileName string
100
-
101
- // log file directory
102
- // (日志文件目录)
103
- fileDir string
104
-
105
- // last write date
106
- // (上次写入日期)
107
- lastWriteDate int
108
-
109
- // file swap lock
110
- // (文件交换锁)
111
- fsLock sync.Mutex
89
+ fw * zutils.Writer
112
90
113
91
onLogHook func ([]byte )
114
92
}
@@ -120,11 +98,11 @@ out: The file io for standard output
120
98
prefix: The prefix of the log
121
99
flag: The flag of the log header information
122
100
*/
123
- func NewZinxLog (out io. Writer , prefix string , flag int ) * ZinxLoggerCore {
101
+ func NewZinxLog (prefix string , flag int ) * ZinxLoggerCore {
124
102
125
103
// By default, debug is turned on, the depth is 2, and the ZinxLogger object calling the log print method can call up to two levels to reach the output function
126
104
// (默认 debug打开, calledDepth深度为2,ZinxLogger对象调用日志打印方法最多调用两层到达output函数)
127
- zlog := & ZinxLoggerCore {out : out , prefix : prefix , flag : flag , file : nil , isolationLevel : 0 , calldDepth : 2 }
105
+ zlog := & ZinxLoggerCore {prefix : prefix , flag : flag , isolationLevel : 0 , calldDepth : 2 }
128
106
129
107
// Set the log object's resource cleanup destructor method (this is not necessary, as go's Gc will automatically collect, but for the sake of neatness)
130
108
// (设置log对象 回收资源 析构方法(不设置也可以,go的Gc会自动回收,强迫症没办法))
@@ -245,23 +223,19 @@ func (log *ZinxLoggerCore) OutPut(level int, s string) error {
245
223
log .buf .WriteByte ('\n' )
246
224
}
247
225
248
- log .updateOutputFile ()
249
-
250
226
var err error
251
- if log .file == nil {
227
+ if log .fw == nil {
252
228
// if log file is not set, output to console
253
229
_ , _ = os .Stderr .Write (log .buf .Bytes ())
254
230
} else {
255
231
// write the filled buffer to IO output
256
- _ , err = log .out .Write (log .buf .Bytes ())
232
+ _ , err = log .fw .Write (log .buf .Bytes ())
257
233
}
258
234
259
235
if log .onLogHook != nil {
260
236
log .onLogHook (log .buf .Bytes ())
261
237
}
262
-
263
238
return err
264
-
265
239
}
266
240
267
241
func (log * ZinxLoggerCore ) verifyLogIsolation (logLevel int ) bool {
@@ -407,66 +381,48 @@ func (log *ZinxLoggerCore) SetPrefix(prefix string) {
407
381
// SetLogFile sets the log file output
408
382
// (设置日志文件输出)
409
383
func (log * ZinxLoggerCore ) SetLogFile (fileDir string , fileName string ) {
410
- log .fileDir = fileDir
411
- log .fileName = fileName
412
- }
413
-
414
- // Close the file associated with the log
415
- // (关闭日志绑定的文件)
416
- func (log * ZinxLoggerCore ) closeFile () {
417
- if log .file != nil {
418
- _ = log .file .Close ()
419
- log .file = nil
420
- log .out = os .Stderr
384
+ if log .fw != nil {
385
+ log .fw .Close ()
421
386
}
387
+ log .fw = zutils .New (filepath .Join (fileDir , fileName ))
422
388
}
423
389
424
- // update the output file for the log
425
- // (更新文件输出)
426
- func (log * ZinxLoggerCore ) updateOutputFile () {
427
-
428
- var file * os.File
429
-
430
- yearDay := time .Now ().YearDay ()
431
-
432
- if log .lastWriteDate == yearDay && log .file != nil {
390
+ // SetMaxAge 最大保留天数
391
+ func (log * ZinxLoggerCore ) SetMaxAge (ma int ) {
392
+ if log .fw == nil {
433
393
return
434
394
}
395
+ log .mu .Lock ()
396
+ defer log .mu .Unlock ()
397
+ log .fw .SetMaxAge (ma )
398
+ }
435
399
436
- log .fsLock .Lock ()
437
- defer log .fsLock .Unlock ()
438
-
439
- if log .lastWriteDate == yearDay && log .file != nil {
400
+ // SetMaxSize 单个日志最大容量 单位:字节
401
+ func (log * ZinxLoggerCore ) SetMaxSize (ms int64 ) {
402
+ if log .fw == nil {
440
403
return
441
404
}
405
+ log .mu .Lock ()
406
+ defer log .mu .Unlock ()
407
+ log .fw .SetMaxSize (ms )
408
+ }
442
409
443
- log .lastWriteDate = yearDay
444
-
445
- // create the log directory
446
- _ = mkdirLog (log .fileDir )
447
-
448
- // define the log file name = log file name . date suffix . log
449
- // supported file name formats:
450
- // 1. "zinx.log"
451
- // 2. "zinx"
452
- // 3. "zinx.zinx.zinx.log"
453
- realFileNameSlice := strings .Split (log .fileName , ".log" )
454
- realFileName := realFileNameSlice [0 ]
455
- newDailyFile := log .fileDir + "/" + realFileName + "." + time .Now ().Format ("20060102" ) + ".log"
456
-
457
- if log .checkFileExist (newDailyFile ) {
458
- file , _ = os .OpenFile (newDailyFile , os .O_APPEND | os .O_RDWR , 0644 ) // rw-r--r--
459
- } else {
460
- file , _ = os .OpenFile (newDailyFile , os .O_CREATE | os .O_RDWR | os .O_APPEND , 0644 )
410
+ // SetCons 同时输出控制台
411
+ func (log * ZinxLoggerCore ) SetCons (b bool ) {
412
+ if log .fw == nil {
413
+ return
461
414
}
415
+ log .mu .Lock ()
416
+ defer log .mu .Unlock ()
417
+ log .fw .SetCons (b )
418
+ }
462
419
463
- if log .file != nil {
464
- log .closeFile ()
420
+ // Close the file associated with the log
421
+ // (关闭日志绑定的文件)
422
+ func (log * ZinxLoggerCore ) closeFile () {
423
+ if log .fw != nil {
424
+ log .fw .Close ()
465
425
}
466
-
467
- log .file = file
468
- log .out = file
469
-
470
426
}
471
427
472
428
func (log * ZinxLoggerCore ) SetLogLevel (logLevel int ) {
@@ -481,19 +437,6 @@ func (log *ZinxLoggerCore) checkFileExist(filename string) bool {
481
437
return exist
482
438
}
483
439
484
- func mkdirLog (dir string ) (e error ) {
485
- _ , er := os .Stat (dir )
486
- b := er == nil || os .IsExist (er )
487
- if ! b {
488
- if err := os .MkdirAll (dir , 0775 ); err != nil {
489
- if os .IsPermission (err ) {
490
- e = err
491
- }
492
- }
493
- }
494
- return
495
- }
496
-
497
440
// Convert an integer to a fixed-length string, where the width of the string should be greater than 0
498
441
// Ensure that the buffer has sufficient capacity
499
442
// (将一个整形转换成一个固定长度的字符串,字符串宽度应该是大于0的
0 commit comments