mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve package glog for rotation feature
This commit is contained in:
@ -97,9 +97,13 @@ func (l *Logger) getFilePath(now time.Time) string {
|
||||
|
||||
// print prints <s> to defined writer, logging file or passed <std>.
|
||||
func (l *Logger) print(std io.Writer, lead string, values ...interface{}) {
|
||||
// Lazy initialize.
|
||||
// Lazy initialize for rotation feature.
|
||||
// It uses atomic reading operation to enhance the checking performance.
|
||||
// It here uses CAP for performance and concurrent safety.
|
||||
if !l.init.Val() && l.init.Cas(false, true) {
|
||||
gtimer.AddOnce(logger.config.RotateCheckInterval, logger.rotateChecksTimely)
|
||||
// It just initializes once for each logger.
|
||||
gtimer.AddOnce(l.config.RotateCheckInterval, l.rotateChecksTimely)
|
||||
intlog.Printf("logger initialized: every %s", l.config.RotateCheckInterval.String())
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
@ -12,6 +12,7 @@ import (
|
||||
"github.com/gogf/gf/encoding/gcompress"
|
||||
"github.com/gogf/gf/internal/intlog"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/os/gmlock"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"github.com/gogf/gf/os/gtimer"
|
||||
"github.com/gogf/gf/text/gregex"
|
||||
@ -81,8 +82,20 @@ func (l *Logger) rotateChecksTimely() {
|
||||
defer gtimer.AddOnce(l.config.RotateCheckInterval, l.rotateChecksTimely)
|
||||
// Checks whether file rotation not enabled.
|
||||
if l.config.RotateSize <= 0 && l.config.RotateExpire == 0 {
|
||||
intlog.Printf(
|
||||
"logging rotation ignore checks: RotateSize: %d, RotateExpire: %s",
|
||||
l.config.RotateSize, l.config.RotateExpire.String(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// It here uses memory lock to guarantee the concurrent safety.
|
||||
lockKey := "glog.rotateChecksTimely:" + l.config.Path
|
||||
if !gmlock.TryLock(lockKey) {
|
||||
return
|
||||
}
|
||||
defer gmlock.Unlock(lockKey)
|
||||
|
||||
var (
|
||||
now = time.Now()
|
||||
pattern = "*.log, *.gz"
|
||||
|
||||
Reference in New Issue
Block a user