Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/PhpJsonLogger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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(),
Expand All @@ -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
);
}

Expand Down
22 changes: 19 additions & 3 deletions tests/Logger/SlackNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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'],
Expand Down