BREAKING CHANGE: Remodelled logging setup
- other.ConfigureLoggingFromCliArgs has been renamed to other.ConfigureLogging - The -logfile cli argument has been removed and replaced with an argument to other.ConfigureLogging. This argument takes a pointer to a string or nil. If it's a valid pointer, it's used just like the -logfile cli flag before - The logfile, if requested via the previously mentioned parameter, can now be rotated by calling the returned function. If no logfile is specified, the returned function does nothing
This commit is contained in:
parent
eedc7b9dc1
commit
afcc54c831
2 changed files with 18 additions and 20 deletions
|
@ -15,13 +15,13 @@ type RotateWriter struct {
|
|||
}
|
||||
|
||||
// Make a new RotateWriter. Return nil if error occurs during setup.
|
||||
func New(filename string) *RotateWriter {
|
||||
func New(filename string) (*RotateWriter, error) {
|
||||
w := &RotateWriter{filename: filename}
|
||||
err := w.Rotate()
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
return w
|
||||
return w, nil
|
||||
}
|
||||
|
||||
// Write satisfies the io.Writer interface.
|
||||
|
|
|
@ -8,11 +8,12 @@ import (
|
|||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"git.mstar.dev/mstar/goutils/logrotate"
|
||||
)
|
||||
|
||||
var cliFlagLogLevel = "info"
|
||||
var cliFlagLogJson = false
|
||||
var cliFlagLogFile = ""
|
||||
|
||||
func SetupFlags() {
|
||||
flag.StringVar(
|
||||
|
@ -27,17 +28,15 @@ func SetupFlags() {
|
|||
false,
|
||||
"Log json objects to stderr instead of nicely formatted ones",
|
||||
)
|
||||
flag.StringVar(
|
||||
&cliFlagLogFile,
|
||||
"logfile",
|
||||
"",
|
||||
"Set the file to write json formatted log messages to",
|
||||
)
|
||||
}
|
||||
|
||||
func ConfigureLoggingFromCliArgs() {
|
||||
configOutputs()
|
||||
// If logfileName != nil, that file will be used for writing logs to.
|
||||
// Additionally, this function will then return another function
|
||||
// for rotating the log file
|
||||
func ConfigureLogging(logfileName *string) func() error {
|
||||
rotateFunc := configOutputs(logfileName)
|
||||
configLevel()
|
||||
return rotateFunc
|
||||
}
|
||||
|
||||
func configLevel() {
|
||||
|
@ -60,18 +59,16 @@ func configLevel() {
|
|||
log.Info().Str("new-level", cliFlagLogLevel).Msg("New logging level set")
|
||||
}
|
||||
|
||||
func configOutputs() {
|
||||
func configOutputs(logfileName *string) (rotateLogs func() error) {
|
||||
rotateLogs = func() error { return nil }
|
||||
extraLogWriters := []io.Writer{}
|
||||
if cliFlagLogFile != "" {
|
||||
file, err := os.OpenFile(cliFlagLogFile, os.O_APPEND|os.O_CREATE, os.ModeAppend)
|
||||
if logfileName != nil {
|
||||
logfile, err := logrotate.New(*logfileName)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to open log file")
|
||||
}
|
||||
// NOTE: Technically this leaks a file handle
|
||||
// Shouldn't matter though since the log file should be open until the very end
|
||||
// Sooooo, eh. Don't care about adding an elaborate system to close the file at the very end.
|
||||
// OS will do that anyway
|
||||
extraLogWriters = append(extraLogWriters, file)
|
||||
extraLogWriters = append(extraLogWriters, logfile)
|
||||
rotateLogs = logfile.Rotate
|
||||
}
|
||||
if !cliFlagLogJson {
|
||||
console := zerolog.ConsoleWriter{Out: os.Stderr}
|
||||
|
@ -84,4 +81,5 @@ func configOutputs() {
|
|||
append([]io.Writer{log.Logger}, extraLogWriters...)...,
|
||||
)).With().Timestamp().Logger()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue