samdark/yii2-psr-log-target

使用PSR-3兼容的日志记录器的Yii 2日志目标

资助包维护!
samdark
Patreon

安装次数1,170,679

依赖项: 8

建议者: 0

安全: 0

星标: 80

关注者: 9

分支: 15

公开问题: 3

类型:yii2-extension

1.1.4 2023-11-23 14:11 UTC

This package is auto-updated.

Last update: 2024-09-18 18:28:32 UTC


README

允许您使用任何PSR-3兼容的日志记录器(如Monolog)来处理日志。

Latest Stable Version Total Downloads Build Status Code Coverage Scrutinizer Quality Score

安装

composer require "samdark/yii2-psr-log-target"

使用方法

为了使用PsrTarget,您应该像以下这样配置您的log应用程序组件

// $psrLogger should be an instance of PSR-3 compatible logger.
// As an example, we'll use Monolog to send log to Slack.
$psrLogger = new \Monolog\Logger('my_logger');
$psrLogger->pushHandler(new \Monolog\Handler\SlackHandler('slack_token', 'logs', null, true, null, \Monolog\Logger::DEBUG));

return [
    // ...
    'bootstrap' => ['log'],    
    // ...    
    'components' => [
        // ...        
        'log' => [
            'targets' => [
                [
                    'class' => 'samdark\log\PsrTarget',
                    'logger' => $psrLogger,
                    
                    // It is optional parameter. The message levels that this target is interested in.
                    // The parameter can be an array.
                    'levels' => ['info', yii\log\Logger::LEVEL_WARNING, Psr\Log\LogLevel::CRITICAL],
                    // It is optional parameter. Default value is false. If you use Yii log buffering, you see buffer write time, and not real timestamp.
                    // If you want write real time to logs, you can set addTimestampToContext as true and use timestamp from log event context.
                    'addTimestampToContext' => true,
                ],
                // ...
            ],
        ],
    ],
];

标准用法

Yii::info('Info message');
Yii::error('Error message');

使用PSR日志级别

Yii::getLogger()->log('Critical message', Psr\Log\LogLevel::CRITICAL);
Yii::getLogger()->log('Alert message', Psr\Log\LogLevel::ALERT);

使用日志中的原始时间戳

// $psrLogger should be an instance of PSR-3 compatible logger.
// As an example, we'll use Monolog to send log to Slack.
$psrLogger = new \Monolog\Logger('my_logger');

$psrLogger->pushProcessor(function($record) {
    if (isset($record['context']['timestamp'])) {
        $dateTime = DateTime::createFromFormat('U.u', $record['context']['timestamp']);
        $timeZone = $record['datetime']->getTimezone();
        $dateTime->setTimezone($timeZone);
        $record['datetime'] = $dateTime;

        unset($record['context']['timestamp']);
    }

    return $record;
});

您可以使用PsrMessage代替常规字符串消息以添加自定义上下文。

标准用法

Yii::error(new \samdark\log\PsrMessage("Critical message", [
    'custom' => 'context',
    'key' => 'value',
]));

使用PSR日志级别

Yii::getLogger()->log(new \samdark\log\PsrMessage("Critical message", [
    'important' => 'context'
]), Psr\Log\LogLevel::CRITICAL);

使用PSR-3日志消息处理

$psrLogger = new \Monolog\Logger('my_logger');
$psrLogger->pushProcessor(new \Monolog\Processor\PsrLogMessageProcessor());
Yii::debug(new \samdark\log\PsrMessage("Greetings from {fruit}", [
    'fruit' => 'banana'
]));

运行测试

为了运行测试,请执行以下命令

composer install
./vendor/bin/phpunit