Skip to content

Commit 9f8cdf2

Browse files
authored
fix(logging): respect log levels for skipped files (#102)
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.
1 parent 807a790 commit 9f8cdf2

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

cmd/headers.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,12 @@ config, see the "copywrite init" command.`,
105105
// log prefix, e.g. logger.Println("[DEBUG] this is inferred as a debug log")
106106
InferLevels: true,
107107
})
108-
// Redirect output to stdout to match the rest of the command output structure
109-
// Ideally, we would differentiate inside the addlicense run, but in lieu of
110-
// reworking the entire logging functionality there, this at least gets us
111-
// consistency and fixes a race condition when using GitHub Action log groups
112-
stdcliLogger.SetOutput(os.Stdout)
108+
109+
// WARNING: because of the way we redirect cliLogger to os.Stdout, anything
110+
// prefixed with "[ERROR]" will not implicitly be written to stderr.
111+
// However, we propagate errors upward from addlicense and then run a
112+
// cobra.CheckErr on the return, which will indeed output to stderr and
113+
// return a non-zero error code.
113114

114115
gha.StartGroup("The following files are missing headers:")
115116
err := addlicense.Run(ignoredPatterns, "only", licenseData, "", verbose, plan, []string{"."}, stdcliLogger)

cmd/root.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ func initLogger() {
9696

9797
hclog.Default().Named("cli")
9898
cliLogger = hclog.New(&hclog.LoggerOptions{
99-
Name: "cli",
100-
Level: logLevel,
101-
Color: hclog.AutoColor,
99+
Name: "cli",
100+
Level: logLevel,
101+
Color: hclog.AutoColor,
102+
Output: os.Stdout,
102103
})
103104
}

0 commit comments

Comments
 (0)