Skip to content

Fix custom logging color attributes raising ValueError (#68129)#69433

Open
dwoz wants to merge 1 commit into
saltstack:3006.xfrom
dwoz:fix/issue-68129
Open

Fix custom logging color attributes raising ValueError (#68129)#69433
dwoz wants to merge 1 commit into
saltstack:3006.xfrom
dwoz:fix/issue-68129

Conversation

@dwoz

@dwoz dwoz commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Stops Salt from emitting ValueError: Formatting field not found in record: 'colorlevel' (and equivalent for colormsg, colorname, colorprocess) on every buffered log message when log_fmt_console/log_fmt_logfile uses one of the documented color attributes.

What issues does this PR fix or reference?

Fixes #68129

Previous Behavior

With a configuration such as:

log_fmt_console: '%(colorlevel)s %(colormsg)s'

Salt printed a --- Logging error --- traceback for every log record emitted before the console handler was set up, while still managing to color the records emitted afterwards.

New Behavior

The buffered records are formatted normally (without color escapes for the records that pre-date the console handler), and the records emitted after console logging is configured continue to be colorized as before. No tracebacks are printed.

Root cause

setup_console_handler switches the active log record factory from SaltLogRecord to SaltColorLogRecord. Records that were buffered by the temporary DeferredStreamHandler before that point were instances of SaltLogRecord, which had no color* attributes. shutdown_temp_handler flushed those records to the now-color-format console handler, and python's logging machinery raised ValueError for each missing attribute.

SaltLogRecord now always populates colorname, colorlevel, colorprocess and colormsg with non-colored fallbacks; SaltColorLogRecord keeps overriding them with colorized values for records created after color logging is set up.

Merge requirements satisfied?

  • Docs (no documented behavior changed)
  • Changelog
  • Tests written/updated

Commits signed with GPG?

Yes

When ``log_fmt_console`` (or ``log_fmt_logfile``) was set to a format
string using the documented color attributes (for example
``%(colorlevel)s %(colormsg)s``), Salt printed a ``ValueError:
Formatting field not found in record: 'colorlevel'`` for every log
message that had been buffered by the temporary ``DeferredStreamHandler``
before logging was fully configured.

The bug stems from ``setup_console_handler`` switching the active log
record factory from ``SaltLogRecord`` to ``SaltColorLogRecord`` only
after the deferred handler had been populated.  When
``shutdown_temp_handler`` flushed those buffered records into the newly
installed console handler -- whose formatter referenced the ``color*``
attributes -- python's logging machinery failed because the records had
been constructed by the plain ``SaltLogRecord`` factory and therefore
lacked any ``color*`` attribute.

Make ``SaltLogRecord`` always populate ``colorname``, ``colorlevel``,
``colorprocess`` and ``colormsg`` with non-colored fallback values.
``SaltColorLogRecord`` continues to override them with colorized values
for records created after color logging has been configured.

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

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant