Skip to content

Commit

Permalink
Merge pull request #1125 from wakatime/feature/log-file-rotation
Browse files Browse the repository at this point in the history
Add log file rotation
  • Loading branch information
gandarez authored Nov 18, 2024
2 parents 910dd41 + c4bfa8c commit 97c1b1e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
10 changes: 6 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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,
}
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
9 changes: 8 additions & 1 deletion pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 97c1b1e

Please sign in to comment.