From 9dfa1126177952433f8339a87fb566c2959fc3d6 Mon Sep 17 00:00:00 2001 From: Bjørn Erik Pedersen Date: Fri, 13 Dec 2024 09:23:09 +0100 Subject: Write all logging (INFO, WARN, ERROR) to stderr The old setup tried to log >= warning to stderr, the rest to stdout. However, that logic was flawed, so warnings ended up in stdout, which makes `hugo list all` etc. hard to reason about from scripts. This commit fixes this by making all logging (info, warn, error) log to stderr and let stdout be reserved for program output. Fixes #13074 --- commands/commandeer.go | 21 ++++++++++++--------- commands/list.go | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) (limited to 'commands') diff --git a/commands/commandeer.go b/commands/commandeer.go index bb82ec654..3f9f02d23 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -103,7 +103,8 @@ type configKey struct { type rootCommand struct { Printf func(format string, v ...interface{}) Println func(a ...interface{}) - Out io.Writer + StdOut io.Writer + StdErr io.Writer logger loggers.Logger @@ -356,7 +357,7 @@ func (r *rootCommand) getOrCreateHugo(cfg config.Provider, ignoreModuleDoesNotEx } func (r *rootCommand) newDepsConfig(conf *commonConfig) deps.DepsCfg { - return deps.DepsCfg{Configs: conf.configs, Fs: conf.fs, LogOut: r.logger.Out(), LogLevel: r.logger.Level(), ChangesFromBuild: r.changesFromBuild} + return deps.DepsCfg{Configs: conf.configs, Fs: conf.fs, StdOut: r.logger.StdOut(), StdErr: r.logger.StdErr(), LogLevel: r.logger.Level(), ChangesFromBuild: r.changesFromBuild} } func (r *rootCommand) Name() string { @@ -421,21 +422,23 @@ func (r *rootCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args } func (r *rootCommand) PreRun(cd, runner *simplecobra.Commandeer) error { - r.Out = os.Stdout + r.StdOut = os.Stdout + r.StdErr = os.Stderr if r.quiet { - r.Out = io.Discard + r.StdOut = io.Discard + r.StdErr = io.Discard } // Used by mkcert (server). - log.SetOutput(r.Out) + log.SetOutput(r.StdOut) r.Printf = func(format string, v ...interface{}) { if !r.quiet { - fmt.Fprintf(r.Out, format, v...) + fmt.Fprintf(r.StdOut, format, v...) } } r.Println = func(a ...interface{}) { if !r.quiet { - fmt.Fprintln(r.Out, a...) + fmt.Fprintln(r.StdOut, a...) } } _, running := runner.Command.(*serverCommand) @@ -485,8 +488,8 @@ func (r *rootCommand) createLogger(running bool) (loggers.Logger, error) { optsLogger := loggers.Options{ DistinctLevel: logg.LevelWarn, Level: level, - Stdout: r.Out, - Stderr: r.Out, + StdOut: r.StdOut, + StdErr: r.StdErr, StoreErrors: running, } diff --git a/commands/list.go b/commands/list.go index f362e22f1..42f3408ba 100644 --- a/commands/list.go +++ b/commands/list.go @@ -57,7 +57,7 @@ func newListCommand() *listCommand { return err } - writer := csv.NewWriter(r.Out) + writer := csv.NewWriter(r.StdOut) defer writer.Flush() writer.Write([]string{ -- cgit v1.2.3