From fd7b4ada027bc178ac3a42d81e43de288bf4477f Mon Sep 17 00:00:00 2001 From: Amir Zarrinkafsh Date: Fri, 15 Jan 2021 22:16:41 +1100 Subject: [PATCH] [BUGFIX] Disable colored logging outputs when file is specified (#1603) 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. --- internal/logging/const.go | 3 +++ internal/logging/logger.go | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 internal/logging/const.go diff --git a/internal/logging/const.go b/internal/logging/const.go new file mode 100644 index 000000000..22e233feb --- /dev/null +++ b/internal/logging/const.go @@ -0,0 +1,3 @@ +package logging + +const logFormatJSON = "json" diff --git a/internal/logging/logger.go b/internal/logging/logger.go index b81ddd927..d0b1e8ae5 100644 --- a/internal/logging/logger.go +++ b/internal/logging/logger.go @@ -23,7 +23,7 @@ func InitializeLogger(format, filename string) error { stackLevels := []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel} logrus.AddHook(logrus_stack.NewHook(callerLevels, stackLevels)) - if format == "json" { + if format == logFormatJSON { logrus.SetFormatter(&logrus.JSONFormatter{}) } else { logrus.SetFormatter(&logrus.TextFormatter{}) @@ -36,6 +36,13 @@ func InitializeLogger(format, filename string) error { return err } + if format != logFormatJSON { + logrus.SetFormatter(&logrus.TextFormatter{ + DisableColors: true, + FullTimestamp: true, + }) + } + logrus.SetOutput(f) }