From 6729985002e44b5d6dbac6cd65d0c4244cdfce2e Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 3 May 2023 13:17:40 +0200 Subject: [PATCH] cmd/main/main.go: implement logrotation. --- cmd/main/main.go | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/cmd/main/main.go b/cmd/main/main.go index 6e7e2b5..9a27759 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "os/signal" + "path" "syscall" "time" @@ -15,6 +16,7 @@ import ( "github.com/eosswedenorg/thalos/app" "github.com/eosswedenorg/thalos/app/abi" "github.com/eosswedenorg/thalos/app/config" + . "github.com/eosswedenorg/thalos/app/log" "github.com/go-redis/redis/v8" log "github.com/sirupsen/logrus" @@ -127,15 +129,6 @@ func main() { return } - if len(*logFile) > 0 { - logfd, err := os.OpenFile(*logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644) - if err != nil { - log.WithError(err).Fatal("failed open logfile") - } - log.Info("Logging to file: ", *logFile) - log.SetOutput(logfd) - } - // Write PID file if len(*pidFile) > 0 { log.Infof("Writing pid to: %s", *pidFile) @@ -153,6 +146,27 @@ func main() { return } + // If log file is given on the commandline, override config values. + if len(*logFile) > 0 { + conf.Log.Directory = path.Dir(*logFile) + conf.Log.Filename = path.Base(*logFile) + } + + if len(conf.Log.Filename) > 0 { + writer, err := NewRotatingFileFromConfig(conf.Log) + if err != nil { + log.WithError(err).Fatal("Failed to open log") + return + } + log.WithFields(log.Fields{ + "maxfilesize": conf.Log.MaxFileSize, + "maxage": conf.Log.MaxTime, + "directory": conf.Log.GetDirectory(), + "filename": conf.Log.GetFilename(), + }).Info("Logging to file: ", conf.Log.GetFilePath()) + log.SetOutput(writer) + } + // Init telegram notification service telegram, err := telegram.New(conf.Telegram.Id) if err != nil {