2019-04-24 21:52:08 +00:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
2021-05-31 04:02:54 +00:00
|
|
|
"io"
|
2020-03-09 19:57:53 +00:00
|
|
|
"os"
|
2022-06-05 23:50:10 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2020-03-09 19:57:53 +00:00
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
logrus_stack "github.com/Gurpartap/logrus-stack"
|
2019-04-24 21:52:08 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-08-03 09:55:21 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
// Logger returns the standard logrus logger.
|
2019-04-24 21:52:08 +00:00
|
|
|
func Logger() *logrus.Logger {
|
|
|
|
return logrus.StandardLogger()
|
|
|
|
}
|
|
|
|
|
2022-09-10 08:02:57 +00:00
|
|
|
// LoggerPrintf returns a new PrintfLogger given a level.
|
|
|
|
func LoggerPrintf(level logrus.Level) (logger *PrintfLogger) {
|
|
|
|
return &PrintfLogger{
|
|
|
|
level: level,
|
|
|
|
logrus: logrus.StandardLogger(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoggerCtxPrintf returns a new CtxPrintfLogger given a level.
|
|
|
|
func LoggerCtxPrintf(level logrus.Level) (logger *CtxPrintfLogger) {
|
|
|
|
return &CtxPrintfLogger{
|
|
|
|
level: level,
|
|
|
|
logrus: logrus.StandardLogger(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
// InitializeLogger configures the default loggers stack levels, formatting, and the output destinations.
|
|
|
|
func InitializeLogger(config schema.LogConfiguration, log bool) error {
|
|
|
|
setLevelStr(config.Level, log)
|
2020-03-09 19:57:53 +00:00
|
|
|
|
2022-10-02 02:07:40 +00:00
|
|
|
var callerLevels []logrus.Level
|
|
|
|
|
2020-03-09 19:57:53 +00:00
|
|
|
stackLevels := []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel}
|
2022-10-02 02:07:40 +00:00
|
|
|
|
2020-03-09 19:57:53 +00:00
|
|
|
logrus.AddHook(logrus_stack.NewHook(callerLevels, stackLevels))
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
if config.Format == logFormatJSON {
|
2020-11-24 23:46:41 +00:00
|
|
|
logrus.SetFormatter(&logrus.JSONFormatter{})
|
|
|
|
} else {
|
|
|
|
logrus.SetFormatter(&logrus.TextFormatter{})
|
|
|
|
}
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
if config.FilePath != "" {
|
2022-06-05 23:50:10 +00:00
|
|
|
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)
|
2020-11-12 23:14:45 +00:00
|
|
|
|
2020-03-09 19:57:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
if config.Format != logFormatJSON {
|
2021-01-15 11:16:41 +00:00
|
|
|
logrus.SetFormatter(&logrus.TextFormatter{
|
|
|
|
DisableColors: true,
|
|
|
|
FullTimestamp: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
if config.KeepStdout {
|
2021-05-31 04:02:54 +00:00
|
|
|
logLocations := io.MultiWriter(os.Stdout, f)
|
|
|
|
logrus.SetOutput(logLocations)
|
|
|
|
} else {
|
|
|
|
logrus.SetOutput(f)
|
|
|
|
}
|
2020-03-09 19:57:53 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-03-09 19:57:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|