file-rotatelogs包实现日志文件的轮转切割

1.go语言本身的日志系统是很强大的,例如:
"log"
"github.com/sirupsen/logrus"
但,go语言本身并没有日志轮询机制,(就是将日志定期清理,保存,使之不会不停涨大)

2.此处使介绍了一个 file-rotatelogs 包 "github.com/lestrrat-go/file-rotatelogs"
****它可以和标准的日志结合使用****
具体参数,程序描述的够清晰了!
利弊看程序解析!

package main

import (
"log"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/sirupsen/logrus"
)

func main() {

content, err := rotatelogs.New(
"/var/log/cli.log"+"-%Y%m%d%H%M",
rotatelogs.WithLinkName("/var/log/cli.log"), // 生成软链,指向最新日志文件
//MaxAge and RotationCount cannot be both set 两者不能同时设置
rotatelogs.WithMaxAge(6*time.Minute), //clear 最小分钟为单位
//rotatelogs.WithRotationCount(5), //number 默认7份 大于7份 或到了清理时间 开始清理
rotatelogs.WithRotationTime(time.Minute), //rotate 最小为1分钟轮询。默认60s 低于1分钟就按1分钟来
)

if err != nil {
log.Printf("failed to create rotatelogs: %s", err)
return
}

logrus.SetOutput(content)

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(121 * time.Second)
}
————————————————
版权声明:本文为CSDN博主「在线笔记-test01」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jkwanga/article/details/107186653

posted @   技术颜良  阅读(530)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2022-04-16 k8s备份工具之velero
2022-04-16 切片指针跟指针切片的循环注意事项(重要)
2021-04-16 Docker 和 Kubernetes:root 与特权
2021-04-16 DRBD详细解说及配置过程记录
2021-04-16 MySQL 高可用方案-PXC环境部署记录
2021-04-16 MySQL高可用方案
2021-04-16 xtrabackup命令用法实战
点击右上角即可分享
微信分享提示