From 9c88a8a55adf7779039504fa77d74ec80d658c40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 28 Oct 2018 16:06:50 +0100 Subject: [PATCH] common/loggers: Make sure the global logger also gets colored labels See #4414 --- commands/hugo.go | 5 +---- common/loggers/loggers.go | 22 ++++++++++++++++++++-- go.mod | 2 +- go.sum | 2 ++ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/commands/hugo.go b/commands/hugo.go index ce11626eb65..ffc6f0228f1 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -162,10 +162,7 @@ func (c *commandeer) createLogger(cfg config.Provider, running bool) (*loggers.L } } - // The global logger is used in some few cases. - jww.SetLogOutput(logHandle) - jww.SetLogThreshold(logThreshold) - jww.SetStdoutThreshold(stdoutThreshold) + loggers.InitGlobalLogger(stdoutThreshold, logThreshold, outHandle, logHandle) helpers.InitLoggers() return loggers.NewLogger(stdoutThreshold, logThreshold, outHandle, logHandle, running), nil diff --git a/common/loggers/loggers.go b/common/loggers/loggers.go index 37f50a6c041..0a9c7da68c3 100644 --- a/common/loggers/loggers.go +++ b/common/loggers/loggers.go @@ -115,9 +115,19 @@ func (a labelColorizer) Write(p []byte) (n int, err error) { } -func newLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, logHandle io.Writer, saveErrors bool) *Logger { +// InitGlobalLogger initalizes the global logger, used in some rare cases. +func InitGlobalLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, logHandle io.Writer) { + outHandle, logHandle = getLogWriters(outHandle, logHandle) + + jww.SetStdoutOutput(outHandle) + jww.SetLogOutput(logHandle) + jww.SetLogThreshold(logThreshold) + jww.SetStdoutThreshold(stdoutThreshold) + +} + +func getLogWriters(outHandle, logHandle io.Writer) (io.Writer, io.Writer) { isTerm := terminal.IsTerminal(os.Stdout) - errorCounter := &jww.Counter{} if logHandle != ioutil.Discard && isTerm { // Remove any Ansi coloring from log output logHandle = ansiCleaner{w: logHandle} @@ -127,6 +137,14 @@ func newLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, logHandle outHandle = labelColorizer{w: outHandle} } + return outHandle, logHandle + +} + +func newLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, logHandle io.Writer, saveErrors bool) *Logger { + errorCounter := &jww.Counter{} + outHandle, logHandle = getLogWriters(outHandle, logHandle) + listeners := []jww.LogListener{jww.LogCounter(errorCounter, jww.LevelError)} var errorBuff *bytes.Buffer if saveErrors { diff --git a/go.mod b/go.mod index 797f561c929..837117cb126 100644 --- a/go.mod +++ b/go.mod @@ -47,7 +47,7 @@ require ( github.com/spf13/cast v1.3.0 github.com/spf13/cobra v0.0.3 github.com/spf13/fsync v0.0.0-20170320142552-12a01e648f05 - github.com/spf13/jwalterweatherman v1.0.1-0.20181005085228-103a6da826d0 + github.com/spf13/jwalterweatherman v1.0.1-0.20181028145347-94f6ae3ed3bc github.com/spf13/nitro v0.0.0-20131003134307-24d7ef30a12d github.com/spf13/pflag v1.0.2 github.com/spf13/viper v1.2.0 diff --git a/go.sum b/go.sum index 09a90fb4968..3aeffabf386 100644 --- a/go.sum +++ b/go.sum @@ -113,6 +113,8 @@ github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9 github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/jwalterweatherman v1.0.1-0.20181005085228-103a6da826d0 h1:kPJPXmEs6V1YyXfHFbp1NCpdqhvFVssh2FGx7+OoJLM= github.com/spf13/jwalterweatherman v1.0.1-0.20181005085228-103a6da826d0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= +github.com/spf13/jwalterweatherman v1.0.1-0.20181028145347-94f6ae3ed3bc h1:IwxheNsBTTVRE87d8rhsO9BA4JU35tFxUNZzcFR7gdc= +github.com/spf13/jwalterweatherman v1.0.1-0.20181028145347-94f6ae3ed3bc/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= github.com/spf13/nitro v0.0.0-20131003134307-24d7ef30a12d h1:ihvj2nmx8eqWjlgNgdW6h0DyGJuq5GiwHadJkG0wXtQ= github.com/spf13/nitro v0.0.0-20131003134307-24d7ef30a12d/go.mod h1:jU8A+8xL+6n1OX4XaZtCj4B3mIa64tULUsD6YegdpFo= github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc=