1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 00:09:16 +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

View File

@ -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)

View File

@ -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
}

View File

@ -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

View File

@ -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)
})