-
Notifications
You must be signed in to change notification settings - Fork 257
/
logs.go
41 lines (35 loc) · 831 Bytes
/
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
package db_local
import (
"log"
"os"
"path/filepath"
"time"
"github.com/turbot/steampipe/pkg/filepaths"
)
const logRetentionDays = 7
func TrimLogs() {
fileLocation := filepaths.EnsureLogDir()
files, err := os.ReadDir(fileLocation)
if err != nil {
log.Println("[TRACE] error listing db log directory", err)
}
for _, file := range files {
fi, err := file.Info()
if err != nil {
log.Printf("[TRACE] error reading file info of %s. continuing\n", file.Name())
continue
}
fileName := fi.Name()
if filepath.Ext(fileName) != ".log" {
continue
}
age := time.Since(fi.ModTime()).Hours()
if age > logRetentionDays*24 {
logPath := filepath.Join(fileLocation, fileName)
err := os.Remove(logPath)
if err != nil {
log.Printf("[TRACE] failed to delete log file %s\n", logPath)
}
}
}
}