2020-03-15 19:32:26 +08:00
|
|
|
// Copyright 2020 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package glog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/container/garray"
|
|
|
|
"github.com/gogf/gf/encoding/gcompress"
|
|
|
|
"github.com/gogf/gf/internal/intlog"
|
|
|
|
"github.com/gogf/gf/os/gfile"
|
|
|
|
"github.com/gogf/gf/os/gtime"
|
|
|
|
"github.com/gogf/gf/os/gtimer"
|
|
|
|
"github.com/gogf/gf/text/gregex"
|
2020-03-24 19:48:10 +08:00
|
|
|
"time"
|
2020-03-15 19:32:26 +08:00
|
|
|
)
|
|
|
|
|
2020-03-25 23:36:56 +08:00
|
|
|
// rotateFileBySize rotates the current logging file according to the
|
|
|
|
// configured rotation size.
|
|
|
|
func (l *Logger) rotateFileBySize(now time.Time) {
|
2020-03-24 19:48:10 +08:00
|
|
|
if l.config.RotateSize <= 0 {
|
2020-03-15 19:32:26 +08:00
|
|
|
return
|
|
|
|
}
|
2020-03-25 00:03:52 +08:00
|
|
|
l.rmu.Lock()
|
|
|
|
defer l.rmu.Unlock()
|
2020-03-25 23:36:56 +08:00
|
|
|
if err := l.doRotateFile(l.getFilePath(now)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 09:29:36 +08:00
|
|
|
// doRotateFile rotates the given logging file.
|
2020-03-25 23:36:56 +08:00
|
|
|
func (l *Logger) doRotateFile(filePath string) error {
|
2020-03-15 19:32:26 +08:00
|
|
|
// No backups, it then just removes the current logging file.
|
2020-03-26 20:58:57 +08:00
|
|
|
if l.config.RotateBackupLimit == 0 {
|
2020-03-15 19:32:26 +08:00
|
|
|
if err := gfile.Remove(filePath); err != nil {
|
2020-03-25 23:36:56 +08:00
|
|
|
return err
|
2020-03-15 19:32:26 +08:00
|
|
|
}
|
|
|
|
intlog.Printf(`%d size exceeds, no backups set, remove original logging file: %s`, l.config.RotateSize, filePath)
|
2020-03-25 23:36:56 +08:00
|
|
|
return nil
|
2020-03-15 19:32:26 +08:00
|
|
|
}
|
|
|
|
// Else it creates new backup files.
|
|
|
|
var (
|
|
|
|
dirPath = gfile.Dir(filePath)
|
|
|
|
fileName = gfile.Name(filePath)
|
2020-03-26 20:58:57 +08:00
|
|
|
fileExtName = gfile.ExtName(filePath)
|
2020-03-15 19:32:26 +08:00
|
|
|
newFilePath = ""
|
|
|
|
)
|
2020-03-26 20:58:57 +08:00
|
|
|
// Rename the logging file by adding extra datetime information to microseconds, like:
|
|
|
|
// access.log -> access.20200326101301899002.log
|
2020-03-26 23:48:21 +08:00
|
|
|
// access.20200326.log -> access.20200326.20200326101301899002.log
|
2020-03-26 20:58:57 +08:00
|
|
|
for {
|
|
|
|
var (
|
|
|
|
now = gtime.Now()
|
|
|
|
micro = now.Microsecond() % 1000
|
|
|
|
)
|
|
|
|
for micro < 100 {
|
|
|
|
micro *= 10
|
|
|
|
}
|
|
|
|
newFilePath = gfile.Join(
|
|
|
|
dirPath,
|
|
|
|
fmt.Sprintf(
|
|
|
|
`%s.%s%d.%s`,
|
|
|
|
fileName, now.Format("YmdHisu"), micro, fileExtName,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if !gfile.Exists(newFilePath) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-03-15 19:32:26 +08:00
|
|
|
if err := gfile.Rename(filePath, newFilePath); err != nil {
|
2020-03-25 23:36:56 +08:00
|
|
|
return err
|
2020-03-15 19:32:26 +08:00
|
|
|
}
|
2020-03-25 23:36:56 +08:00
|
|
|
return nil
|
2020-03-15 19:32:26 +08:00
|
|
|
}
|
|
|
|
|
2020-03-25 23:36:56 +08:00
|
|
|
// rotateChecksTimely timely checks the backups expiration and the compression.
|
|
|
|
func (l *Logger) rotateChecksTimely() {
|
2020-03-26 20:58:57 +08:00
|
|
|
defer gtimer.AddOnce(l.config.RotateCheckInterval, l.rotateChecksTimely)
|
2020-03-15 19:32:26 +08:00
|
|
|
// Checks whether file rotation not enabled.
|
2020-03-25 23:36:56 +08:00
|
|
|
if l.config.RotateSize <= 0 && l.config.RotateExpire == 0 {
|
2020-03-15 19:32:26 +08:00
|
|
|
return
|
|
|
|
}
|
2020-03-25 23:36:56 +08:00
|
|
|
var (
|
|
|
|
now = time.Now()
|
|
|
|
pattern = "*.log, *.gz"
|
|
|
|
files, _ = gfile.ScanDirFile(l.config.Path, pattern, true)
|
|
|
|
)
|
2020-03-15 19:32:26 +08:00
|
|
|
intlog.Printf("logging rotation start checks: %+v", files)
|
2020-03-25 23:36:56 +08:00
|
|
|
// =============================================================
|
|
|
|
// Rotation expire file checks.
|
|
|
|
// =============================================================
|
|
|
|
if l.config.RotateExpire > 0 {
|
2020-03-26 09:29:36 +08:00
|
|
|
var (
|
|
|
|
mtime time.Time
|
|
|
|
subDuration time.Duration
|
|
|
|
expireRotated bool
|
|
|
|
)
|
2020-03-25 23:36:56 +08:00
|
|
|
for _, file := range files {
|
|
|
|
if gfile.ExtName(file) == "gz" {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-26 09:29:36 +08:00
|
|
|
mtime = gfile.MTime(file)
|
|
|
|
subDuration = now.Sub(mtime)
|
|
|
|
if subDuration > l.config.RotateExpire {
|
|
|
|
expireRotated = true
|
|
|
|
intlog.Printf(
|
|
|
|
`%v - %v = %v > %v, rotation expire logging file: %s`,
|
|
|
|
now, mtime, subDuration, l.config.RotateExpire, file,
|
|
|
|
)
|
|
|
|
if err := l.doRotateFile(file); err != nil {
|
2020-03-25 23:36:56 +08:00
|
|
|
intlog.Error(err)
|
|
|
|
}
|
2020-03-26 09:29:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if expireRotated {
|
2020-03-25 23:36:56 +08:00
|
|
|
// Update the files array.
|
|
|
|
files, _ = gfile.ScanDirFile(l.config.Path, pattern, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// =============================================================
|
|
|
|
// Rotated file compression.
|
|
|
|
// =============================================================
|
2020-03-15 19:32:26 +08:00
|
|
|
needCompressFileArray := garray.NewStrArray()
|
2020-03-26 20:58:57 +08:00
|
|
|
if l.config.RotateBackupCompress > 0 {
|
2020-03-15 19:32:26 +08:00
|
|
|
for _, file := range files {
|
2020-03-26 20:58:57 +08:00
|
|
|
// Eg: access.20200326101301899002.log.gz
|
2020-03-15 19:32:26 +08:00
|
|
|
if gfile.ExtName(file) == "gz" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Eg:
|
2020-03-26 20:58:57 +08:00
|
|
|
// access.20200326101301899002.log
|
|
|
|
if gregex.IsMatchString(`.+\.\d{20}\.log`, gfile.Basename(file)) {
|
2020-03-15 19:32:26 +08:00
|
|
|
needCompressFileArray.Append(file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if needCompressFileArray.Len() > 0 {
|
|
|
|
needCompressFileArray.Iterator(func(_ int, path string) bool {
|
|
|
|
err := gcompress.GzipFile(path, path+".gz")
|
|
|
|
if err == nil {
|
|
|
|
intlog.Printf(`compressed done, remove original logging file: %s`, path)
|
|
|
|
if err = gfile.Remove(path); err != nil {
|
|
|
|
intlog.Print(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
intlog.Print(err)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
// Update the files array.
|
2020-03-25 23:36:56 +08:00
|
|
|
files, _ = gfile.ScanDirFile(l.config.Path, pattern, true)
|
2020-03-15 19:32:26 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-25 23:36:56 +08:00
|
|
|
|
|
|
|
// =============================================================
|
2020-03-15 19:32:26 +08:00
|
|
|
// Backups count limit and expiration checks.
|
2020-03-25 23:36:56 +08:00
|
|
|
// =============================================================
|
2020-03-15 19:32:26 +08:00
|
|
|
var (
|
|
|
|
backupFilesMap = make(map[string]*garray.SortedArray)
|
|
|
|
originalLoggingFilePath = ""
|
|
|
|
)
|
2020-03-26 20:58:57 +08:00
|
|
|
if l.config.RotateBackupLimit > 0 || l.config.RotateBackupExpire > 0 {
|
2020-03-15 19:32:26 +08:00
|
|
|
for _, file := range files {
|
2020-03-26 20:58:57 +08:00
|
|
|
originalLoggingFilePath, _ = gregex.ReplaceString(`\.\d{20}`, "", file)
|
2020-03-15 19:32:26 +08:00
|
|
|
if backupFilesMap[originalLoggingFilePath] == nil {
|
|
|
|
backupFilesMap[originalLoggingFilePath] = garray.NewSortedArray(func(a, b interface{}) int {
|
2020-03-26 09:29:36 +08:00
|
|
|
// Sorted by rotated/backup file mtime.
|
|
|
|
// The old rotated/backup file is put in the head of array.
|
2020-03-15 19:32:26 +08:00
|
|
|
file1 := a.(string)
|
|
|
|
file2 := b.(string)
|
2020-03-26 09:29:36 +08:00
|
|
|
result := gfile.MTimestampMilli(file1) - gfile.MTimestampMilli(file2)
|
2020-03-15 19:32:26 +08:00
|
|
|
if result <= 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
})
|
|
|
|
}
|
2020-03-26 09:29:36 +08:00
|
|
|
// Check if this file a rotated/backup file.
|
2020-03-26 20:58:57 +08:00
|
|
|
if gregex.IsMatchString(`.+\.\d{20}\.log`, gfile.Basename(file)) {
|
2020-03-15 19:32:26 +08:00
|
|
|
backupFilesMap[originalLoggingFilePath].Add(file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
intlog.Printf(`calculated backup files map: %+v`, backupFilesMap)
|
|
|
|
for _, array := range backupFilesMap {
|
2020-03-26 20:58:57 +08:00
|
|
|
diff := array.Len() - l.config.RotateBackupLimit
|
2020-03-25 23:36:56 +08:00
|
|
|
for i := 0; i < diff; i++ {
|
2020-03-15 19:32:26 +08:00
|
|
|
path := array.PopLeft().(string)
|
2020-03-26 20:58:57 +08:00
|
|
|
intlog.Printf(`remove exceeded backup limit file: %s`, path)
|
2020-03-15 19:32:26 +08:00
|
|
|
if err := gfile.Remove(path); err != nil {
|
|
|
|
intlog.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 09:29:36 +08:00
|
|
|
// Backup expiration checks.
|
2020-03-26 20:58:57 +08:00
|
|
|
if l.config.RotateBackupExpire > 0 {
|
2020-03-26 09:29:36 +08:00
|
|
|
var (
|
|
|
|
mtime time.Time
|
|
|
|
subDuration time.Duration
|
|
|
|
)
|
2020-03-15 19:32:26 +08:00
|
|
|
for _, array := range backupFilesMap {
|
|
|
|
array.Iterator(func(_ int, v interface{}) bool {
|
|
|
|
path := v.(string)
|
2020-03-26 09:29:36 +08:00
|
|
|
mtime = gfile.MTime(path)
|
|
|
|
subDuration = now.Sub(mtime)
|
2020-03-26 20:58:57 +08:00
|
|
|
if subDuration > l.config.RotateBackupExpire {
|
2020-03-15 19:32:26 +08:00
|
|
|
intlog.Printf(
|
2020-03-26 09:29:36 +08:00
|
|
|
`%v - %v = %v > %v, remove expired backup file: %s`,
|
2020-03-26 20:58:57 +08:00
|
|
|
now, mtime, subDuration, l.config.RotateBackupExpire, path,
|
2020-03-15 19:32:26 +08:00
|
|
|
)
|
|
|
|
if err := gfile.Remove(path); err != nil {
|
|
|
|
intlog.Print(err)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|