Skip to content

cfg: respect VOUCH_LOGLEVEL when no config file value is set#610

Open
c-tonneslan wants to merge 1 commit into
vouch:masterfrom
c-tonneslan:fix/loglevel-env-respected
Open

cfg: respect VOUCH_LOGLEVEL when no config file value is set#610
c-tonneslan wants to merge 1 commit into
vouch:masterfrom
c-tonneslan:fix/loglevel-env-respected

Conversation

@c-tonneslan
Copy link
Copy Markdown

Fixes #540.

`Logging.configure()` reset `Cfg.LogLevel` back to the default whenever `viper.IsSet` on the config-file key was false. `envconfig` (which is what reads `VOUCH_LOGLEVEL`) writes straight into `Cfg.LogLevel` and never goes through viper, so a user who set `VOUCH_LOGLEVEL=debug` without also putting `logLevel` in the config file always ended up back on info.

Also check the matching env var before clobbering the value, so an env-only configuration is honored. The config-file path still wins when both are present, matching the documented precedence.

Logging.configure() reset Cfg.LogLevel back to the default whenever
viper.IsSet on the config-file key was false. envconfig writes the
env-var value straight into Cfg.LogLevel and never goes through viper,
so a user who set VOUCH_LOGLEVEL=debug without also putting logLevel
in the config file always ended up back on info.

Also check the matching env var before overriding so an env-only
configuration is honored. The config-file path still wins when both
are present, matching the documented precedence.

Fixes vouch#540.

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
@bnfinet
Copy link
Copy Markdown
Member

bnfinet commented May 17, 2026

@c-tonneslan very much appreciate the contribution.

Regarding...

The config-file path still wins when both are present,

Please take a look at...

// The priority of the sources is the following:

The ENV var should take precedence.

@c-tonneslan
Copy link
Copy Markdown
Author

Good catch, sorry about that. You're right, the documented precedence is env > config and the actual order in Configure already does that: parseConfigFile populates Cfg.LogLevel and then configureFromEnv overwrites it via envconfig.Process. My fix doesn't change that flow, it just stops Logging.configure from clobbering the env value back to the default when no config-file value is set.

The line I wrote in the PR description ("config-file path still wins when both are present") was backwards. Want me to amend the commit message or push a description-only fixup?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting log level via environment variables does not work

2 participants