mirror of
https://github.com/Seldaek/monolog.git
synced 2026-04-26 08:05:53 +03:00
[GH-ISSUE #1936] Support for logging exception instances #834
Labels
No labels
Bug
Documentation
Feature
Needs Work
Support
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/monolog#834
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @delacry on GitHub (Jan 29, 2025).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1936
Hi, I would love to add better support for logging exception instances, for example
Logger::info(new Exception()), it's useful in try-catch block where you don't want to throw an error 500, but just log the exception. It's useful later in handlers, where some library can generate HTML dump of that exception (like Tracy).Since PHP 8.0,
ThrowableextendsStringable, so instances can be used as$messageparameter.Currently, there is a problem, that exception instance is lost in Logger::log, because it's converted to string when passing to addRecord.
It would be great if there was instanceof check for
Throwableand$messagewould then be added to$context['exception'](if not set previously).Maybe it would be better (but we would also add it to context for BC) to have
Throwable $throwableas a constructor property ofLogRecord, so it's type-safe in handlers and easier to access, also when logging messages, we could check forThrowableinstance of$context['exception']and add it to LogRecord as that new$throwableparameter, to ensure better backwards compatibility.@stof commented on GitHub (Jan 29, 2025):
Stringable are supported as message by being casted to string, not by being passed untransformed inside the whole Monolog stack (PSR-3 has added a union type for
\Stringablein the interface only because of the behavior ofstrict_typesin PHP, AFAICT).The recommended usage of PSR-3 is to pass it as the
exceptionkey of the context (which implementation are expected to handle fine). Doing that in your try-catch block would make your code compatible with any PSR-3 logger.@delacry commented on GitHub (Jan 29, 2025):
I know, I'm talking just about
Loggerclass, wherelog()acceptsstring|Stringable, and we can then check forThrowableinstance and add it to$context['exception']or add it toLogRecordas another property so handler implementations can access it with type-safety.True, but when someone is using the monolog library, he could log exceptions more easily. Instead of typing:
They would type:
So handlers receive the exception instance.
@Seldaek commented on GitHub (Mar 16, 2025):
While I see this technically could be done, I do believe the explicit
$this->logger->info($e, ['exception' => $e]);is better as it is more interoperable and it's not really that long to type.. so I think I'd rather not add a shortcut here.I'd also argue that in most cases logging some more human-readable details and context in the message is usually very valuable, and logging
$e->getMessage()as log message tends to result in unactionable logs because exception messages are garbage in many libs.So I usually would encourage
$this->logger->error('Failed to do x', ['exception' => $e]);