Skip to content

Stop JsonFormatter from encoding INF and NaN as 0 - #2078

Merged
Seldaek merged 1 commit into
Seldaek:mainfrom
kevin9327:json-formatter-non-finite-floats
Oct 5, 2026
Merged

Seldaek merged 1 commit into
Seldaek:mainfrom
kevin9327:json-formatter-non-finite-floats

Conversation

@kevin9327

Copy link
Copy Markdown
Contributor

JsonFormatter logs INF, -INF and NAN as 0:

$logger->warning('ratio', ['ratio' => INF, 'avg' => NAN]);
// {"message":"ratio","context":{"ratio":0,"avg":0},...}

JSON has no representation for these values. json_encode() fails on them, and because Utils::DEFAULT_JSON_FLAGS includes JSON_PARTIAL_OUTPUT_ON_ERROR, each one is replaced with 0. That reads like a real measurement, so the log ends up misleading rather than visibly broken.

NormalizerFormatter::normalize() already maps these to the strings 'INF', '-INF' and 'NaN' (from #523), and LineFormatter gets that for free. JsonFormatter overrides normalize() and returns scalars untouched, so it missed out. This PR hands non-finite floats to the parent, so JsonFormatter gives the same output as the other formatters. All other scalars are still returned as they are.

I added a JsonFormatter test covering INF, -INF, NAN, a nested INF, and a normal float. It fails on main with 0 is identical to 'INF'. The full PHPUnit suite and PHPStan pass.

@Seldaek Seldaek added this to the 3.x milestone Oct 5, 2026
@Seldaek
Seldaek merged commit 3bed304 into Seldaek:main Oct 5, 2026
20 checks passed
@Seldaek

Seldaek commented Oct 5, 2026

Copy link
Copy Markdown
Owner

Thanks

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.

2 participants