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.一个易于使用的,轻量级、可配置、可扩展的日志库。支持多个级别,输出到多文件;内置文件日志处理、自动切割、清理、压缩等增强功能
https://pkg.go.dev/github.com/gookit/slog
MIT License
393 stars 25 forks source link

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

Closed ly020044 closed 1 year ago

ly020044 commented 1 year ago

System (please complete the following information):

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

inhere commented 1 year ago

感谢报告,我测试一下

inhere commented 1 year ago

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

ly020044 commented 1 year ago

辛苦了,测试可以了!