diff --git a/backend/init/log/log.go b/backend/init/log/log.go index 132a89070..38164f1a8 100644 --- a/backend/init/log/log.go +++ b/backend/init/log/log.go @@ -16,6 +16,7 @@ import ( const ( TimeFormat = "2006-01-02 15:04:05" + FileTImeFormat = "2006-01-02-15-04-05" RollingTimePattern = "0 0 * * *" ) @@ -31,9 +32,10 @@ func setOutput(logger *logrus.Logger, config configs.LogConfig) { writer, err := log.NewWriterFromConfig(&log.Config{ LogPath: config.Path, FileName: config.LogName, - TimeTagFormat: TimeFormat, + TimeTagFormat: FileTImeFormat, MaxRemain: config.LogBackup, RollingTimePattern: RollingTimePattern, + LogSuffix: config.LogSuffix, }) if err != nil { panic(err) diff --git a/backend/log/config.go b/backend/log/config.go index 438840bb2..08c1d6173 100644 --- a/backend/log/config.go +++ b/backend/log/config.go @@ -20,6 +20,7 @@ type Config struct { TimeTagFormat string LogPath string FileName string + LogSuffix string MaxRemain int RollingTimePattern string } @@ -35,6 +36,6 @@ type RollingWriter interface { } func FilePath(c *Config) (filepath string) { - filepath = path.Join(c.LogPath, c.FileName) + ".log" + filepath = path.Join(c.LogPath, c.FileName) + c.LogSuffix return } diff --git a/backend/log/manager.go b/backend/log/manager.go index e451084d4..c24f3a991 100644 --- a/backend/log/manager.go +++ b/backend/log/manager.go @@ -47,7 +47,7 @@ func NewManager(c *Config) (Manager, error) { func (m *manager) GenLogFileName(c *Config) (filename string) { m.lock.Lock() - filename = path.Join(c.LogPath, c.FileName+"-"+m.startAt.Format(c.TimeTagFormat)) + ".log" + filename = path.Join(c.LogPath, c.FileName+"-"+m.startAt.Format(c.TimeTagFormat)) + c.LogSuffix m.startAt = time.Now() m.lock.Unlock() return diff --git a/backend/log/writer.go b/backend/log/writer.go index 87473dd6b..698482c38 100644 --- a/backend/log/writer.go +++ b/backend/log/writer.go @@ -119,11 +119,13 @@ func NewWriterFromConfig(c *Config) (RollingWriter, error) { continue } - fileName := c.FileName + ".log." - if strings.Contains(fi.Name(), fileName) { - fileSuffix := path.Ext(fi.Name()) - if len(fileSuffix) > 1 { - _, err := time.Parse(c.TimeTagFormat, fileSuffix[1:]) + fileName := c.FileName + if strings.Contains(fi.Name(), fileName) && strings.Contains(fi.Name(), c.LogSuffix+".tar.gz") { + start := strings.Index(fi.Name(), "-") + end := strings.Index(fi.Name(), c.LogSuffix) + name := fi.Name() + if start > 0 && end > 0 { + _, err := time.Parse(c.TimeTagFormat, name[start+1:end]) if err == nil { files = append(files, fi.Name()) } @@ -131,10 +133,12 @@ func NewWriterFromConfig(c *Config) (RollingWriter, error) { } } sort.Slice(files, func(i, j int) bool { - fileSuffix1 := path.Ext(files[i]) - fileSuffix2 := path.Ext(files[j]) - t1, _ := time.Parse(c.TimeTagFormat, fileSuffix1[1:]) - t2, _ := time.Parse(c.TimeTagFormat, fileSuffix2[1:]) + t1Start := strings.Index(files[i], "-") + t1End := strings.Index(files[i], c.LogSuffix) + t2Start := strings.Index(files[i], "-") + t2End := strings.Index(files[i], c.LogSuffix) + t1, _ := time.Parse(c.TimeTagFormat, files[i][t1Start+1:t1End]) + t2, _ := time.Parse(c.TimeTagFormat, files[j][t2Start+1:t2End]) return t1.Before(t2) })