Skip to content

Commit

Permalink
Added metadataProvider to PsrLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
Takashi Matsuo committed May 2, 2017
1 parent a87363f commit fc8a0a6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
32 changes: 1 addition & 31 deletions src/Logging/PsrBatchLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ class PsrBatchLogger implements LoggerInterface
/** @var string */
private $logName;

/** @var MetadataProviderInterface */
private $metadataProvider;

/** @var BatchRunner */
private $batchRunner;

Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*/
Expand Down
17 changes: 15 additions & 2 deletions src/Logging/PsrLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
26 changes: 24 additions & 2 deletions src/Logging/PsrLoggerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
*/
trait PsrLoggerTrait
{
/** @var MetadataProviderInterface */
private $metadataProvider;

/**
* Return a Logger for sending logs.
*
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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 + [
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Logging/PsrLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()]);
}

/**
Expand Down

0 comments on commit fc8a0a6

Please sign in to comment.