gpt4 book ai didi

php - Laravel 5.6 如何向所有日志记录添加数据?

转载 作者:行者123 更新时间:2023-12-04 14:31:27 25 4
gpt4 key购买 nike

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.

我远不是这方面的专家,因此非常感谢您的帮助。

最佳答案

这是我解决这个问题的方法。

  1. 在 .env 中,使用“堆栈 channel ”进行日志记录(即“LOG_CHANNEL=stack”)
  2. 在 config\logging.php 中为驱动程序添加一个“tap”

    'single' => [
    'driver' => 'single',
    'tap' => [App\Logging\CustomizeFormatter::class],
    'path' => storage_path('logs/laravel.log'),
    'level' => 'debug',
    ]
  3. 创建 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com