forked from safing/portmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
57 lines (49 loc) · 1.31 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package core
import (
"context"
"os"
"path/filepath"
"strings"
"time"
"github.com/safing/portbase/dataroot"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
)
const (
logTTL = 30 * 24 * time.Hour
logFileDir = "logs"
logFileSuffix = ".log"
)
func registerLogCleaner() {
module.NewTask("log cleaner", logCleaner).
Repeat(24 * time.Hour).
Schedule(time.Now().Add(15 * time.Minute))
}
func logCleaner(_ context.Context, _ *modules.Task) error {
ageThreshold := time.Now().Add(-logTTL)
return filepath.Walk(
filepath.Join(dataroot.Root().Path, logFileDir),
func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Warningf("core: failed to access %s while deleting old log files: %s", path, err)
return nil
}
switch {
case !info.Mode().IsRegular():
// Only delete regular files.
case !strings.HasSuffix(path, logFileSuffix):
// Only delete files that end with the correct suffix.
case info.ModTime().After(ageThreshold):
// Only delete files that are older that the log TTL.
default:
// Delete log file.
err := os.Remove(path)
if err != nil {
log.Warningf("core: failed to delete old log file %s: %s", path, err)
} else {
log.Tracef("core: deleted old log file %s", path)
}
}
return nil
})
}