Skip to content

Commit 97826fe

Browse files
committed
fix(log): rotate the app log at 10 MB and compress backups
The log file only rotated at 100 MB, so a slow-growing app.log stayed on disk indefinitely: lumberjack applies MaxAge to rotated files only, never to the active one. Lower the rotation threshold to 10 MB and gzip the rotated backups to keep the whole log directory small. Also use filepath.Dir so log paths are handled correctly on Windows.
1 parent 7a2f881 commit 97826fe

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

‎internal/log/log.go‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@ import (
44
"fmt"
55
"io"
66
"os"
7-
"path"
7+
"path/filepath"
88

99
"github.com/fatih/color"
1010
"github.com/rs/zerolog"
1111
"github.com/rs/zerolog/log"
1212
"gopkg.in/natefinch/lumberjack.v2"
1313
)
1414

15+
const (
16+
// maxLogSizeMB is the size at which the active log file is rotated.
17+
maxLogSizeMB = 10
18+
// maxLogBackups is the number of rotated log files kept on disk.
19+
maxLogBackups = 3
20+
// maxLogAgeDays is the maximum age of a rotated log file.
21+
maxLogAgeDays = 7
22+
)
23+
1524
func init() {
1625
zerolog.SetGlobalLevel(zerolog.InfoLevel)
1726
zerolog.TimeFieldFormat = "2006-01-02 15:04:05"
@@ -123,7 +132,7 @@ func CreateRollingLogFile(logPath string) io.Writer {
123132
return nil
124133
}
125134

126-
logDir := path.Dir(logPath)
135+
logDir := filepath.Dir(logPath)
127136
if err := os.MkdirAll(logDir, os.ModePerm); err != nil {
128137
log.Err(err).Msgf("Can't create log directory. path: %s, err: %s", logPath, err)
129138
return nil
@@ -132,8 +141,9 @@ func CreateRollingLogFile(logPath string) io.Writer {
132141
log.Info().Msgf("Log file: %s", logPath)
133142
return &lumberjack.Logger{
134143
Filename: logPath,
135-
MaxSize: 100, // megabytes
136-
MaxBackups: 3,
137-
MaxAge: 7, //daysdays
144+
MaxSize: maxLogSizeMB,
145+
MaxBackups: maxLogBackups,
146+
MaxAge: maxLogAgeDays,
147+
Compress: true,
138148
}
139149
}

0 commit comments

Comments
 (0)
⌂ 换网址