mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-20 09:56:47 +02:00
commit
fc97b8cd02
4 changed files with 78 additions and 11 deletions
48
app/log/HookWriter.go
Normal file
48
app/log/HookWriter.go
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HookWriter struct {
|
||||||
|
Writer io.Writer
|
||||||
|
LogLevels []log.Level
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hook *HookWriter) Fire(entry *log.Entry) error {
|
||||||
|
line, err := entry.String()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = hook.Writer.Write([]byte(line))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hook *HookWriter) Levels() []log.Level {
|
||||||
|
return hook.LogLevels
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeStdHook(writer io.Writer) *HookWriter {
|
||||||
|
return &HookWriter{
|
||||||
|
Writer: writer,
|
||||||
|
LogLevels: []log.Level{
|
||||||
|
log.InfoLevel,
|
||||||
|
log.DebugLevel,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeErrorHook(writer io.Writer) *HookWriter {
|
||||||
|
return &HookWriter{
|
||||||
|
Writer: writer,
|
||||||
|
LogLevels: []log.Level{
|
||||||
|
log.ErrorLevel,
|
||||||
|
log.WarnLevel,
|
||||||
|
log.FatalLevel,
|
||||||
|
log.PanicLevel,
|
||||||
|
log.TraceLevel,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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 {
|
||||||
|
|
@ -58,6 +62,10 @@ func (w *RotatingFile) newFilename(name string) string {
|
||||||
return fmt.Sprintf("%s-%s%s", name, time.Now().Format(w.format), ext)
|
return fmt.Sprintf("%s-%s%s", name, time.Now().Format(w.format), ext)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w RotatingFile) GetFilename() string {
|
||||||
|
return path.Base(w.fd.Name())
|
||||||
|
}
|
||||||
|
|
||||||
// Rotate the file.
|
// Rotate the file.
|
||||||
func (w *RotatingFile) Rotate() error {
|
func (w *RotatingFile) Rotate() error {
|
||||||
dst, err := os.OpenFile(w.newFilename(w.fd.Name()), os.O_CREATE|os.O_WRONLY, 0o666)
|
dst, err := os.OpenFile(w.newFilename(w.fd.Name()), os.O_CREATE|os.O_WRONLY, 0o666)
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ message_codec: "json"
|
||||||
# Logging settings
|
# Logging settings
|
||||||
log:
|
log:
|
||||||
# Filename to use.
|
# Filename to use.
|
||||||
filename: thalos.log
|
filename: thalos
|
||||||
# Directory to store the logfiles in.
|
# Directory to store the logfiles in.
|
||||||
directory: logs
|
directory: logs
|
||||||
# Format to rename log files when rotating
|
# Format to rename log files when rotating
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue