monolog WebProcessor 在webman中无法获取ip等参数,日志中该如何获取请求参数等内容呢

听泪无言

monolog WebProcessor 在webman中无法获取ip等参数
protected $extraFields = [
'url' => 'REQUEST_URI',
'ip' => 'REMOTE_ADDR',
'http_method' => 'REQUEST_METHOD',
'server' => 'SERVER_NAME',
'referrer' => 'HTTP_REFERER',
];

1589 2 0
2个回答

Tinywan

你这是在哪里设置的?

  • 听泪无言 2022-03-03

    support\Log.php中channel方法里加了一行$logger->pushProcessor(new WebProcessor());

  • 听泪无言 2022-03-03
    public static function channel($name = 'default')
    {
        if (!static::$_instance) {
            $configs = config('log', []);
            foreach ($configs as $channel => $config) {
                $logger = static::$_instance[$channel] = new Logger($channel);
                foreach ($config['handlers'] as $handler_config) {
                    $handler = new $handler_config['class'](... \array_values($handler_config['constructor']));
                    if (isset($handler_config['formatter'])) {
                        $formatter = new $handler_config['formatter']['class'](... \array_values($handler_config['formatter']['constructor']));
                        $handler->setFormatter($formatter);
                    }
                    $logger->pushHandler($handler);
                    $logger->pushProcessor(new WebProcessor());
                }
            }
        }
        return static::$_instance[$name];
    }
听泪无言

已解决 重写了WebProcessor
如有问题请指正,谢谢
代码如下
<?php

namespace support;

class WebProcessor extends \Monolog\Processor\WebProcessor
{
protected $extraFields = [
'url' => 'REQUEST_URI',
'ip' => 'REMOTE_ADDR',
'http_method' => 'REQUEST_METHOD',
'header' => 'REQUEST_HEADER',
'get' => 'REQUEST_GET',
'post' => 'REQUEST_POST',
];

public function __invoke(array $record): array
{
    $this->serverData['REQUEST_URI'] = request()->url();
    $this->serverData['REMOTE_ADDR'] = request()->getRealIp();
    $this->serverData['REQUEST_METHOD'] = request()->method();
    $this->serverData['REQUEST_HEADER'] = request()->header();
    $this->serverData['REQUEST_GET'] = request()->get();
    $this->serverData['REQUEST_POST'] = request()->post();
    if (!isset($this->serverData['REQUEST_URI'])) {
        return $record;
    }
    $record['extra'] = $this->appendExtraFields($record['extra']);

    return $record;
}

private function appendExtraFields(array $extra): array
{
    foreach ($this->extraFields as $extraName => $serverName) {
        $extra[$extraName] = $this->serverData[$serverName] ?? null;
    }

    return $extra;
}

}

  • 暂无评论
年代过于久远,无法发表回答
×
🔝