mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
fd7b4ada02
In some scenarios if a user has a `log_file_path` specified and a TTY seems to be detected this causes terminal coloring outputs to be written to the file. This in turn will cause issues when attempting to utilise the log with the provided fail2ban regexes. We now override any TTY detection/logging treatments and disable coloring/removal of the timestamp when a user is utilising the text based logger to a file.
51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
package logging
|
|
|
|
import (
|
|
"os"
|
|
|
|
logrus_stack "github.com/Gurpartap/logrus-stack"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Logger return the standard logrus logger.
|
|
func Logger() *logrus.Logger {
|
|
return logrus.StandardLogger()
|
|
}
|
|
|
|
// SetLevel set the level of the logger.
|
|
func SetLevel(level logrus.Level) {
|
|
logrus.SetLevel(level)
|
|
}
|
|
|
|
// InitializeLogger initialize logger.
|
|
func InitializeLogger(format, filename string) error {
|
|
callerLevels := []logrus.Level{}
|
|
stackLevels := []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel}
|
|
logrus.AddHook(logrus_stack.NewHook(callerLevels, stackLevels))
|
|
|
|
if format == logFormatJSON {
|
|
logrus.SetFormatter(&logrus.JSONFormatter{})
|
|
} else {
|
|
logrus.SetFormatter(&logrus.TextFormatter{})
|
|
}
|
|
|
|
if filename != "" {
|
|
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if format != logFormatJSON {
|
|
logrus.SetFormatter(&logrus.TextFormatter{
|
|
DisableColors: true,
|
|
FullTimestamp: true,
|
|
})
|
|
}
|
|
|
|
logrus.SetOutput(f)
|
|
}
|
|
|
|
return nil
|
|
}
|