diff --git a/src/PhpJsonLogger/Logger.php b/src/PhpJsonLogger/Logger.php index 5649591..b8cce0f 100644 --- a/src/PhpJsonLogger/Logger.php +++ b/src/PhpJsonLogger/Logger.php @@ -5,6 +5,7 @@ use Monolog\Handler\SlackHandler; use Monolog\Logger as MonoLogger; use Monolog\Processor\IntrospectionProcessor; +use Monolog\Processor\WebProcessor; use Ramsey\Uuid\Uuid; /** @@ -82,16 +83,6 @@ public function __construct(LoggerBuilder $builder) $rotating ]; - if ($builder->getSlackHandler() instanceof SlackHandler) { - $slack = $builder->getSlackHandler(); - $slack->setFormatter($formatter); - - array_push( - $handlers, - $slack - ); - } - $introspection = new IntrospectionProcessor( $this->getLogLevel(), $builder->getSkipClassesPartials(), @@ -105,10 +96,30 @@ public function __construct(LoggerBuilder $builder) return $record; }; + $processors = [ + $introspection, + $extraRecords, + ]; + + if ($builder->getSlackHandler() instanceof SlackHandler) { + $slack = $builder->getSlackHandler(); + $slack->setFormatter($formatter); + + array_push( + $handlers, + $slack + ); + + $webProcessor = new WebProcessor(); + $webProcessor->addExtraField('server_ip_address', 'SERVER_ADDR'); + $webProcessor->addExtraField('user_agent', 'HTTP_USER_AGENT'); + array_push($processors, $webProcessor); + } + $this->monologInstance = new MonoLogger( $this->getChannel(), $handlers, - [$introspection, $extraRecords] + $processors ); } diff --git a/tests/Logger/SlackNotificationTest.php b/tests/Logger/SlackNotificationTest.php index f7e839b..77fc012 100644 --- a/tests/Logger/SlackNotificationTest.php +++ b/tests/Logger/SlackNotificationTest.php @@ -53,12 +53,28 @@ public function notificationToSlack() $slackHandlerBuilder = new SlackHandlerBuilder($slackToken, $slackChannel); $slackHandlerBuilder->setLevel(LoggerBuilder::CRITICAL); + $_SERVER['REQUEST_URI'] = '/tests/notifications'; + $_SERVER['REMOTE_ADDR'] = '192.168.10.10'; + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_SERVER['SERVER_NAME'] = 'cat-moko.localhost'; + $_SERVER['HTTP_REFERER'] = 'https://github.com/nekonomokochan/php-json-logger/issues/50'; + $_SERVER['SERVER_ADDR'] = '10.0.0.11'; + $_SERVER['HTTP_USER_AGENT'] = 'Chrome'; + $loggerBuilder = new LoggerBuilder(); $loggerBuilder->setFileName($this->outputFileBaseName); $loggerBuilder->setSlackHandler($slackHandlerBuilder->build()); $logger = $loggerBuilder->build(); $logger->critical($exception, $context); + unset($_SERVER['REQUEST_URI']); + unset($_SERVER['REMOTE_ADDR']); + unset($_SERVER['REQUEST_METHOD']); + unset($_SERVER['SERVER_NAME']); + unset($_SERVER['HTTP_REFERER']); + unset($_SERVER['SERVER_ADDR']); + unset($_SERVER['HTTP_USER_AGENT']); + $resultJson = file_get_contents($this->outputFileName); $resultArray = json_decode($resultJson, true); @@ -72,10 +88,10 @@ public function notificationToSlack() 'channel' => 'PhpJsonLogger', 'trace_id' => $logger->getTraceId(), 'file' => __FILE__, - 'line' => 60, + 'line' => 68, 'context' => $context, - 'remote_ip_address' => '127.0.0.1', - 'user_agent' => 'unknown', + 'remote_ip_address' => '192.168.10.10', + 'user_agent' => 'Chrome', 'datetime' => $resultArray['datetime'], 'timezone' => date_default_timezone_get(), 'process_time' => $resultArray['process_time'],