diff --git a/os/glog/glog_logger.go b/os/glog/glog_logger.go index 260719acf..6054a3553 100644 --- a/os/glog/glog_logger.go +++ b/os/glog/glog_logger.go @@ -97,9 +97,13 @@ func (l *Logger) getFilePath(now time.Time) string { // print prints to defined writer, logging file or passed . 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 ( diff --git a/os/glog/glog_logger_rotate.go b/os/glog/glog_logger_rotate.go index ac1ca49fc..5524e84be 100644 --- a/os/glog/glog_logger_rotate.go +++ b/os/glog/glog_logger_rotate.go @@ -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"