作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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。我查看了“创建自定义 channel ”的文档,但没有看到任何明显的方法来实现上述行为。
错误发生于
@php artisan package: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.
我远不是这方面的专家,因此非常感谢您的帮助。
最佳答案
这是我解决这个问题的方法。
在 config\logging.php 中为驱动程序添加一个“tap”
'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;
});
}
}
}
关于php - Laravel 5.6 如何向所有日志记录添加数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52032203/
我是一名优秀的程序员,十分优秀!