From c4bfa8c474578671c81f1c77209b793f4162bd08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Henrique=20Guard=C3=A3o=20Gandarez?= Date: Mon, 18 Nov 2024 17:45:57 -0300 Subject: [PATCH] Add log file rotation --- cmd/run.go | 10 ++++++---- go.mod | 1 + go.sum | 2 ++ pkg/log/log.go | 9 ++++++++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index d191316f..12b68a52 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -38,6 +38,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" iniv1 "gopkg.in/ini.v1" + "gopkg.in/natefinch/lumberjack.v2" ) type diagnostics struct { @@ -242,7 +243,7 @@ func SetupLogging(ctx context.Context, v *viper.Viper) (*log.Logger, error) { return nil, fmt.Errorf("failed to load log params: %s", err) } - destOutput := os.Stdout + var destOutput io.Writer = os.Stdout if !params.ToStdout { dir := filepath.Dir(params.File) @@ -253,9 +254,10 @@ func SetupLogging(ctx context.Context, v *viper.Viper) (*log.Logger, error) { } } - destOutput, err = os.OpenFile(params.File, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) // nolint:gosec - if err != nil { - return nil, fmt.Errorf("error opening log file: %s", err) + destOutput = &lumberjack.Logger{ + Filename: params.File, + MaxSize: log.MaxLogFileSize, + MaxBackups: log.MaxNumberOfBackups, } } diff --git a/go.mod b/go.mod index e7d0d980..40971265 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( golang.org/x/net v0.31.0 golang.org/x/text v0.20.0 gopkg.in/ini.v1 v1.67.0 + gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) require ( diff --git a/go.sum b/go.sum index 5ca7963f..65f1f79f 100644 --- a/go.sum +++ b/go.sum @@ -248,6 +248,8 @@ gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/mgo.v2 v2.0.0-20160818015218-f2b6f6c918c4/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637/go.mod h1:BHsqpu/nsuzkT5BpiH1EMZPLyqSMM8JbIavyFACoFNk= gopkg.in/yaml.v2 v2.0.0-20170712054546-1be3d31502d6/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= diff --git a/pkg/log/log.go b/pkg/log/log.go index 66c45009..54720343 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -4,13 +4,20 @@ import ( "fmt" "io" + jww "github.com/spf13/jwalterweatherman" "go.uber.org/zap" "go.uber.org/zap/zapcore" - jww "github.com/spf13/jwalterweatherman" "github.com/wakatime/wakatime-cli/pkg/version" ) +const ( + // MaxLogFileSize is the maximum size of the log file. + MaxLogFileSize = 25 // 25MB + // MaxNumberOfBackups is the maximum number of log file backups. + MaxNumberOfBackups = 4 +) + // Logger is the log entry. type Logger struct { entry *zap.Logger