This package have PSR-3 compatible log message interface.
It can be useful for implementation messages that can be easily logged via \Psr\Log\LoggerInterface::log
method.
You can install the package via composer:
composer require nomad42/loggable
Step 1.
Implement interface in your exception.
class MyAppException extends Exception implements Loggable
{
public function getLevel(): string
{
return LogLevel::CRITICAL;
}
public function getContext(): array
{
return ['exception' => $this];
}
}
Step 2.
At your error handler add one extra catch block.
$logger = new NullLogger();
try {
throw new MyAppException('Oh no!');
} catch (Loggable $exception) { // <- this block; yes, it can be caught
$logger->log(
$exception->getLevel(),
$exception->getMessage(),
$exception->getContext(),
);
} catch (Exception $exception) {
$logger->error('Oh no!', ['exception' => $exception]);
}
$exception = new \Exception('Oh no!');
$logger = new \Psr\Log\NullLogger();
LogMessage::makeFromException($exception)->logByLogger($logger);
(new LogMessage(
PsrLogLevel::INFO,
'Cron task end successfully',
['metrics' => $metrics],
))->logByLogger($logger);
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.