From 075817a8dd76c268886735b6c6ff420656b052bf Mon Sep 17 00:00:00 2001 From: kui Date: Tue, 27 Aug 2024 20:43:25 +0800 Subject: [PATCH] Add custom log file name date format (#4333) --- core/logx/config.go | 2 ++ core/logx/logs.go | 4 ++++ core/logx/rotatelogger.go | 8 +++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/core/logx/config.go b/core/logx/config.go index a86e4d74..8e6eb7f8 100644 --- a/core/logx/config.go +++ b/core/logx/config.go @@ -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"` } diff --git a/core/logx/logs.go b/core/logx/logs.go index 627bab9b..2f84cd00 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -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 { diff --git a/core/logx/rotatelogger.go b/core/logx/rotatelogger.go index 61f9a9d0..58d837ae 100644 --- a/core/logx/rotatelogger.go +++ b/core/logx/rotatelogger.go @@ -19,7 +19,6 @@ import ( const ( dateFormat = "2006-01-02" - fileTimeFormat = time.RFC3339 hoursPerDay = 24 bufferSize = 100 defaultDirMode = 0o755 @@ -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.