This commit is contained in:
2026-08-14 16:34:17 +08:00
parent e0b0e1d8c8
commit f63acae353
53 changed files with 2488 additions and 350 deletions
+30 -7
View File
@@ -11,15 +11,38 @@ import (
// file handle open for the active day and rotates on the first write after
// midnight, matching the directory layout consumed by the log viewer.
type DailyWriter struct {
mu sync.Mutex
root string
name string
date string
file *os.File
mu sync.Mutex
root string
name string
date string
file *os.File
retentionDay int
}
func NewDailyWriter(root, name string) *DailyWriter {
return &DailyWriter{root: root, name: name}
func NewDailyWriter(root, name string, retentionDay int) *DailyWriter {
w := &DailyWriter{root: root, name: name, retentionDay: retentionDay}
w.removeExpired()
return w
}
func (w *DailyWriter) removeExpired() {
if w.retentionDay <= 0 {
return
}
entries, err := os.ReadDir(w.root)
if err != nil {
return
}
cutoff := time.Now().AddDate(0, 0, -w.retentionDay)
for _, entry := range entries {
if !entry.IsDir() {
continue
}
date, err := time.Parse("2006-01-02", entry.Name())
if err == nil && date.Before(cutoff) {
_ = os.RemoveAll(filepath.Join(w.root, entry.Name()))
}
}
}
func (w *DailyWriter) Write(value []byte) (int, error) {