1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-02-27 18:44:13 +08:00

fix: 修改不能删除日志超量文件的BUG

This commit is contained in:
zhengkunwang223 2022-12-08 23:34:06 +08:00 committed by zhengkunwang223
parent 46201cc169
commit 62a298f6dd
4 changed files with 19 additions and 12 deletions

@ -16,6 +16,7 @@ import (
const ( const (
TimeFormat = "2006-01-02 15:04:05" TimeFormat = "2006-01-02 15:04:05"
FileTImeFormat = "2006-01-02-15-04-05"
RollingTimePattern = "0 0 * * *" RollingTimePattern = "0 0 * * *"
) )
@ -31,9 +32,10 @@ func setOutput(logger *logrus.Logger, config configs.LogConfig) {
writer, err := log.NewWriterFromConfig(&log.Config{ writer, err := log.NewWriterFromConfig(&log.Config{
LogPath: config.Path, LogPath: config.Path,
FileName: config.LogName, FileName: config.LogName,
TimeTagFormat: TimeFormat, TimeTagFormat: FileTImeFormat,
MaxRemain: config.LogBackup, MaxRemain: config.LogBackup,
RollingTimePattern: RollingTimePattern, RollingTimePattern: RollingTimePattern,
LogSuffix: config.LogSuffix,
}) })
if err != nil { if err != nil {
panic(err) panic(err)

@ -20,6 +20,7 @@ type Config struct {
TimeTagFormat string TimeTagFormat string
LogPath string LogPath string
FileName string FileName string
LogSuffix string
MaxRemain int MaxRemain int
RollingTimePattern string RollingTimePattern string
} }
@ -35,6 +36,6 @@ type RollingWriter interface {
} }
func FilePath(c *Config) (filepath string) { func FilePath(c *Config) (filepath string) {
filepath = path.Join(c.LogPath, c.FileName) + ".log" filepath = path.Join(c.LogPath, c.FileName) + c.LogSuffix
return return
} }

@ -47,7 +47,7 @@ func NewManager(c *Config) (Manager, error) {
func (m *manager) GenLogFileName(c *Config) (filename string) { func (m *manager) GenLogFileName(c *Config) (filename string) {
m.lock.Lock() 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.startAt = time.Now()
m.lock.Unlock() m.lock.Unlock()
return return

@ -119,11 +119,13 @@ func NewWriterFromConfig(c *Config) (RollingWriter, error) {
continue continue
} }
fileName := c.FileName + ".log." fileName := c.FileName
if strings.Contains(fi.Name(), fileName) { if strings.Contains(fi.Name(), fileName) && strings.Contains(fi.Name(), c.LogSuffix+".tar.gz") {
fileSuffix := path.Ext(fi.Name()) start := strings.Index(fi.Name(), "-")
if len(fileSuffix) > 1 { end := strings.Index(fi.Name(), c.LogSuffix)
_, err := time.Parse(c.TimeTagFormat, fileSuffix[1:]) name := fi.Name()
if start > 0 && end > 0 {
_, err := time.Parse(c.TimeTagFormat, name[start+1:end])
if err == nil { if err == nil {
files = append(files, fi.Name()) files = append(files, fi.Name())
} }
@ -131,10 +133,12 @@ func NewWriterFromConfig(c *Config) (RollingWriter, error) {
} }
} }
sort.Slice(files, func(i, j int) bool { sort.Slice(files, func(i, j int) bool {
fileSuffix1 := path.Ext(files[i]) t1Start := strings.Index(files[i], "-")
fileSuffix2 := path.Ext(files[j]) t1End := strings.Index(files[i], c.LogSuffix)
t1, _ := time.Parse(c.TimeTagFormat, fileSuffix1[1:]) t2Start := strings.Index(files[i], "-")
t2, _ := time.Parse(c.TimeTagFormat, fileSuffix2[1:]) 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) return t1.Before(t2)
}) })