yii1tech / psr-log
为 Yii1 应用程序提供支持 PSR 兼容的日志记录器
Requires
- php: >=7.1
- psr/log: >=1.0.0
- yiisoft/yii: ~1.1.0
Requires (Dev)
- phpunit/phpunit: ^6.0 || ^7.0 || ^8.0 || ^9.3 || ^10.0.7
This package is auto-updated.
Last update: 2024-08-28 09:35:33 UTC
README
Yii1 PSR 日志扩展
此扩展允许与 Yii1 的 PSR 兼容日志记录器集成。它特别允许使用 Monolog 日志记录器。
有关许可证信息,请检查 LICENSE 文件。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令:
php composer.phar require --prefer-dist yii1tech/psr-log
或者
"yii1tech/psr-log": "*"
将以下内容添加到您的 composer.json 文件的 "require" 部分:
用法
此扩展允许与 Yii1 的 PSR 兼容日志记录器集成。它为此提供了一些工具。请根据您的具体需求选择合适的工具。
将 PSR 日志记录器包装到 Yii 日志记录器中
PSR 日志记录器在 Yii 应用程序中使用的最常见用例是使用第三方日志库,如 Monolog。这可以通过使用 \yii1tech\psr\log\Logger
实例作为 Yii 日志记录器来实现。在创建应用程序实例之前,应将其实例传递给 \Yii::setLogger()
。
应用程序入口脚本示例
<?php // file '/public/index.php' require __DIR__ . '../vendor/autoload.php'; // ... // set custom logger: Yii::setLogger( \yii1tech\psr\log\Logger::new() ->setPsrLogger(function () { // use Monolog as internal PSR logger: $log = new \Monolog\Logger('yii'); $log->pushHandler(new \Monolog\Handler\StreamHandler('path/to/your.log', \Monolog\Level::Warning)); return $log; }) ->enableYiiLog(true) // whether to continue passing logs to standard Yii log mechanism or not ); // create and run Yii application: Yii::createWebApplication($config)->run();
\yii1tech\psr\log\Logger
将通过 Yii::log()
记录的所有消息传递给相关的 PSR 日志记录器,并按照其自身的内部逻辑存储它们。
此外,使用 \yii1tech\psr\log\Logger
还为您提供了一些额外的优势
- 它允许使用
\Psr\log\LogLevel
常量来指定日志级别,而不是使用\CLogger
中的那些。 - 它允许将日志上下文作为数组传递给
Yii::log()
方法的第三个参数,并将其保存到 Yii 日志中。
例如
<?php use Psr\log\LogLevel; Yii::log('psr message', LogLevel::INFO, 'psr-category'); // same as `Yii::log('psr message', CLogger::LEVEL_INFO, 'psr-category');` Yii::log('context message', LogLevel::INFO, ['category' => 'context-category']); // same as `Yii::log('context message', CLogger::LEVEL_INFO, 'context-category');` Yii::log('context message', LogLevel::INFO, [ 'foo' => 'bar', // specifying log context, which will be passed to the related PSR logged, and added as JSON to the Yii log message, if it is enabled ]); try { // ... } catch (\Throwable $exception) { Yii::log('context exception', LogLevel::ERROR, [ 'exception' => $exception, // exception data such as class, message, file, line and so on will be logged ]); }
您还可以指定一个全局日志上下文,该上下文应与每条消息一起写入。例如
<?php // set custom logger: Yii::setLogger( \yii1tech\psr\log\Logger::new() ->setPsrLogger(function () { // ... }) ->withGlobalContext(function () { $context = []; // log remote IP address if available: if (!empty($_SERVER['REMOTE_ADDR'])) { $context['ip'] = $_SERVER['REMOTE_ADDR']; } // log authenticated user ID, if available: $webUser = Yii::app()->getComponent('user', false); if ($webUser !== null && !$webUser->getIsGuest()) { $context['auth_user_id'] = $webUser->getId(); } return $context; }) );
PSR 日志路由
如果需要将日志传递到 PSR 日志记录器,则不需要 \yii1tech\psr\log\Logger
。作为替代方案,您可以将 \yii1tech\psr\log\PsrLogRoute
日志路由添加到标准的 Yii "log" 组件。
应用程序配置示例
<?php return [ 'components' => [ 'log' => [ 'class' => \CLogRouter::class, 'routes' => [ [ 'class' => \yii1tech\psr\log\PsrLogRoute::class, 'psrLogger' => function () { $log = new \Monolog\Logger('yii'); $log->pushHandler(new \Monolog\Handler\StreamHandler('path/to/your.log', \Monolog\Level::Warning)); return $log; }, ], // ... ], ], // ... ], // ... ];
注意:即使您使用
\yii1tech\psr\log\Logger
作为 Yii 日志记录器,此\yii1tech\psr\log\PsrLogRoute
也将无法正确处理传递的日志上下文。
将 Yii 日志记录器包装到 PSR 日志记录器中
除了引导永久日志存储之外,与 PSR 日志记录器相关的另一个用例。有时第三方库可能需要将 PSR 日志记录器实例传递给它们才能正常工作。例如,想象我们有一个用于“守护程序”应用程序的第三方库正在运行
<?php namespace vendor\daemon; use Psr\Log\LoggerInterface; class DaemonApplication { public function __construct(LoggerInterface $logger) { // .... } }
您可以使用 \yii1tech\psr\log\PsrLogger
将标准的 Yii 日志机制包装成 PSR 接口。
应用程序配置示例
<?php return [ 'components' => [ \Psr\Log\LoggerInterface::class => [ 'class' => \yii1tech\psr\log\PsrLogger::class, ], // ... ], // ... ];
现在,当与我们的示例外部“守护程序”应用程序一起工作时,我们可以使用以下代码
<?php use Psr\Log\LoggerInterface; use vendor\daemon\DaemonApplication; $daemon = new DaemonApplication(Yii::app()->getComponent(LoggerInterface::class)); // ...
注意:为了正确处理日志上下文,应与
\yii1tech\psr\log\Logger
一起使用\yii1tech\psr\log\PsrLogger
。