Skip to content

Commit

Permalink
Add custom log file name date format (#4333)
Browse files Browse the repository at this point in the history
  • Loading branch information
louyuexing authored Aug 27, 2024
1 parent 29400f6 commit 075817a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions core/logx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ type LogConf struct {
// daily: daily rotation.
// size: size limited rotation.
Rotation string `json:",default=daily,options=[daily,size]"`
// FileTimeFormat represents the time format for file name, default is `2006-01-02T15:04:05.000Z07:00`.
FileTimeFormat string `json:",optional"`
}
4 changes: 4 additions & 0 deletions core/logx/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ func SetUp(c LogConf) (err error) {
timeFormat = c.TimeFormat
}

if len(c.FileTimeFormat) > 0 {
fileTimeFormat = c.FileTimeFormat
}

atomic.StoreUint32(&maxContentLength, c.MaxContentLength)

switch c.Encoding {
Expand Down
8 changes: 5 additions & 3 deletions core/logx/rotatelogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

const (
dateFormat = "2006-01-02"
fileTimeFormat = time.RFC3339
hoursPerDay = 24
bufferSize = 100
defaultDirMode = 0o755
Expand All @@ -28,8 +27,11 @@ const (
megaBytes = 1 << 20
)

// ErrLogFileClosed is an error that indicates the log file is already closed.
var ErrLogFileClosed = errors.New("error: log file closed")
var (
// ErrLogFileClosed is an error that indicates the log file is already closed.
ErrLogFileClosed = errors.New("error: log file closed")
fileTimeFormat = time.RFC3339
)

type (
// A RotateRule interface is used to define the log rotating rules.
Expand Down

0 comments on commit 075817a

Please sign in to comment.