-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for logging exception instances #1936
Comments
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 The recommended usage of PSR-3 is to pass it as the |
I know, I'm talking just about
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. |
While I see this technically could be done, I do believe the explicit 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 So I usually would encourage |
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,
Throwable
extendsStringable
, so instances can be used as$message
parameter.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
Throwable
and$message
would 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 $throwable
as a constructor property ofLogRecord
, so it's type-safe in handlers and easier to access, also when logging messages, we could check forThrowable
instance of$context['exception']
and add it to LogRecord as that new$throwable
parameter, to ensure better backwards compatibility.The text was updated successfully, but these errors were encountered: