You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 extends Stringable, 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 of LogRecord, so it's type-safe in handlers and easier to access, also when logging messages, we could check for Throwable 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:
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 \Stringable in the interface only because of the behavior of strict_types in PHP, AFAICT).
The recommended usage of PSR-3 is to pass it as the exception key 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.
Stringable are supported as message by being casted to string, not by being passed untransformed inside the whole Monolog stack
I know, I'm talking just about Logger class, where log() accepts string|Stringable, and we can then check for Throwable instance and add it to $context['exception'] or add it to LogRecord as another property so handler implementations can access it with type-safety.
The recommended usage of PSR-3 is to pass it as the exception key 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.
True, but when someone is using the monolog library, he could log exceptions more easily. Instead of typing:
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: