mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-19 04:50:02 +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
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRotatingFileFromConfig(config Config) (*RotatingFile, error) {
|
func NewRotatingFileFromConfig(config Config, suffix string) (*RotatingFile, error) {
|
||||||
return NewRotatingFile(config.GetFilePath(), int64(config.MaxFileSize), config.MaxTime)
|
if len(suffix) > 0 {
|
||||||
|
suffix = "_" + suffix
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewRotatingFile(config.GetFilePath()+suffix+".log", int64(config.MaxFileSize), config.MaxTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *RotatingFile) newFilename(name string) string {
|
func (w *RotatingFile) newFilename(name string) string {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path"
|
"path"
|
||||||
|
|
@ -190,18 +191,28 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(conf.Log.Filename) > 0 {
|
if len(conf.Log.Filename) > 0 {
|
||||||
writer, err := NewRotatingFileFromConfig(conf.Log)
|
stdWriter, err := NewRotatingFileFromConfig(conf.Log, "info")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Fatal("Failed to open log")
|
log.WithError(err).Fatal("Failed to open info log")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
errWriter, err := NewRotatingFileFromConfig(conf.Log, "error")
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Fatal("Failed to open error log")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"maxfilesize": conf.Log.MaxFileSize,
|
"maxfilesize": conf.Log.MaxFileSize,
|
||||||
"maxage": conf.Log.MaxTime,
|
"maxage": conf.Log.MaxTime,
|
||||||
"directory": conf.Log.GetDirectory(),
|
"directory": conf.Log.GetDirectory(),
|
||||||
"filename": conf.Log.GetFilename(),
|
"info_filename": stdWriter.GetFilename(),
|
||||||
}).Info("Logging to file: ", conf.Log.GetFilePath())
|
"error_filename": errWriter.GetFilename(),
|
||||||
log.SetOutput(writer)
|
}).Info("Logging to file")
|
||||||
|
|
||||||
|
log.SetOutput(io.Discard)
|
||||||
|
log.AddHook(MakeStdHook(stdWriter))
|
||||||
|
log.AddHook(MakeErrorHook(errWriter))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init telegram notification service
|
// Init telegram notification service
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue