diff --git a/Logging/src/LogMessageProcessor.php b/Logging/src/LogMessageProcessor.php new file mode 100644 index 000000000000..94d97423c08b --- /dev/null +++ b/Logging/src/LogMessageProcessor.php @@ -0,0 +1,33 @@ +formatter = new NormalizerFormatter(); + $this->processor = new PsrLogMessageProcessor(); + } + + public function processLogMessage($message, $context) + { + $processor = $this->processor; + + return $processor([ + 'message' => (string) $message, + 'context' => $this->formatter->format($context) + ]); + } +} diff --git a/Logging/src/LogMessageProcessor/MonologV3MessageProcessor.php b/Logging/src/LogMessageProcessor/MonologV3MessageProcessor.php new file mode 100644 index 000000000000..f7417e242f40 --- /dev/null +++ b/Logging/src/LogMessageProcessor/MonologV3MessageProcessor.php @@ -0,0 +1,60 @@ +processor = new PsrLogMessageProcessor(); + } + + public function processLogMessage($message, $context) + { + // The datetime, channel, and level are required but not relevant here + $logRecord = new LogRecord( + new \DateTimeImmutable(), + 'channel', + Level::Info, + (string) $message, + $context + ); + + $processor = $this->processor; + $processed = $processor($logRecord); + + return [ + 'message' => $processed['message'], + 'context' => $processed['context'], + ]; + } +} diff --git a/Logging/src/LogMessageProcessorFactory.php b/Logging/src/LogMessageProcessorFactory.php new file mode 100644 index 000000000000..b9419b2d4004 --- /dev/null +++ b/Logging/src/LogMessageProcessorFactory.php @@ -0,0 +1,47 @@ +logger = $logger; $this->logName = $logger->name(); + $this->logMessageProcessor = LogMessageProcessorFactory::build(); $this->messageKey = $messageKey ?: 'message'; $this->metadataProvider = isset($options['metadataProvider']) ? $options['metadataProvider'] @@ -383,12 +387,7 @@ public function log($level, $message, array $context = []) unset($context['stackdriverOptions']); } - $formatter = new NormalizerFormatter(); - $processor = new PsrLogMessageProcessor(); - $processedData = $processor([ - 'message' => (string) $message, - 'context' => $formatter->format($context) - ]); + $processedData = $this->logMessageProcessor->processLogMessage($message, $context); $jsonPayload = [$this->messageKey => $processedData['message']]; // Adding labels for log request correlation. diff --git a/Logging/tests/Unit/PsrLoggerTest.php b/Logging/tests/Unit/PsrLoggerTest.php index bea51a9ca401..3ea83b5f8253 100644 --- a/Logging/tests/Unit/PsrLoggerTest.php +++ b/Logging/tests/Unit/PsrLoggerTest.php @@ -17,13 +17,10 @@ namespace Google\Cloud\Logging\Tests\Unit; -use Google\Cloud\Core\Batch\BatchRunner; -use Google\Cloud\Core\Batch\OpisClosureSerializer; use Google\Cloud\Core\Report\EmptyMetadataProvider; use Google\Cloud\Logging\Logger; use Google\Cloud\Logging\PsrLogger; use Google\Cloud\Logging\Connection\ConnectionInterface; -use Prophecy\Argument; use Yoast\PHPUnitPolyfills\TestCases\TestCase; use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;