From 202822b689965e764fa05b922032ef39e22be8b5 Mon Sep 17 00:00:00 2001 From: Caleb Albers Date: Tue, 7 Nov 2023 12:15:30 -0800 Subject: [PATCH] fix(logging): respect log levels for skipped files Previously, the `copywrite headers` command would output a lot of `[DEBUG] skipping file...` statements, esp. when large folders were excluded (looking at you, `NODE_MODULES`). This made for a poor experience, and was a bug introduced when I made #80 - as in an attempt to fix an issue with GHA log group directives and addlicense sending to stdout and stderr, respectively, I introduced a bug that ignored the `COPYWRITE_LOG_LEVEL` env var. This PR fixes that error while ensuring the GHA log grouping still works by redirecting the `cliLogger` we use throughout the headers cmd (and others) to stdout by default. This doesn't appear to be a large shift or change for other commands, but does make a big difference for the headers command. --- cmd/headers.go | 11 ++++++----- cmd/root.go | 7 ++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cmd/headers.go b/cmd/headers.go index f74c3cf..097cf91 100644 --- a/cmd/headers.go +++ b/cmd/headers.go @@ -105,11 +105,12 @@ config, see the "copywrite init" command.`, // log prefix, e.g. logger.Println("[DEBUG] this is inferred as a debug log") InferLevels: true, }) - // Redirect output to stdout to match the rest of the command output structure - // Ideally, we would differentiate inside the addlicense run, but in lieu of - // reworking the entire logging functionality there, this at least gets us - // consistency and fixes a race condition when using GitHub Action log groups - stdcliLogger.SetOutput(os.Stdout) + + // WARNING: because of the way we redirect cliLogger to os.Stdout, anything + // prefixed with "[ERROR]" will not implicitly be written to stderr. + // However, we propagate errors upward from addlicense and then run a + // cobra.CheckErr on the return, which will indeed output to stderr and + // return a non-zero error code. gha.StartGroup("The following files are missing headers:") err := addlicense.Run(ignoredPatterns, "only", licenseData, "", verbose, plan, []string{"."}, stdcliLogger) diff --git a/cmd/root.go b/cmd/root.go index be7082a..cccb9e6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -96,8 +96,9 @@ func initLogger() { hclog.Default().Named("cli") cliLogger = hclog.New(&hclog.LoggerOptions{ - Name: "cli", - Level: logLevel, - Color: hclog.AutoColor, + Name: "cli", + Level: logLevel, + Color: hclog.AutoColor, + Output: os.Stdout, }) }