oat-sa/lib-correlation-ids-monolog

OAT Correlation Ids Monolog 库

0.3.0 2022-01-13 12:57 UTC

This package is auto-updated.

Last update: 2024-09-17 14:12:54 UTC


README

PHP 库,用于基于 correlation ids 库 的 correlation ids monolog 处理器。

目录

安装

$ composer require oat-sa/lib-correlation-ids-monolog

原则

此库提供了一款现成的 monolog 处理器,可以扩展日志上下文,包含从 correlation ids 注册表 中获取的 correlation ids。

使用方法

CorrelationIdsMonologProcessor 推送到您的日志记录器,方法如下

<?php declare(strict_types=1);

use OAT\Library\CorrelationIds\Registry\CorrelationIdsRegistry;
use OAT\Library\CorrelationIds\Registry\CorrelationIdsRegistryInterface;
use OAT\Library\CorrelationIdsMonolog\Processor\CorrelationIdsMonologProcessor;
use Monolog\Logger;

/** @var CorrelationIdsRegistryInterface $registry */
$registry = new CorrelationIdsRegistry(...);

$logger = new Logger('channel-name');
$logger->pushProcessor(new CorrelationIdsMonologProcessor($registry));

...

$logger->info('log message'); // this log entry context will be automatically populated with the correlation ids.

注意:您可以通过提供自己的 CorrelationIdsHeaderNamesProviderInterface 实现并传递给 CorrelationIdsMonologProcessor 构造函数来自定义日志上下文键名。

测试

运行测试

$ vendor/bin/phpunit

注意:有关可用的测试套件,请参阅 phpunit.xml.dist