diff --git a/src/Logging/PsrBatchLogger.php b/src/Logging/PsrBatchLogger.php index 466a9cb7bab9..bdf16e97971a 100644 --- a/src/Logging/PsrBatchLogger.php +++ b/src/Logging/PsrBatchLogger.php @@ -58,9 +58,6 @@ class PsrBatchLogger implements LoggerInterface /** @var string */ private $logName; - /** @var MetadataProviderInterface */ - private $metadataProvider; - /** @var BatchRunner */ private $batchRunner; @@ -126,17 +123,6 @@ public function __construct($logName, array $options = []) ); } - /** - * Return additional labels. Now it returns labels for log request - * correlation. - * - * @return array - */ - protected function getLabels() - { - return $this->metadataProvider->labels(); - } - /** * Return a Logger object for the current logName. * @@ -146,27 +132,11 @@ protected function getLogger() { if (!array_key_exists($this->logName, self::$loggers)) { $c = new LoggingClient($this->clientConfig); - $resource = $this->metadataProvider->monitoredResource(); - if (empty($resource)) { - self::$loggers[$this->logName] = $c->logger($this->logName); - } else { - self::$loggers[$this->logName] = - $c->logger($this->logName, ['resource' => $resource]); - } + self::$loggers[$this->logName] = $c->logger($this->logName); } return self::$loggers[$this->logName]; } - /** - * Return the MetadataProvider. - * - * @return MetadataProviderInterface - */ - public function getMetadataProvider() - { - return $this->metadataProvider; - } - /** * Submit the given entry to the BatchRunner. */ diff --git a/src/Logging/PsrLogger.php b/src/Logging/PsrLogger.php index 2bee2c78f9b3..5e09c4190eeb 100644 --- a/src/Logging/PsrLogger.php +++ b/src/Logging/PsrLogger.php @@ -17,6 +17,8 @@ namespace Google\Cloud\Logging; +use Google\Cloud\Core\Report\MetadataProviderInterface; +use Google\Cloud\Core\Report\MetadataProviderUtils; use Monolog\Formatter\NormalizerFormatter; use Monolog\Processor\PsrLogMessageProcessor; use Psr\Log\InvalidArgumentException; @@ -229,11 +231,22 @@ class PsrLogger implements LoggerInterface * @param Logger $logger The logger used to write entries. * @param string $messageKey The key in the `jsonPayload` used to contain * the logged message. **Defaults to** `message`. + * @param array $options [optional] { + * Configuration options. + * @type MetadataProviderInterface $metadataProvider + * **Defaults to null** If null, it will be automatically chosen. + * } */ - public function __construct(Logger $logger, $messageKey = 'message') - { + public function __construct( + Logger $logger, + $messageKey = 'message', + array $options = [] + ) { $this->logger = $logger; $this->messageKey = $messageKey; + $this->metadataProvider = isset($options['metadataProvider']) + ? $options['metadataProvider'] + : MetadataProviderUtils::autoSelect($_SERVER); } /** diff --git a/src/Logging/PsrLoggerTrait.php b/src/Logging/PsrLoggerTrait.php index e40d3992ef4f..7ac30071df5f 100644 --- a/src/Logging/PsrLoggerTrait.php +++ b/src/Logging/PsrLoggerTrait.php @@ -26,6 +26,9 @@ */ trait PsrLoggerTrait { + /** @var MetadataProviderInterface */ + private $metadataProvider; + /** * Return a Logger for sending logs. * @@ -34,13 +37,24 @@ trait PsrLoggerTrait protected abstract function getLogger(); /** - * Return common labels for each log entry. + * Return additional labels. Now it returns labels for log request + * correlation. * * @return array */ protected function getLabels() { - return []; + return $this->metadataProvider->labels(); + } + + /** + * Return the MetadataProvider. + * + * @return MetadataProviderInterface + */ + public function getMetadataProvider() + { + return $this->metadataProvider; } /** @@ -211,6 +225,14 @@ public function log($level, $message, array $context = []) ? $options['labels'] : []) + $labels; } + // Adding MonitoredResource + $resource = $this->metadataProvider->monitoredResource(); + if (! empty($resource)) { + $options['resource'] = + (isset($options['resource']) + ? $options['resource'] + : []) + $resource; + } $entry = $this->getLogger()->entry( $jsonPayload + $processedData['context'], $options + [ diff --git a/tests/unit/Logging/PsrLoggerTest.php b/tests/unit/Logging/PsrLoggerTest.php index d1fb6c574447..3037e5bf5938 100644 --- a/tests/unit/Logging/PsrLoggerTest.php +++ b/tests/unit/Logging/PsrLoggerTest.php @@ -17,6 +17,7 @@ namespace Google\Cloud\Tests\Unit\Logging; +use Google\Cloud\Core\Report\EmptyMetadataProvider; use Google\Cloud\Logging\Logger; use Google\Cloud\Logging\PsrLogger; use Google\Cloud\Logging\Connection\ConnectionInterface; @@ -44,7 +45,7 @@ public function setUp() public function getPsrLogger($connection, array $resource = null, array $labels = null, $messageKey = 'message') { $logger = new Logger($connection->reveal(), $this->logName, $this->projectId, $resource, $labels); - return new PsrLogger($logger, $messageKey); + return new PsrLogger($logger, $messageKey, ['metadataProvider' => new EmptyMetadataProvider()]); } /**