lumberjack转储klog日志
go.mod
module lumberjack
go 1.19
require (
gopkg.in/natefinch/lumberjack.v2 v2.2.1
k8s.io/klog/v2 v2.100.1
)
require github.com/go-logr/logr v1.2.0 // indirect
main.go
package main
import (
"flag"
"gopkg.in/natefinch/lumberjack.v2"
"k8s.io/klog/v2"
)
func main() {
// 初始化
klog.InitFlags(nil)
flag.Set("logtostderr", "false")
flag.Set("log_file", "test.log")
flag.Parse()
// klog不转储日志,只会默认切割日志,保证在1.8G以内
// klog日志打印到lumberjack,lumberjack写入文件并转储
l := &lumberjack.Logger{
Filename: "test.log",
MaxSize: 1, // mb
MaxBackups: 3,
MaxAge: 28, //days
Compress: true, // disabled by default
}
klog.SetOutput(l)
defer klog.Flush()
for i := 0; i < 100000; i++ {
klog.Infof("hello")
}
}
./main