nomad42 / loggable
PSR-3 compatible log message interface
Fund package maintenance!
NoMad42
Requires
- php: ^8.2
- psr/log: ^3.0
Requires (Dev)
- laravel/pint: ^1.2
- pestphp/pest: ^1.20
This package is auto-updated.
Last update: 2025-09-09 21:53:11 UTC
README
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.
Installation
You can install the package via composer:
composer require nomad42/loggable
Usage
Standardize the workflow with logging your exception.
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]); }
Wrap existing exception and log it immediately
$exception = new \Exception('Oh no!'); $logger = new \Psr\Log\NullLogger(); LogMessage::makeFromException($exception)->logByLogger($logger);
Log some data in easy way
(new LogMessage( PsrLogLevel::INFO, 'Cron task end successfully', ['metrics' => $metrics], ))->logByLogger($logger);
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.