背景
lestrrat-go/file-rotatelogs 项目已经归档了,但是其自身存在一些缺陷。
https://github.com/iproj/file-rotatelogs 对下述问题进行了修复,并投入大规模线上使用,运行平稳
iproj/file-rotatelogs + logrus 可以作为入门golang 日志使用的最佳实践
lestrrat-go/file-rotatelogs 缺陷
No longer output log, when too much log output rotated by size
You can use “log” or “logrus” test
package main
import (
"fmt"
"log"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
)
func main() {
rl, _ := rotatelogs.New(
"/tmp/logs/access_log.%Y%m%d%H%M",
rotatelogs.WithLinkName("/tmp/logs/access_log"),
//rotatelogs.WithMaxAge(24*time.Hour),
rotatelogs.WithRotationCount(3),
//rotatelogs.WithRotationTime(time.Hour),
rotatelogs.WithRotationSize(12),
)
log.SetOutput(rl)
for i := 0; i < 1000; i++ {
fmt.Printf("Console print %d\n", i)
log.Printf("Test content %d\n", i)
time.Sleep(time.Second)
}
log.Printf("Ending pose!\n")
}
The log output is stuck in the ninth, expected to be the 1000th
ls -l /tmp/logs/
total 24
lrwxr-xr-x 1 kane wheel 26 8 22 15:33 access_log -> access_log.202108220000.10
-rw-r--r-- 1 kane wheel 35 8 22 15:33 access_log.202108220000.7
-rw-r--r-- 1 k