How to add data to all log records in Laravel?回答了如何在Laravel 5.5及更早版本中向所有日志记录添加数据。例如将以下内容添加到AppServiceProvider :: register():
$monolog = \Log::getMonolog();
$monolog->pushProcessor(function ($record) {
$record['extra']['ip'] = \Request::getClientIp();
$record['extra']['path'] = \Request::path();
return $record;
});
这不适用于Laravel 5.6。我浏览了有关“创建自定义渠道”的文档,但没有发现任何明显的方式来获得上述行为。
错误发生于
@php artisan软件包:discover -v
Symfony\Component\Debug\Exception\FatalThrowableError : Call to undefined method Monolog\Logger::getMonolog()
at ...\vendor\laravel\framework\src\Illuminate\Log\Logger.php: 273
269: * @return mixed
270: */
271: public function __call($method, $parameters)
272: {
273: return $this->logger->{$method}(...$parameters);
274: }
275: }
276:
Exception trace:
1 Illuminate\Log\Logger::__call("getMonolog", [])
...\vendor\laravel\framework\src\Illuminate\Log\LogManager.php : 609
2 Illuminate\Log\LogManager::__call("getMonolog", [])
...\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php : 223
Please use the argument -v to see more details.
我在这方面还远非专家,因此可以提供任何帮助。
答案 0 :(得分:3)
这是我解决这个问题的方式。
在config \ logging.php中向驱动程序添加“敲击”
'single' => [
'driver' => 'single',
'tap' => [App\Logging\CustomizeFormatter::class],
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
]
创建App \ Logging \ CustomizeFormatter:
namespace App\Logging;
class CustomizeFormatter
{
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
$handler->pushProcessor(function ($record) {
$record['extra']['ip'] = \Request::getClientIp();
$record['extra']['path'] = \Request::path();
return $record;
});
}
}
}