fix(log): avoid Array to string conversion warning in SimpleFormatter#8
Conversation
Context values that are arrays or objects (e.g. extra fields passed through Horde::log()) triggered a PHP "Array to string conversion" warning when interpolated into the format string, which could mask or corrupt the intended log entry. Encode non-scalar values as JSON before interpolation instead.
ralflang
left a comment
There was a problem hiding this comment.
This doesn't look right. Which context exactly got provided as structured data? Usually these fields are not supposed to be logged this way.
|
Fair question — here is the concrete evidence and why I think the formatter needs the guard. Where this was observed Not constructed by me — it comes from a user's production log on a stock install (horde 6.0.0 / core 3.1.0 / imp 7.0.x), reported in horde/imp#88: horde/imp#88 (comment) Line 85 is the Why "these fields are not supposed to be logged this way" doesn't prevent the warning Agreed — structured context is not supposed to end up in the format string. But Why array context is legitimate input
Why it matters in practice The warning fires while a log line is being formatted. Depending on the error handler, that warning is fed back through the same logging pipeline and can suppress or garble the entry being written. In imp#88 that degraded the diagnostics we were trying to collect. Open to an alternative If you would rather have non-scalar values never appear in output at all, I am happy to change the patch to skip them the way |
Summary
SimpleFormatter::format()when a log context value is an array or object.Motivation
Reported while investigating horde/imp#88: a log entry with array context data triggered this warning, which — depending on the configured error handler — can swallow or corrupt the very log entry being written. That made it hard to get reliable diagnostics out of
horde.logfor that issue.Changes
SimpleFormatter::format()now JSON-encodes array/object context values before interpolating them into the format string, instead of relying on implicit(string)casting.Test plan
vendor/bin/phpunit test/Formatter/SimpleFormatterTest.php(18/18 passing)