diff --git a/configs/wingologrotate.yaml b/configs/wingologrotate.yaml new file mode 100644 index 0000000..e7d5032 --- /dev/null +++ b/configs/wingologrotate.yaml @@ -0,0 +1,14 @@ +schedule: "*/30 * * * *" + +logs: +- path: "C:\\workspace\\test\\test_logs\\test2\\*.txt" + type: delete + condition: + age: "30m" + +- path: + - "C:\\workspace\\test\\test_logs\\test3\\*.txt" + - "C:\\workspace\\test\\test_logs\\test4\\*.txt" + type: delete + condition: + age: "30m" \ No newline at end of file diff --git a/configs/wingologrotate.yaml.example b/configs/wingologrotate.yaml.example deleted file mode 100644 index d795fcf..0000000 --- a/configs/wingologrotate.yaml.example +++ /dev/null @@ -1,19 +0,0 @@ -schedule: "0 0 * * *" - -logs: -- path: "/tmp/test/logs/delete/*.log" - type: delete - condition: - age: "30m" - -- path: "/tmp/test/logs/compress/*.log" - type: rotate - condition: - max_keep: 5 - size: "30MB" - compress: true - -- path: - - "/path1/*.log" - - "/path2/*.log" - type: delete \ No newline at end of file diff --git a/logrotate.go b/logrotate.go index 791293a..8449c42 100644 --- a/logrotate.go +++ b/logrotate.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "os" "path/filepath" @@ -34,7 +35,7 @@ func runLogRotation() { if err != nil { log.Printf("Failed to schedule task for path %s: %v", logEntry.Path, err) } else { - log.Printf("Scheduled task for path %s with schedule: %s", logEntry.Path, schedule) + log.Printf("Scheduled task for path %s", logEntry.Path) } } @@ -97,51 +98,75 @@ func createTask(logEntry LogEntry) func() { } func rotateLogFiles(logEntry LogEntry) { - log.Printf("Rotating logs for path: %s", logEntry.Path) - // matchingFiles, err := filepath.Glob(filepath.Clean(logEntry.Path)) - // if err != nil { - // log.Printf("Failed to expand wildcard for path %s: %v", logEntry.Path, err) - // return - // } - - // for _, file := range matchingFiles { - // // Check if file size exceeds the specified condition - // fileInfo, err := os.Stat(file) - // if err != nil { - // log.Printf("Failed to get file info for %s: %v", file, err) - // continue - // } - - // // Parse size condition - // maxSize, err := parseSize(logEntry.Condition.Size) - // if err != nil { - // log.Printf("Invalid size format for rotation: %v", err) - // continue - // } - - // if fileInfo.Size() >= maxSize { - // rotatedFilePath := fmt.Sprintf("%s.%s", file, time.Now().Format("20060102-150405")) - // if err := os.Rename(file, rotatedFilePath); err != nil { - // log.Printf("Failed to rotate log file %s: %v", file, err) - // continue - // } - // log.Printf("Rotated log file: %s to %s", file, rotatedFilePath) - - // // Compress the rotated log file if necessary - // if logEntry.Condition.Compress == nil || *logEntry.Condition.Compress { - // if err := compressLogFile(rotatedFilePath); err != nil { - // log.Printf("Failed to compress rotated log file %s: %v", rotatedFilePath, err) - // } else { - // log.Printf("Compressed log file: %s", rotatedFilePath) - // } - // } - - // // Remove old log files if MaxKeep is set - // if logEntry.Condition.MaxKeep != nil { - // if err := removeOldLogFiles(filepath.Dir(file), filepath.Base(file), *logEntry.Condition.MaxKeep); err != nil { - // log.Printf("Failed to remove old log files: %v", err) - // } - // } - // } - // } + for _, path := range logEntry.Path { + log.Printf("Rotating logs for path: %s", path) + matchingFiles, err := filepath.Glob(filepath.Clean(path)) + if err != nil { + log.Printf("Failed to expand wildcard for path %s: %v", path, err) + return + } + + for _, file := range matchingFiles { + fileInfo, err := os.Stat(file) + if err != nil { + log.Printf("Failed to get file info for %s: %v", file, err) + continue + } + + // Check the size condition, if provided + rotateDueToSize := false + if logEntry.Condition.Size != nil { + maxSize, err := parseSize(*logEntry.Condition.Size) + if err != nil { + log.Printf("Invalid size format for rotation: %v", err) + continue + } + + if fileInfo.Size() >= maxSize { + rotateDueToSize = true + } + } + + // Check the age condition, if provided + rotateDueToAge := false + if logEntry.Condition.Age != nil { + ageDuration, err := parseDuration(*logEntry.Condition.Age) + if err != nil { + log.Printf("Invalid age format for rotation: %v", err) + continue + } + + fileAge := time.Since(fileInfo.ModTime()) + if fileAge >= ageDuration { + rotateDueToAge = true + } + } + + // Rotate the file if either size or age condition is met + if rotateDueToSize || rotateDueToAge { + rotatedFilePath := fmt.Sprintf("%s.%s", file, time.Now().Format("20060102-150405")) + if err := os.Rename(file, rotatedFilePath); err != nil { + log.Printf("Failed to rotate log file %s: %v", file, err) + continue + } + log.Printf("Rotated log file: %s to %s", file, rotatedFilePath) + + // Compress the rotated log file if necessary + if logEntry.Condition.Compress == nil || *logEntry.Condition.Compress { + if err := compressLogFile(rotatedFilePath); err != nil { + log.Printf("Failed to compress rotated log file %s: %v", rotatedFilePath, err) + } else { + log.Printf("Compressed log file: %s", rotatedFilePath) + } + } + + // Remove old log files if MaxKeep is set + if logEntry.Condition.MaxKeep != nil { + if err := removeOldLogFiles(filepath.Dir(file), filepath.Base(file), *logEntry.Condition.MaxKeep); err != nil { + log.Printf("Failed to remove old log files: %v", err) + } + } + } + } + } }