作者:mobiledu2502855037 | 来源:互联网 | 2023-08-01 16:48
“–————人与人之间还是直接点好——————”
**为go的log 增加轮询的功能**
使日志自动 保存,清理 实现轮询的机制!使你的系统日志不会再涨大 导致资源匮乏 崩溃 ·
包地址: https://github.com/gggwvg/logrotate
具体参数信息如程序所示!
package main
import (
"time"
"github.com/gggwvg/logrotate"
"github.com/sirupsen/logrus"
)
func main() {
opts := []logrotate.Option{
logrotate.File("/var/log/agent-mqtt-cli.log"),
}
opts = append(opts, logrotate.MaxArchives(2))
opts = append(opts, logrotate.RotateSize("5m"))
logger, err := logrotate.NewLogger(opts...)
if err != nil {
panic(err)
}
logrus.SetOutput(logger)
logrus.Println("rotate by daily and file size 100m")
f := func() {
for i := 0; i < 100; i {
logrus.WithFields(logrus.Fields{
"animal": "walrus",
"number": i,
}).Info("A walrus appears")
logrus.Error("xxxxxxxxxxxxxxx")
logrus.Error("xxxxxxxxxxxxxxx2")
logrus.Error("xxxxxxxxxxxxxxx3")
time.Sleep(time.Second)
}
}
for i := 0; i < 1000000; i {
go f()
time.Sleep(time.Second)
}
f()
time.Sleep(10 * time.Second)
logger.Close()
}