Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure all batch related options can be passed through to PSR logger #1110

Merged
merged 4 commits into from
Jun 19, 2018
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
12 changes: 11 additions & 1 deletion Logging/src/LoggingClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ function (array $entry) {
* Please note debug output currently only applies in CLI based
* applications. **Defaults to** `false`. Applies only when
* `batchEnabled` is set to `true`.
* @type resource $debugOutputResource A resource to output debug output
* to. Applies only when `batchEnabled` is set to `true`.
* @type array $batchOptions A set of options for a BatchJob.
* {@see \Google\Cloud\Core\Batch\BatchJob::__construct()} for
* more details.
Expand All @@ -537,6 +539,12 @@ function (array $entry) {
* @type BatchRunner $batchRunner A BatchRunner object. Mainly used for
* the tests to inject a mock. **Defaults to** a newly created
* BatchRunner. Applies only when `batchEnabled` is set to `true`.
* @type ClosureSerializerInterface $closureSerializer An implementation
* responsible for serializing closures used in the
* `$clientConfig`. This is especially important when using the
* batch daemon. **Defaults to**
* {@see Google\Cloud\Core\Batch\OpisClosureSerializer} if the
* `opis/closure` library is installed.
* }
* @return PsrLogger
*/
Expand All @@ -555,7 +563,9 @@ public function psrLogger($name, array $options = [])
'debugOutput',
'batchOptions',
'clientConfig',
'batchRunner'
'batchRunner',
'closureSerializer',
'debugOutputResource'
], $options);

return new PsrLogger(
Expand Down
24 changes: 22 additions & 2 deletions Logging/src/PsrLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class PsrLogger implements LoggerInterface, \Serializable
* @type bool $batchEnabled Determines whether or not to use background
* batching. **Defaults to** `false`. Note that this option is
* currently considered **experimental** and is subject to change.
* @type resource $debugOutputResource A resource to output debug output
* to.
* @type bool $debugOutput Whether or not to output debug information.
* Please note debug output currently only applies in CLI based
* applications. **Defaults to** `false`. Applies only when
Expand Down Expand Up @@ -428,14 +430,24 @@ public function getMetadataProvider()
*/
public function serialize()
{
$debugOutputResource = null;
if (is_resource($this->debugOutputResource)) {
$metadata = stream_get_meta_data($this->debugOutputResource);
$debugOutputResource = [
'uri' => $metadata['uri'],
'mode' => $metadata['mode']
];
}

return serialize([
$this->messageKey,
$this->batchEnabled,
$this->metadataProvider,
$this->debugOutput,
$this->clientConfig,
$this->batchMethod,
$this->logName
$this->logName,
$debugOutputResource
]);
}

Expand All @@ -454,8 +466,16 @@ public function unserialize($data)
$this->debugOutput,
$this->clientConfig,
$this->batchMethod,
$this->logName
$this->logName,
$debugOutputResource
) = unserialize($data);

if (is_array($debugOutputResource)) {
$this->debugOutputResource = fopen(
$debugOutputResource['uri'],
$debugOutputResource['mode']
);
}
}

/**
Expand Down
37 changes: 36 additions & 1 deletion Logging/tests/Unit/LoggingClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@

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\Core\Testing\GrpcTestTrait;
use Google\Cloud\Logging\Connection\Grpc;
use Google\Cloud\Logging\Logger;
use Google\Cloud\Logging\LoggingClient;
use Google\Cloud\Logging\Metric;
use Google\Cloud\Logging\PsrLogger;
use Google\Cloud\Logging\Sink;
use Google\Cloud\Logging\Connection\ConnectionInterface;
use Google\Cloud\Core\Testing\GrpcTestTrait;
use Prophecy\Argument;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_Assert;

/**
* @group logging
Expand Down Expand Up @@ -310,6 +314,37 @@ public function testGetsPsrLogger()
$this->assertInstanceOf(PsrLogger::class, $this->client->psrLogger('myLogger'));
}

public function testOptionsArePassedToPsrLogger()
{
$options = [
'metadataProvider' => new EmptyMetadataProvider,
'batchEnabled' => true,
'debugOutput' => true,
'batchOptions' => [
'batchSize' => 100,
'callPeriod' => 2.0,
'numWorkers' => 2
],
'clientConfig' => [
'projectId' => 'test'
],
'batchRunner' => new BatchRunner,
'closureSerializer' => new OpisClosureSerializer,
'debugOutputResource' => fopen('php://temp', 'wb')
];

$this->client->setConnection($this->connection->reveal());
$psrLogger = $this->client->psrLogger('myLogger', $options);

foreach ($options as $name => $value) {
$this->assertEquals(
$value,
PHPUnit_Framework_Assert::readAttribute($psrLogger, $name),
"$name assertion failed."
);
}
}

public function testGetsLogger()
{
$this->client->setConnection($this->connection->reveal());
Expand Down
63 changes: 63 additions & 0 deletions Logging/tests/Unit/PsrLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@

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 PHPUnit\Framework\TestCase;
use PHPUnit_Framework_Assert;

/**
* @group logging
Expand Down Expand Up @@ -229,4 +232,64 @@ public function testUsesCustomMessageKey()
'stackdriverOptions' => ['timestamp' => null]
]);
}

public function testSerializesCorrectly()
{
$expectedDebugResource = fopen('php://temp', 'wb');
$options = [
'metadataProvider' => new EmptyMetadataProvider,
'batchEnabled' => true,
'debugOutput' => true,
'clientConfig' => [
'projectId' => 'test'
],
'debugOutputResource' => $expectedDebugResource
];
$logger = $this->prophesize(Logger::class);
$logger->name()->willReturn($this->logName);
$psrLogger = new PsrLogger(
$logger->reveal(),
null,
$options
);
$options['messageKey'] = 'message';
$options['batchMethod'] = 'writeBatch';
$options['logName'] = $this->logName;
$psrLogger = unserialize(serialize($psrLogger));
$debugResourceMetadata = stream_get_meta_data(
PHPUnit_Framework_Assert::readAttribute($psrLogger, 'debugOutputResource')
);
$expectedDebugResourceMetadata = stream_get_meta_data($expectedDebugResource);

$this->assertEquals($debugResourceMetadata['uri'], $expectedDebugResourceMetadata['uri']);
$this->assertEquals($debugResourceMetadata['mode'], $expectedDebugResourceMetadata['mode']);
$this->assertEquals(
PHPUnit_Framework_Assert::readAttribute($psrLogger, 'metadataProvider'),
$options['metadataProvider']
);
$this->assertEquals(
PHPUnit_Framework_Assert::readAttribute($psrLogger, 'batchEnabled'),
$options['batchEnabled']
);
$this->assertEquals(
PHPUnit_Framework_Assert::readAttribute($psrLogger, 'debugOutput'),
$options['debugOutput']
);
$this->assertEquals(
PHPUnit_Framework_Assert::readAttribute($psrLogger, 'clientConfig'),
$options['clientConfig']
);
$this->assertEquals(
PHPUnit_Framework_Assert::readAttribute($psrLogger, 'messageKey'),
$options['messageKey']
);
$this->assertEquals(
PHPUnit_Framework_Assert::readAttribute($psrLogger, 'batchMethod'),
$options['batchMethod']
);
$this->assertEquals(
PHPUnit_Framework_Assert::readAttribute($psrLogger, 'logName'),
$this->logName
);
}
}