1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-16 04:24:56 +02:00

cmd/main/main.go: implement logrotation.

This commit is contained in:
Henrik Hautakoski 2023-05-03 13:17:40 +02:00
parent e44dc2f4f1
commit 6729985002

View file

@ -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 {