monolog WebProcessor 在webman中無法獲取ip等參數(shù)
protected $extraFields = [
'url' => 'REQUEST_URI',
'ip' => 'REMOTE_ADDR',
'http_method' => 'REQUEST_METHOD',
'server' => 'SERVER_NAME',
'referrer' => 'HTTP_REFERER',
];
你這是在哪里設(shè)置的?
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;
}
}