Skip to content
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

Open
Rixafy opened this issue Jan 29, 2025 · 2 comments
Open

Support for logging exception instances #1936

Rixafy opened this issue Jan 29, 2025 · 2 comments
Labels

Comments

@Rixafy
Copy link
Contributor

Rixafy commented Jan 29, 2025

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.

@Rixafy Rixafy added the Feature label Jan 29, 2025
@stof
Copy link
Contributor

stof commented 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 \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.

@Rixafy
Copy link
Contributor Author

Rixafy commented Jan 29, 2025

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:

$this->logger->info($e, ['exception' => $e]);

They would type:

$this->logger->info($e);

So handlers receive the exception instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants