[GH-ISSUE #944] Does NormalizeFormatter->normalize() ever receive anything other than an array? #376

Closed
opened 2026-03-04 02:14:33 +03:00 by kerem · 1 comment
Owner

Originally created by @rosshettel on GitHub (Mar 14, 2017).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/944

Trying to fix up our app's logging set up, and wanted to set up Exceptions to be formatted nicely in Slack. I'm having trouble understanding how to do so though.

So if I do $log->error($e);, the Exception gets cast to a string here, which makes sense.

Or if I try $log->error('Exception happened', $e);, I get an error because context is expected to be an array here.

But I noticed in NormalizerFormatter that it has a special case to normalize an Exception, which sounds great. But as far as I can tell and with my testing, it will only ever get passed an array - so how do I properly log an exception so that it does so?

Originally created by @rosshettel on GitHub (Mar 14, 2017). Original GitHub issue: https://github.com/Seldaek/monolog/issues/944 Trying to fix up our app's logging set up, and wanted to set up Exceptions to be formatted nicely in Slack. I'm having trouble understanding how to do so though. So if I do `$log->error($e);`, the Exception gets cast to a string [here](https://github.com/Seldaek/monolog/blob/1e044bc4b34e91743943479f1be7a1d5eb93add0/src/Monolog/Logger.php#L323), which makes sense. Or if I try `$log->error('Exception happened', $e);`, I get an error because context is expected to be an array [here](https://github.com/Seldaek/monolog/blob/1e044bc4b34e91743943479f1be7a1d5eb93add0/src/Monolog/Logger.php#L614). But I noticed in NormalizerFormatter that it has a [special case to normalize an Exception](https://github.com/Seldaek/monolog/blob/1e044bc4b34e91743943479f1be7a1d5eb93add0/src/Monolog/Formatter/NormalizerFormatter.php#L92), which sounds great. But as far as I can tell and with my testing, it will only ever get passed an array - so how do I properly log an exception so that it does so?
kerem closed this issue 2026-03-04 02:14:33 +03:00
Author
Owner

@Seldaek commented on GitHub (Mar 17, 2017):

Logging exceptions, as per PSR-3, should be done as $logger->error('Something something', ['exception' => $e]);

Generally speaking, context information should be an array with meaningful keys and their associated data I would say.

<!-- gh-comment-id:287479148 --> @Seldaek commented on GitHub (Mar 17, 2017): Logging exceptions, as per [PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md), should be done as `$logger->error('Something something', ['exception' => $e]);` Generally speaking, context information should be an array with meaningful keys and their associated data I would say.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#376
No description provided.