gookit / slog

📑 Lightweight, configurable, extensible logging library written in Go. Support multi level, multi outputs and built-in multi file logger, buffers, clean, rotate-file handling.一个易于使用的,轻量级、可配置、可扩展的日志库。支持多个级别,输出到多文件;内置文件日志处理、自动切割、清理、压缩等增强功能

Home Page:https://pkg.go.dev/github.com/gookit/slog

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

slog 运行时不会限制备份数量和压缩备份文件

ly020044 opened this issue · comments

System (please complete the following information):

  • OS: macOS
  • GO Version: 1.21
  • Pkg Version: 0.5.3

Describe the bug

运行时 slog 不会根据 BackupNum 参数限制文件备份文件的数量,开启压缩选项后在运行过程中也不会将备份的文件进行压缩。
当重新启动后 slog 才会移除多余的备份文件,及压缩备份文件

To Reproduce

以下程序将每隔 30 秒产生一个备份文件,持续 10 分钟。允许的最大备份数据为 5 并开启压缩功能。

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/gookit/slog"
	"github.com/gookit/slog/handler"
	"github.com/gookit/slog/rotatefile"
)

const pth = "./logs/main.log"

func main() {
	log := slog.New()

	h, err := handler.NewTimeRotateFileHandler(
		pth,
		rotatefile.RotateTime((time.Second * 30).Seconds()),
		handler.WithBuffSize(0),
		handler.WithBackupNum(5),
		handler.WithCompress(true),
	)

	if err != nil {
		panic(err)
	}

	log.AddHandler(h)

	ctx, _ := context.WithTimeout(context.Background(), time.Minute*10)
	go func() {
		for {
			select {
			case <-ctx.Done():

				return
			case <-time.After(time.Second):
				log.Info("Log " + time.Now().String())
			}
		}
	}()

	<-ctx.Done()

	fmt.Println("Exit.")
}

Expected behavior

应该在运行时就限制相应的备份数量及压缩备份文件,而不是在重新启动后。

Screenshots
截屏2023-07-27 09 21 11

commented

感谢报告,我测试一下

commented

有个BUG修复了,可以拉取最新版本试试。

辛苦了,测试可以了!