Skip to content

Commit

Permalink
🐛 fix: log file path parse maybe error on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 7, 2024
1 parent 581d3ba commit 9e6939b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# Test binary, build with `go test -c`
*.test
*.log.*
*~

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
Expand Down
11 changes: 8 additions & 3 deletions rotatefile/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/rand"
"os"
"path"
"path/filepath"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -62,7 +63,7 @@ func NewWriterWith(fns ...ConfigFn) (*Writer, error) {
// init rotate dispatcher
func (d *Writer) init() error {
logfile := d.cfg.Filepath
d.fileDir = path.Dir(logfile)
d.fileDir = filepath.Dir(logfile)
d.backupDur = d.cfg.backupDuration()

// if d.cfg.BackupNum > 0 {
Expand Down Expand Up @@ -314,10 +315,14 @@ func (d *Writer) Clean() (err error) {

// oldFiles: xx.log.yy files, no gz file
var oldFiles, gzFiles []fileInfo
fileDir, fileName := path.Split(d.cfg.Filepath)
fileDir, fileName := filepath.Split(d.cfg.Filepath)
if len(fileDir) > 0 {
// removes the trailing separator
fileDir = fileDir[:len(fileDir)-1]
}

// find and clean old files
err = fsutil.FindInDir(fileDir[:len(fileDir)-1], func(fPath string, ent fs.DirEntry) error {
err = fsutil.FindInDir(fileDir, func(fPath string, ent fs.DirEntry) error {
fi, err := ent.Info()
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package slog
import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -47,7 +47,7 @@ func formatCaller(rf *runtime.Frame, flag uint8) (cs string) {
lineNum := strconv.FormatInt(int64(rf.Line), 10)
switch flag {
case CallerFlagFull:
return rf.Function + "," + path.Base(rf.File) + ":" + lineNum
return rf.Function + "," + filepath.Base(rf.File) + ":" + lineNum
case CallerFlagFunc:
return rf.Function
case CallerFlagFcLine:
Expand All @@ -59,12 +59,12 @@ func formatCaller(rf *runtime.Frame, flag uint8) (cs string) {
case CallerFlagPkgFnl:
i := strings.LastIndex(rf.Function, "/")
i += strings.IndexByte(rf.Function[i+1:], '.')
return rf.Function[:i+1] + "," + path.Base(rf.File) + ":" + lineNum
return rf.Function[:i+1] + "," + filepath.Base(rf.File) + ":" + lineNum
case CallerFlagFnlFcn:
ss := strings.Split(rf.Function, ".")
return path.Base(rf.File) + ":" + lineNum + "," + ss[len(ss)-1]
return filepath.Base(rf.File) + ":" + lineNum + "," + ss[len(ss)-1]
case CallerFlagFnLine:
return path.Base(rf.File) + ":" + lineNum
return filepath.Base(rf.File) + ":" + lineNum
case CallerFlagFcName:
ss := strings.Split(rf.Function, ".")
return ss[len(ss)-1]
Expand Down

0 comments on commit 9e6939b

Please sign in to comment.