authelia/internal/logging/logger.go

83 lines
1.8 KiB
Go
Raw Normal View History

package logging
import (
"io"
"os"
"strings"
"time"
2019-11-16 19:50:58 +00:00
logrus_stack "github.com/Gurpartap/logrus-stack"
"github.com/sirupsen/logrus"
"github.com/authelia/authelia/v4/internal/configuration/schema"
)
// Logger returns the standard logrus logger.
func Logger() *logrus.Logger {
return logrus.StandardLogger()
}
// InitializeLogger configures the default loggers stack levels, formatting, and the output destinations.
func InitializeLogger(config schema.LogConfiguration, log bool) error {
setLevelStr(config.Level, log)
callerLevels := []logrus.Level{}
stackLevels := []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel}
logrus.AddHook(logrus_stack.NewHook(callerLevels, stackLevels))
if config.Format == logFormatJSON {
logrus.SetFormatter(&logrus.JSONFormatter{})
} else {
logrus.SetFormatter(&logrus.TextFormatter{})
}
if config.FilePath != "" {
filePath := strings.ReplaceAll(config.FilePath, "%d", time.Now().Format("2006-02-01T150405Z"))
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
if config.Format != logFormatJSON {
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
}
if config.KeepStdout {
logLocations := io.MultiWriter(os.Stdout, f)
logrus.SetOutput(logLocations)
} else {
logrus.SetOutput(f)
}
}
return nil
}
func setLevelStr(level string, log bool) {
switch level {
case "error":
logrus.SetLevel(logrus.ErrorLevel)
case "warn":
logrus.SetLevel(logrus.WarnLevel)
case "info":
logrus.SetLevel(logrus.InfoLevel)
case "debug":
logrus.SetLevel(logrus.DebugLevel)
case "trace":
logrus.SetLevel(logrus.TraceLevel)
default:
level = "info (default)"
logrus.SetLevel(logrus.InfoLevel)
}
if log {
logrus.Infof("Log severity set to %s", level)
}
}