mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
add file configuration support for logger in ghttp.Server
This commit is contained in:
@ -102,9 +102,9 @@ func (l *Logger) print(ctx context.Context, level int, values ...interface{}) {
|
||||
if p.parent != nil {
|
||||
p = p.parent
|
||||
}
|
||||
if !p.init.Val() && p.init.Cas(false, true) {
|
||||
// It just initializes once for each logger.
|
||||
if p.config.RotateSize > 0 || p.config.RotateExpire > 0 {
|
||||
// It just initializes once for each logger.
|
||||
if p.config.RotateSize > 0 || p.config.RotateExpire > 0 {
|
||||
if !p.init.Val() && p.init.Cas(false, true) {
|
||||
gtimer.AddOnce(p.config.RotateCheckInterval, p.rotateChecksTimely)
|
||||
intlog.Printf("logger rotation initialized: every %s", p.config.RotateCheckInterval.String())
|
||||
}
|
||||
|
||||
@ -19,6 +19,10 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
memoryLockPrefixForRotating = "glog.rotateChecksTimely:"
|
||||
)
|
||||
|
||||
// rotateFileBySize rotates the current logging file according to the
|
||||
// configured rotation size.
|
||||
func (l *Logger) rotateFileBySize(now time.Time) {
|
||||
@ -91,6 +95,7 @@ func (l *Logger) doRotateFile(filePath string) error {
|
||||
// rotateChecksTimely timely checks the backups expiration and the compression.
|
||||
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(
|
||||
@ -101,17 +106,20 @@ func (l *Logger) rotateChecksTimely() {
|
||||
}
|
||||
|
||||
// It here uses memory lock to guarantee the concurrent safety.
|
||||
memoryLockKey := "glog.rotateChecksTimely:" + l.config.Path
|
||||
memoryLockKey := memoryLockPrefixForRotating + l.config.Path
|
||||
if !gmlock.TryLock(memoryLockKey) {
|
||||
return
|
||||
}
|
||||
defer gmlock.Unlock(memoryLockKey)
|
||||
|
||||
var (
|
||||
now = time.Now()
|
||||
pattern = "*.log, *.gz"
|
||||
files, _ = gfile.ScanDirFile(l.config.Path, pattern, true)
|
||||
now = time.Now()
|
||||
pattern = "*.log, *.gz"
|
||||
files, err = gfile.ScanDirFile(l.config.Path, pattern, true)
|
||||
)
|
||||
if err != nil {
|
||||
intlog.Error(err)
|
||||
}
|
||||
intlog.Printf("logging rotation start checks: %+v", files)
|
||||
// =============================================================
|
||||
// Rotation of expired file checks.
|
||||
@ -141,7 +149,10 @@ func (l *Logger) rotateChecksTimely() {
|
||||
}
|
||||
if expireRotated {
|
||||
// Update the files array.
|
||||
files, _ = gfile.ScanDirFile(l.config.Path, pattern, true)
|
||||
files, err = gfile.ScanDirFile(l.config.Path, pattern, true)
|
||||
if err != nil {
|
||||
intlog.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -175,7 +186,10 @@ func (l *Logger) rotateChecksTimely() {
|
||||
return true
|
||||
})
|
||||
// Update the files array.
|
||||
files, _ = gfile.ScanDirFile(l.config.Path, pattern, true)
|
||||
files, err = gfile.ScanDirFile(l.config.Path, pattern, true)
|
||||
if err != nil {
|
||||
intlog.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -192,10 +206,12 @@ func (l *Logger) rotateChecksTimely() {
|
||||
if backupFilesMap[originalLoggingFilePath] == nil {
|
||||
backupFilesMap[originalLoggingFilePath] = garray.NewSortedArray(func(a, b interface{}) int {
|
||||
// Sorted by rotated/backup file mtime.
|
||||
// The old rotated/backup file is put in the head of array.
|
||||
file1 := a.(string)
|
||||
file2 := b.(string)
|
||||
result := gfile.MTimestampMilli(file1) - gfile.MTimestampMilli(file2)
|
||||
// The older rotated/backup file is put in the head of array.
|
||||
var (
|
||||
file1 = a.(string)
|
||||
file2 = b.(string)
|
||||
result = gfile.MTimestampMilli(file1) - gfile.MTimestampMilli(file2)
|
||||
)
|
||||
if result <= 0 {
|
||||
return -1
|
||||
}
|
||||
@ -214,11 +230,11 @@ func (l *Logger) rotateChecksTimely() {
|
||||
path, _ := array.PopLeft()
|
||||
intlog.Printf(`remove exceeded backup limit file: %s`, path)
|
||||
if err := gfile.Remove(path.(string)); err != nil {
|
||||
intlog.Print(err)
|
||||
intlog.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Backup expiration checks.
|
||||
// Backups expiration checking.
|
||||
if l.config.RotateBackupExpire > 0 {
|
||||
var (
|
||||
mtime time.Time
|
||||
@ -235,7 +251,7 @@ func (l *Logger) rotateChecksTimely() {
|
||||
now, mtime, subDuration, l.config.RotateBackupExpire, path,
|
||||
)
|
||||
if err := gfile.Remove(path); err != nil {
|
||||
intlog.Print(err)
|
||||
intlog.Error(err)
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user