auto delete logs after x days
parent
06fe5a0333
commit
a0796b9cb2
7
main.go
7
main.go
|
@ -19,6 +19,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"jannex/log-manager/modules/cache"
|
"jannex/log-manager/modules/cache"
|
||||||
"jannex/log-manager/modules/config"
|
"jannex/log-manager/modules/config"
|
||||||
|
"jannex/log-manager/modules/loghandler"
|
||||||
"jannex/log-manager/modules/structs"
|
"jannex/log-manager/modules/structs"
|
||||||
"jannex/log-manager/modules/utils"
|
"jannex/log-manager/modules/utils"
|
||||||
"jannex/log-manager/routers/router"
|
"jannex/log-manager/routers/router"
|
||||||
|
@ -36,6 +37,12 @@ func init() {
|
||||||
utils.ValidatorInit()
|
utils.ValidatorInit()
|
||||||
|
|
||||||
utils.CreateDirectoryIfNotExists(config.Cfg.LogFolder)
|
utils.CreateDirectoryIfNotExists(config.Cfg.LogFolder)
|
||||||
|
|
||||||
|
// checking for deletable logs on startup
|
||||||
|
loghandler.CheckForDeletableLogs()
|
||||||
|
|
||||||
|
// starting the background log deleter
|
||||||
|
go loghandler.StartBackgroundLogDeleter()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -2,7 +2,9 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
@ -14,6 +16,7 @@ type Config struct {
|
||||||
Port string
|
Port string
|
||||||
LogFolder string
|
LogFolder string
|
||||||
SSEServerEnabled bool
|
SSEServerEnabled bool
|
||||||
|
DaysToKeepLogs int
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig() {
|
func LoadConfig() {
|
||||||
|
@ -25,10 +28,17 @@ func LoadConfig() {
|
||||||
fmt.Println("Load env from system")
|
fmt.Println("Load env from system")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
daysToKeepLogs, err := strconv.Atoi(os.Getenv("DAYS_TO_KEEP_LOGS"))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to load DAYS_TO_KEEP_LOGS from env, err: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
Cfg = Config{
|
Cfg = Config{
|
||||||
Host: os.Getenv("HOST"),
|
Host: os.Getenv("HOST"),
|
||||||
Port: os.Getenv("PORT"),
|
Port: os.Getenv("PORT"),
|
||||||
LogFolder: os.Getenv("LOG_FOLDER"),
|
LogFolder: os.Getenv("LOG_FOLDER"),
|
||||||
SSEServerEnabled: os.Getenv("SSE_SERVER_ENABLED") == "true",
|
SSEServerEnabled: os.Getenv("SSE_SERVER_ENABLED") == "true",
|
||||||
|
DaysToKeepLogs: daysToKeepLogs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"jannex/log-manager/modules/structs"
|
"jannex/log-manager/modules/structs"
|
||||||
"jannex/log-manager/modules/utils"
|
"jannex/log-manager/modules/utils"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -162,3 +163,66 @@ func GetAvailableLogTypes() []string {
|
||||||
|
|
||||||
return availableLogTypes
|
return availableLogTypes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func StartBackgroundLogDeleter() {
|
||||||
|
ticker := time.NewTicker(24 * time.Hour)
|
||||||
|
|
||||||
|
for range ticker.C {
|
||||||
|
CheckForDeletableLogs()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckForDeletableLogs() {
|
||||||
|
|
||||||
|
daysToKeepLogs := config.Cfg.DaysToKeepLogs
|
||||||
|
|
||||||
|
logFolder := config.Cfg.LogFolder
|
||||||
|
|
||||||
|
err := filepath.Walk(logFolder, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if !info.IsDir() {
|
||||||
|
// check if the file is older than the daysToKeepLogs
|
||||||
|
// get the date from the file name
|
||||||
|
date := strings.Split(info.Name(), ".")[0]
|
||||||
|
|
||||||
|
dateFormat := "2-1-2006"
|
||||||
|
|
||||||
|
fileDate, err := time.Parse(dateFormat, date)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the date from the daysToKeepLogs
|
||||||
|
daysToKeepLogsDate := time.Now().AddDate(0, 0, -daysToKeepLogs)
|
||||||
|
|
||||||
|
// compare the dates
|
||||||
|
if fileDate.Before(daysToKeepLogsDate) {
|
||||||
|
// delete the file
|
||||||
|
err := os.Remove(path)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
AddLog(structs.LogBody{
|
||||||
|
Type: "system",
|
||||||
|
Logs: []string{"I "+ utils.GetTime() + "LogManager: Deleted log file " + path + " because it was older than " + strconv.Itoa(daysToKeepLogs) + " days."},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ package utils
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
@ -49,3 +50,7 @@ func CreateDirectoryIfNotExists(path string) {
|
||||||
os.Mkdir(path, 0755)
|
os.Mkdir(path, 0755)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetTime() string {
|
||||||
|
return time.Now().Format("15:04:05 02-01-2006") + " "
|
||||||
|
}
|
Loading…
Reference in New Issue