fix: first log message not in JSON format when JSON logging is configured #15
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
When the application server is configured to use JSON log format (
logging.json: trueorDLC_LOGGING_JSON=true), the first few log messages emitted during config loading are still in human-readable console format instead of JSON.Affected log messages
In
pkg/config/config.go,LoadConfig()sets up a console writer before reading the config:The following logs are then emitted while the logger is still in console format:
"Using custom config file path"(whenDLC_CONFIG_FILEis set)"Error reading config file, using defaults"(on config file error)"Config file loaded"← always emitted when a config file is foundThe logger is only switched to JSON format after
v.Unmarshal()at line 231, so by that point the "Config file loaded" message has already been written in text format.Expected behaviour
All log messages, including those emitted during config loading, should respect the
logging.jsonsetting. If JSON logging is configured, every log line — including the very first — must be valid JSON.Root cause
Classic chicken-and-egg: the logger format depends on the config, but the config load itself emits logs.
Proposed fix
Peek at the JSON logging setting before the first log is emitted, by:
DLC_LOGGING_JSONenv var directly viaos.Getenv()(highest priority, zero cost)viperinstance to read onlylogging.jsonThis allows the logger to be configured in the correct format before any output is produced. The existing format-switch block after unmarshal becomes a no-op for stderr (idempotent) and is left in place for clarity.