fix: limit deleted raft log name

Signed-off-by: wenjia322 <buaa1214wwj@126.com>
This commit is contained in:
wenjia322 2020-09-18 18:05:15 +08:00
parent 465154be4c
commit 4a780bb323
2 changed files with 27 additions and 6 deletions

View File

@ -15,21 +15,26 @@ func TestCleanRaftLog(t *testing.T) {
os.MkdirAll(dir, 0755)
}
logFilePath1 := path.Join(dir, "test_clean.log.old")
logFilePath1 := path.Join(dir, "raft_info.log.old")
if err = createFile(logFilePath1, true); err != nil {
t.Errorf("create file[%v] err[%v]", logFilePath1, err)
return
}
logFilePath2 := path.Join(dir, "test_clean.log")
logFilePath2 := path.Join(dir, "test_clean.log.old")
if err = createFile(logFilePath2, true); err != nil {
t.Errorf("create file[%v] err[%v]", logFilePath2, err)
return
}
logFilePath3 := path.Join(dir, "test_clean.log.new")
logFilePath3 := path.Join(dir, "raft_err.log.new")
if err = createFile(logFilePath3, false); err != nil {
t.Errorf("create file[%v] err[%v]", logFilePath3, err)
return
}
logFilePath4 := path.Join(dir, "raft_info.log")
if err = createFile(logFilePath4, true); err != nil {
t.Errorf("create file[%v] err[%v]", logFilePath4, err)
return
}
newRaftLogger("/tmp/raft")
time.Sleep(time.Second * 10)
@ -49,6 +54,11 @@ func TestCleanRaftLog(t *testing.T) {
t.Errorf("expect file[%v] exists but err is [%v]", logFilePath3, err)
return
}
_, err = os.Stat(logFilePath4)
if err != nil {
t.Errorf("expect file[%v] exists but err is [%v]", logFilePath4, err)
return
}
os.RemoveAll(dir)
}

View File

@ -255,7 +255,7 @@ func NewLog(dir, module, level string) (*Log, error) {
if dir != "" {
go lg.checkLogRotation(dir, module)
go lg.checkCleanLog(dir)
go lg.checkCleanLog(dir, module)
}
go lg.loopMsg()
@ -428,7 +428,7 @@ func (l *Log) checkLogRotation(logDir, module string) {
}
}
func (l *Log) checkCleanLog(logDir string) {
func (l *Log) checkCleanLog(logDir, module string) {
// handle panic
defer func() {
if r := recover(); r != nil {
@ -449,7 +449,7 @@ func (l *Log) checkCleanLog(logDir string) {
}
var needDelFiles RolledFile
for _, info := range fInfos {
if time.Since(info.ModTime()) > LogMaxReservedDays && !strings.HasSuffix(info.Name(), ".log") {
if time.Since(info.ModTime()) > LogMaxReservedDays && isExpiredRaftLog(module, info.Name()) {
needDelFiles = append(needDelFiles, info)
}
}
@ -463,6 +463,17 @@ func (l *Log) checkCleanLog(logDir string) {
}
}
func isExpiredRaftLog(module, name string) bool {
if strings.HasSuffix(name, ".log") {
return false
}
if strings.HasPrefix(name, module + infoLogFileName) || strings.HasPrefix(name, module + debugLogFileName) ||
strings.HasPrefix(name, module + warnLogFileName) || strings.HasPrefix(name, module + errLogFileName) {
return true
}
return false
}
func (l *Log) Debug(format string, v ...interface{}) {
if l.IsEnableDebug() {
l.Output(3, l.SetPrefix(fmt.Sprintf(format+"\r\n", v...), levels[DebugLevel]), false)