mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-16 04:24:56 +02:00
cmd/thalos/main.go: Implement log splitting if file logging is enabled.
This commit is contained in:
parent
e1c092659a
commit
93e765f14c
2 changed files with 25 additions and 10 deletions
|
|
@ -46,8 +46,12 @@ func NewRotatingFile(filename string, maxSize int64, maxAge time.Duration) (*Rot
|
|||
}, nil
|
||||
}
|
||||
|
||||
func NewRotatingFileFromConfig(config Config) (*RotatingFile, error) {
|
||||
return NewRotatingFile(config.GetFilePath(), int64(config.MaxFileSize), config.MaxTime)
|
||||
func NewRotatingFileFromConfig(config Config, suffix string) (*RotatingFile, error) {
|
||||
if len(suffix) > 0 {
|
||||
suffix = "_" + suffix
|
||||
}
|
||||
|
||||
return NewRotatingFile(config.GetFilePath()+suffix+".log", int64(config.MaxFileSize), config.MaxTime)
|
||||
}
|
||||
|
||||
func (w *RotatingFile) newFilename(name string) string {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
|
|
@ -190,18 +191,28 @@ func main() {
|
|||
}
|
||||
|
||||
if len(conf.Log.Filename) > 0 {
|
||||
writer, err := NewRotatingFileFromConfig(conf.Log)
|
||||
stdWriter, err := NewRotatingFileFromConfig(conf.Log, "info")
|
||||
if err != nil {
|
||||
log.WithError(err).Fatal("Failed to open log")
|
||||
log.WithError(err).Fatal("Failed to open info log")
|
||||
return
|
||||
}
|
||||
errWriter, err := NewRotatingFileFromConfig(conf.Log, "error")
|
||||
if err != nil {
|
||||
log.WithError(err).Fatal("Failed to open error 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)
|
||||
"maxfilesize": conf.Log.MaxFileSize,
|
||||
"maxage": conf.Log.MaxTime,
|
||||
"directory": conf.Log.GetDirectory(),
|
||||
"info_filename": stdWriter.GetFilename(),
|
||||
"error_filename": errWriter.GetFilename(),
|
||||
}).Info("Logging to file")
|
||||
|
||||
log.SetOutput(io.Discard)
|
||||
log.AddHook(MakeStdHook(stdWriter))
|
||||
log.AddHook(MakeErrorHook(errWriter))
|
||||
}
|
||||
|
||||
// Init telegram notification service
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue