【发布时间】:2020-08-29 17:41:51
【问题描述】:
对于 Laravel 5.7 应用,我想在默认日志前缀中添加一些额外的信息。
当前的日志条目如下所示:
[2020-05-13 13:07:42] production.INFO: Information here...
我想扩展前缀来实现:
[2020-05-13 13:07:42] PREFIX.production.INFO: Information here...
PREFIX 来自环境变量,LOG_PREFIX:
LOG_CHANNEL=daily
LOG_PREFIX=PREFIX
我不介意在自定义格式化程序中使用正则表达式将 production.INFO 更改为 PREFIX.production.INFO。
我在日志频道中添加了一个水龙头:
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 7,
'tap' => [App\Logging\CustomizeFormatter::class],
]
我在 CustomizeFormatter 类的调用方法中遇到问题:
class CustomizeFormatter
{
/**
* Customize the given logger instance.
*
* @param \Illuminate\Log\Logger $logger
* @return void
*/
public function __invoke($logger)
{
// What to add here?
}
}
那里可以用什么来做我需要的事情?
这是走错路了吗?
谢谢。
使用在此处找到的答案https://stackoverflow.com/a/34289039/121946 我可以使用以下命令在消息末尾添加前缀作为额外信息:
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
$handler->pushProcessor(function ($record) {
$record['extra']['prefix'] = env('LOG_PREFIX');
return $record;
});
}
}
查看$record,我没有看到完整的日志文本,只有自定义部分:
array:7 [▼
"message" => "Information here..."
"context" => []
"level" => 200
"level_name" => "INFO"
"channel" => "production"
"datetime" => DateTime @1589377784 {#953 ▶}
"extra" => []
]
【问题讨论】:
标签: laravel laravel-5.7 monolog