Skip to content

Commit

Permalink
rotate log added
Browse files Browse the repository at this point in the history
  • Loading branch information
PAKalucki committed Sep 12, 2024
1 parent cb5e42c commit 1b11fe4
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 67 deletions.
14 changes: 14 additions & 0 deletions configs/wingologrotate.yaml
Original file line number Diff line number Diff line change
@@ -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"
19 changes: 0 additions & 19 deletions configs/wingologrotate.yaml.example

This file was deleted.

121 changes: 73 additions & 48 deletions logrotate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}
}
}
}
}

0 comments on commit 1b11fe4

Please sign in to comment.