Laravel MonoLog、pushProcessor 不是 Logging Addition 属性
Laravel MonoLog, pushProcessor not Logging Addition attributes
我希望能够在我的日志记录中添加一个唯一的 ID (Uid)。
在 示例 1 中: 依赖于下面的 config/logging.php
和 ProcessorTap
文件未按预期工作。日志记录配置为使用 stdout
,它指的是 ProcessorTap
class 应该添加一个 Uid,当创建日志语句时(根据 UidProcessor
)
示例 2: 使用纯 Mono classes 按预期工作。
为什么示例 1 不将 Uid
添加到日志中,而 laravel ("laravel/framework": "5.7.*")
也应该使用 Monolog classes?
示例 1:调用此 api 时,Log::info('test')
的输出不包括 UiD
Route::get('/test', function () {
Log::info('test'); //output = [2020-03-24 04:51:16] local.INFO: test
});
config/logging.php:
'default' => env('LOG_CHANNEL', 'stdout'), //.env LOG_CHANNEL=stdout
'stdout' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'with' => [
'stream' => 'php://stdout',
],
'tap' => [
ProcessorTap::class,
],
]
ProcessorTap:
use Monolog\Processor\UidProcessor;
class ProcessorTap
{
/**
* Customize the given logger instance.
*
* @param \Illuminate\Log\Logger $logger
* @return void
*/
public function __invoke($logger)
{
$logger->pushProcessor(new UidProcessor());
}
}
示例 2:正确使用 Uid (a484a6729e14996c0af1)
添加到 $logger->info('test')
的日志中
use Monolog\Logger;
use Monolog\Processor\UidProcessor;
Route::get('/test', function () {
$logger = new Logger('main');
$logger->pushProcessor(new UidProcessor(20));
$logger->info('test'); // output = [2020-03-24 04:57:26] main.INFO: test [] {"uid":"a484a6729e14996c0af1"}
});
这可能是 laravel (5.7)/mono 版本特定的问题,但我能够解决通过处理程序迭代并调用 pushProcessor
use Monolog\Processor\UidProcessor;
class ProcessorTap
{
/**
* Customize the given logger instance.
*
* @param \Illuminate\Log\Logger $logger
* @return void
*/
public function __invoke($logger)
{
collect($logger->getHandlers())->each(function ($handler) {
$handler->pushProcessor(new UidProcessor());
});
}
}
我希望能够在我的日志记录中添加一个唯一的 ID (Uid)。
在 示例 1 中: 依赖于下面的 config/logging.php
和 ProcessorTap
文件未按预期工作。日志记录配置为使用 stdout
,它指的是 ProcessorTap
class 应该添加一个 Uid,当创建日志语句时(根据 UidProcessor
)
示例 2: 使用纯 Mono classes 按预期工作。
为什么示例 1 不将 Uid
添加到日志中,而 laravel ("laravel/framework": "5.7.*")
也应该使用 Monolog classes?
示例 1:调用此 api 时,Log::info('test')
的输出不包括 UiD
Route::get('/test', function () {
Log::info('test'); //output = [2020-03-24 04:51:16] local.INFO: test
});
config/logging.php:
'default' => env('LOG_CHANNEL', 'stdout'), //.env LOG_CHANNEL=stdout
'stdout' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'with' => [
'stream' => 'php://stdout',
],
'tap' => [
ProcessorTap::class,
],
]
ProcessorTap:
use Monolog\Processor\UidProcessor;
class ProcessorTap
{
/**
* Customize the given logger instance.
*
* @param \Illuminate\Log\Logger $logger
* @return void
*/
public function __invoke($logger)
{
$logger->pushProcessor(new UidProcessor());
}
}
示例 2:正确使用 Uid (a484a6729e14996c0af1)
添加到 $logger->info('test')
use Monolog\Logger;
use Monolog\Processor\UidProcessor;
Route::get('/test', function () {
$logger = new Logger('main');
$logger->pushProcessor(new UidProcessor(20));
$logger->info('test'); // output = [2020-03-24 04:57:26] main.INFO: test [] {"uid":"a484a6729e14996c0af1"}
});
这可能是 laravel (5.7)/mono 版本特定的问题,但我能够解决通过处理程序迭代并调用 pushProcessor
use Monolog\Processor\UidProcessor;
class ProcessorTap
{
/**
* Customize the given logger instance.
*
* @param \Illuminate\Log\Logger $logger
* @return void
*/
public function __invoke($logger)
{
collect($logger->getHandlers())->each(function ($handler) {
$handler->pushProcessor(new UidProcessor());
});
}
}