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

Logging throwables with all their details by using PSR-3 interface #1535

Closed
boesing opened this issue Feb 16, 2021 · 1 comment
Closed

Logging throwables with all their details by using PSR-3 interface #1535

boesing opened this issue Feb 16, 2021 · 1 comment
Labels

Comments

@boesing
Copy link
Contributor

boesing commented Feb 16, 2021

Monolog version 2

Hey there,

is there some kind of "best practice" to format throwables before passing to LoggerInterface::* method?

As of monolog, passing an instance of Throwable is "okay", as LoggerInterface::log does not have any native typehint and thus its okay to pass anything and within the NormalizerFormatter that Throwable would be converted (that formatter is used in almost all handlers).

But as I've seen these "by-law" version bumps on e.g. PSR-6, I think there will be a new Version soon, which enforces string typehint.

Are there any suggestions on how to pre-format a Throwable or probably monolog might provide a new Interface which allows to log throwables and using the PSR-3 methods?

Maybe something like this?

interface ThrowableLoggerInterface extends \Psr\Log\LoggerInterface
{
    public function logThrowable(Throwable $throwable, string $level): void;
}
@Seldaek
Copy link
Owner

Seldaek commented Apr 4, 2021

The right way to pass throwables IMO is in the $context info as ['exception' => $e]. The message should be a string. This is what PSR-3 advocates too, and monolog will normalize them correctly depending on user preferences, including stack trace or not etc. The caller should not be able to decide this, it should pass the Throwable object together with a meaningful message indicating what happened and that's it.

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