Skip to content

[core] support unbraced $name fields in $ style formats - #69

Merged
nhairs merged 3 commits into
nhairs:mainfrom
Sanjays2402:fix/string-template-unbraced-names
Aug 15, 2026
Merged

nhairs merged 3 commits into
nhairs:mainfrom
Sanjays2402:fix/string-template-unbraced-names

Conversation

@Sanjays2402

Copy link
Copy Markdown
Contributor

Closes #18

parse() matched only ${name} for StringTemplateStyle, so a $-style format like "$asctime $levelname $message" returned no fields at all and the emitted JSON was missing every requested attribute. string.Template accepts both $name and ${name}, so the regex now matches both and skips the $$ escape.

New test_string_template_format sits beside test_percentage_format/test_comma_format in tests/test_formatters.py and covers both forms plus $$; it fails on the current regex and passes with the fix.

This change was prepared with AI assistance; the regression test was run locally and fails without the fix.

Sanjays2402 and others added 2 commits August 15, 2026 20:52
BaseJsonFormatter.parse() matched only ${name} for StringTemplateStyle,
so a format like "$asctime $message" produced no fields at all and the
resulting log records were missing every requested attribute. Python's
string.Template accepts both $name and ${name}.

The regex now matches both forms and skips the $$ escape, and parse()
picks whichever group matched.

Closes nhairs#18
@nhairs
nhairs force-pushed the fix/string-template-unbraced-names branch from c48cdca to 81d368f Compare August 15, 2026 10:59

@nhairs nhairs left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work on this @Sanjays2402

@nhairs nhairs changed the title fix(core): support unbraced $name fields in $ style formats [core] support unbraced $name fields in $ style formats Aug 15, 2026
@nhairs
nhairs merged commit b865bd3 into nhairs:main Aug 15, 2026
4 checks passed
nhairs pushed a commit that referenced this pull request Sep 23, 2026
Closes #74.

`parse()` reads escaped literals as fields: `%%(b)s` in a `%` format,
`{{b}}` in a `{` one. #69 fixed this for `$`; these two were left. `{`
also folds a conversion or format spec into the name, so
`"{levelname:>8} {message}"` — an ordinary stdlib format that
`StrFormatStyle.validate()` accepts — logs `{"levelname:>8": null,
"message": "hello"}`.

**Changes**
- `%`: skip `%%`, mirroring the `$` fix.
- `{`: use `string.Formatter`, which is what
`logging.StrFormatStyle.validate` parses the same string with. Covers
`{{`/`}}`, `!r`, `:>8`, and nested specs like `{levelname:>{width}}`.
`STYLE_STRING_FORMAT_REGEX` is kept but unused — say the word and I'll
drop it.

**Testing**
`pytest tests`: 224 passed, 218 before. Two new tests beside the
existing per-style ones. black/pylint/mypy/validate-pyproject clean.

Oracle: the substitution engine itself (`fmt % tracking_dict`,
`string.Formatter().parse`, `Template.get_identifiers`). Over 37 formats
`$` agreed 7/7 as a control; `%`+`{` diverged 13x before, 1 after — the
remainder is `"{}"`, which `validate()` skips too.

Reverting only the `core.py` change fails the 6 new cases and nothing
else. A regex-only alternative passes `{levelname:>8}` but reads `width`
out of `{levelname:>{width}}`; hence that case in the test.

One behaviour change: with `validate=False` a malformed `{` format now
raises at construction instead of silently yielding no fields.

Prepared with AI assistance; I reviewed and tested it locally.

---------

Co-authored-by: Dylan Pulver <dylanpulver@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
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.

StringTemplateStyle ($) does not support unbraced names

2 participants