Skip to content

Don't treat message and asctime as extras in ExtraAdder - #850

Open
Debasishhh wants to merge 4 commits into
hynek:mainfrom
Debasishhh:extraadder-formatter-attrs
Open

Debasishhh wants to merge 4 commits into
hynek:mainfrom
Debasishhh:extraadder-formatter-attrs

Conversation

@Debasishhh

Copy link
Copy Markdown

Summary

ExtraAdder builds its list of standard LogRecord attributes from a freshly created record, which doesn't have message or asctime yet. logging.Formatter.format() sets both on the record, so if another handler formats it before ProcessorFormatter runs (a plain console handler next to a JSON one, pytest's caplog, etc.), ExtraAdder copies them into the event dict as if they were extras:

root = logging.getLogger()
plain = logging.StreamHandler()
plain.setFormatter(logging.Formatter("%(asctime)s %(message)s"))
json_h = logging.StreamHandler()
json_h.setFormatter(ProcessorFormatter(processor=JSONRenderer(), foreign_pre_chain=[ExtraAdder()]))
root.addHandler(plain)
root.addHandler(json_h)

logging.getLogger("app").warning("user %s logged in", "bob", extra={"request_id": "r1"})
# {"event": "user bob logged in", "request_id": "r1", "message": "user bob logged in", "asctime": "..."}

The output also depends on handler order. Both names are reserved by Logger.makeRecord() (it raises if you pass them in extra), so they can never be real extras. This adds them to _LOG_RECORD_KEYS, which only ExtraAdder uses.

#842 adds the same two names to a separate reserved set for render_to_log_kwargs; this doesn't touch that code path.

Pull Request Checklist

  • I acknowledge this project's AI policy.
  • This pull request is not from my main branch.
  • There's tests for all new and changed code.
  • New APIs are added to our typing tests in api.py.
  • Updated documentation for changed code.
    • New functions/classes have to be added to docs/api.rst by hand.
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
  • Documentation in .rst and .md files is written using semantic newlines.
  • Changes (and possible deprecations) are documented in the changelog.

@codspeed

codspeed Bot commented Oct 18, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 11 untouched benchmarks


Comparing Debasishhh:extraadder-formatter-attrs (0b6a92e) with main (19c4f21)

Open in CodSpeed

This branch has not been deployed

No deployments
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.

1 participant