Golang日志轮询

本文探讨了lestrrat-go/file-rotatelogs存在的问题,如日志输出停止、文件名模糊和时间轮转不准确等。推荐使用iproj/file-rotatelogs替代,它修复了这些问题并已在大规模线上环境中稳定运行。结合logrus,可以实现Golang日志管理的最佳实践。同时,建议设置WithRotationTime(time.Second)以避免日志输出卡住的情况。
摘要由CSDN通过智能技术生成

背景

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值