nomad42 / loggable
PSR-3 兼容的日志消息接口
0.2.1
2023-08-03 09:34 UTC
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: 2024-09-09 19:39:22 UTC
README
此包具有与 PSR-3 兼容的日志消息接口。它可以用于实现可以通过 \Psr\Log\LoggerInterface::log
方法轻松记录的消息。
安装
您可以通过 composer 安装此包。
composer require nomad42/loggable
使用
通过记录异常来标准化工作流程。
步骤 1.
在你的异常中实现接口。
class MyAppException extends Exception implements Loggable { public function getLevel(): string { return LogLevel::CRITICAL; } public function getContext(): array { return ['exception' => $this]; } }
步骤 2.
在错误处理程序中添加一个额外的 catch 块。
$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
变更日志
有关最近更改的更多信息,请参阅 变更日志。
贡献
有关详细信息,请参阅 贡献指南。
安全漏洞
请查看我们的安全策略,了解如何报告安全漏洞: 安全政策。
致谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。