From 3d5ee16e90bc0d7462318ce7231f3a114e8b66f6 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Fri, 15 Oct 2021 10:38:32 +1100 Subject: [PATCH] Fix colours on console when logging to file (#1846) * Fix colours on console when logging to file --- pkg/logger/hook.go | 25 ++++++++++++++++ pkg/logger/logger.go | 29 +++++++++++++++---- .../components/Changelog/versions/v0110.md | 1 + 3 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 pkg/logger/hook.go diff --git a/pkg/logger/hook.go b/pkg/logger/hook.go new file mode 100644 index 000000000..b0e0417dc --- /dev/null +++ b/pkg/logger/hook.go @@ -0,0 +1,25 @@ +package logger + +import ( + "io" + + "github.com/sirupsen/logrus" +) + +type fileLogHook struct { + Writer io.Writer + Formatter logrus.Formatter +} + +func (hook *fileLogHook) Fire(entry *logrus.Entry) error { + line, err := hook.Formatter.Format(entry) + if err != nil { + return err + } + _, err = hook.Writer.Write(line) + return err +} + +func (hook *fileLogHook) Levels() []logrus.Level { + return logrus.AllLevels +} diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 0e2a3db0b..2cf8d10c3 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -2,7 +2,6 @@ package logger import ( "fmt" - "io" "os" "sync" "time" @@ -35,6 +34,13 @@ func Init(logFile string, logOut bool, logLevel string) { customFormatter.FullTimestamp = true logger.SetFormatter(customFormatter) + // #1837 - trigger the console to use color-mode since it won't be + // otherwise triggered until the first log entry + // this is covers the situation where the logger is only logging to file + // and therefore does not trigger the console color-mode - resulting in + // the access log colouring not being applied + _, _ = customFormatter.Format(logrus.NewEntry(logger)) + if logFile != "" { var err error file, err = os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) @@ -44,11 +50,22 @@ func Init(logFile string, logOut bool, logLevel string) { } } - if file != nil && logOut { - mw := io.MultiWriter(os.Stderr, file) - logger.Out = mw - } else if file != nil { - logger.Out = file + if file != nil { + if logOut { + // log to file separately disabling colours + fileFormatter := new(logrus.TextFormatter) + fileFormatter.TimestampFormat = customFormatter.TimestampFormat + fileFormatter.FullTimestamp = customFormatter.FullTimestamp + logger.AddHook(&fileLogHook{ + Writer: file, + Formatter: fileFormatter, + }) + } else { + // logging to file only + // turn off the colouring for the file + customFormatter.ForceColors = false + logger.Out = file + } } // otherwise, output to StdErr diff --git a/ui/v2.5/src/components/Changelog/versions/v0110.md b/ui/v2.5/src/components/Changelog/versions/v0110.md index 8b6d7c015..db698cadb 100644 --- a/ui/v2.5/src/components/Changelog/versions/v0110.md +++ b/ui/v2.5/src/components/Changelog/versions/v0110.md @@ -4,6 +4,7 @@ * Added interface options to disable creating performers/studios/tags from dropdown selectors. ([#1814](https://github.com/stashapp/stash/pull/1814)) ### 🐛 Bug fixes +* Fix colour codes not outputting correctly when logging to file on Windows. ([#1846](https://github.com/stashapp/stash/pull/1846)) * Sort directory listings using case sensitive collation. ([#1823](https://github.com/stashapp/stash/pull/1823)) * Fix auto-tag logic for names which have single-letter words. ([#1817](https://github.com/stashapp/stash/pull/1817)) * Fix huge memory usage spike during clean task. ([#1805](https://github.com/stashapp/stash/pull/1805))