Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom log file name date format #4333

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
timeFormat = c.TimeFormat
}

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

Check warning on line 298 in core/logx/logs.go

View check run for this annotation

Codecov / codecov/patch

core/logx/logs.go#L298

Added line #L298 was not covered by tests
}

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