Skip to content

Commit

Permalink
✅ test: add a test case for issues #121
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 21, 2024
1 parent 1b25cfd commit ff670f2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
3 changes: 3 additions & 0 deletions handler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,6 @@ func WithCompress(compress bool) ConfigFn {
func WithUseJSON(useJSON bool) ConfigFn {
return func(c *Config) { c.UseJSON = useJSON }
}

// WithDebugMode setting for debug mode
func WithDebugMode(c *Config) { c.DebugMode = true }
36 changes: 36 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (

"github.com/gookit/goutil/byteutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/goutil/timex"
"github.com/gookit/slog"
"github.com/gookit/slog/handler"
"github.com/gookit/slog/rotatefile"
)

// https://github.com/gookit/slog/issues/27
Expand Down Expand Up @@ -149,3 +151,37 @@ func TestIssues_139(t *testing.T) {
ctx := context.WithValue(context.Background(), "requestid", "111111")
L.WithCtx(ctx).Info("test")
}

// https://github.com/gookit/slog/issues/121
// 当我配置按日期的方式来滚动日志时,当大于 1 天时只能按 1 天来滚动日志。
func TestIssues_121(t *testing.T) {
seconds := timex.OneDaySec * 7 // 7天
logFile := "testdata/issue121_7day.log"

clock := rotatefile.NewMockClock("2024-03-25 08:04:02")
fh, err := handler.NewTimeRotateFileHandler(
logFile,
rotatefile.RotateTime(seconds),
handler.WithLogLevels(slog.NormalLevels),
handler.WithBuffSize(128),
handler.WithBackupNum(20),
handler.WithTimeClock(clock),
handler.WithDebugMode, // debug mode
// handler.WithCompress(log.compress),
// handler.WithFilePerm(log.filePerm),
)
assert.NoError(t, err)

// create logger with handler and clock.
l := slog.NewWithHandlers(fh).Config(func(sl *slog.Logger) {
sl.TimeClock = clock.Now
})

// add logs
for i := 0; i < 50; i++ {
l.Infof("hi, this is a exmple information ... message text. log index=%d", i)
clock.Add(24 * timex.Hour)
}

l.MustClose()
}
2 changes: 1 addition & 1 deletion rotatefile/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (c *Config) IsMode(m RotateMode) bool { return c.RotateMode == m }
// Debug print debug message on development
func (c *Config) Debug(vs ...any) {
if c.DebugMode {
stdio.WriteString("[rotate-file.DEBUG] " + fmt.Sprintln(vs...))
stdio.WriteString("[rotatefile.DEBUG] " + fmt.Sprintln(vs...))
}
}

Expand Down
9 changes: 5 additions & 4 deletions rotatefile/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,17 +289,17 @@ func (d *Writer) asyncClean() {

// start a goroutine to clean backups
go func() {
d.cfg.Debug("start a goroutine consumer for clean old files")
d.cfg.Debug("START a goroutine consumer for clean old files")

// consume the signal until stop
for {
select {
case <-d.cleanCh:
d.cfg.Debug("clean old files handling ...")
d.cfg.Debug("receive signal - clean old files handling")
printErrln("rotatefile: clean old files error:", d.Clean())
case <-d.stopCh:
d.cleanCh = nil
d.cfg.Debug("stop consumer for clean old files")
d.cfg.Debug("STOP consumer for clean old files")
return // stop clean
}
}
Expand All @@ -317,7 +317,7 @@ func (d *Writer) Clean() (err error) {
fileDir, fileName := path.Split(d.cfg.Filepath)

// find and clean old files
err = fsutil.FindInDir(fileDir, func(fPath string, ent fs.DirEntry) error {
err = fsutil.FindInDir(fileDir[:len(fileDir)-2], func(fPath string, ent fs.DirEntry) error {
fi, err := ent.Info()
if err != nil {
return err
Expand Down Expand Up @@ -434,6 +434,7 @@ func (d *Writer) buildFilterFns(fileName string) []fsutil.FilterFunc {
}

// remove expired files
d.cfg.Debug("remove expired file:", fPath)
printErrln("rotatefile: remove expired file error:", os.Remove(fPath))
return false
})
Expand Down

0 comments on commit ff670f2

Please sign in to comment.