fix clean self log
@@ -64,6 +64,17 @@ func getValidPath(confPaths []string) (paths []string) {
return
}
+type MatchFile struct {
+ Name string
+ ModTime time.Time
+}
+
+type MatchFiles []MatchFile
+
+func (f MatchFiles) Len() int { return len(f) }
+func (f MatchFiles) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
+func (f MatchFiles) Less(i, j int) bool { return f[i].ModTime.Before(f[j].ModTime) }
+
func cleanLogkitLog(dir, pattern string, reserveCnt int) {
var err error
path := filepath.Join(dir, pattern)
@@ -72,12 +83,24 @@ func cleanLogkitLog(dir, pattern string, reserveCnt int) {
log.Errorf("filepath.Glob path %v error %v", path, err)
return
}
- if len(matches) <= reserveCnt {
+ var files MatchFiles
+ for _, name := range matches {
+ info, err := os.Stat(name)
+ if err != nil {
+ log.Errorf("os.Stat name %v error %v", name, err)
+ continue
+ }
+ files = append(files, MatchFile{
+ Name: name,
+ ModTime: info.ModTime(),
+ })
+ }
+ if len(files) <= reserveCnt {
return
}
- sort.Strings(matches)
- for _, f := range matches[0 : len(matches)-reserveCnt] {
- err := os.Remove(f)
+ sort.Sort(files)
+ for _, f := range files[0 : len(files)-reserveCnt] {
+ err := os.Remove(f.Name)
if err != nil {
log.Errorf("Remove %s failed , error: %v", f, err)
continue
