From 4ec44b60c46fb9cb9b7d26a01cdc3293f2776263 Mon Sep 17 00:00:00 2001 From: Dave Supplee Date: Fri, 15 Jun 2018 10:16:33 -0400 Subject: [PATCH 1/4] ensure batch related options can be passed through to PSR logger --- Logging/src/LoggingClient.php | 12 +++++++++++- Logging/src/PsrLogger.php | 8 ++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Logging/src/LoggingClient.php b/Logging/src/LoggingClient.php index e008dca892db..dfc51e15c119 100644 --- a/Logging/src/LoggingClient.php +++ b/Logging/src/LoggingClient.php @@ -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. @@ -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 */ @@ -555,7 +563,9 @@ public function psrLogger($name, array $options = []) 'debugOutput', 'batchOptions', 'clientConfig', - 'batchRunner' + 'batchRunner', + 'closureSerializer', + 'debugOutputResource' ], $options); return new PsrLogger( diff --git a/Logging/src/PsrLogger.php b/Logging/src/PsrLogger.php index 87d25ddc284e..35f98bf42d15 100644 --- a/Logging/src/PsrLogger.php +++ b/Logging/src/PsrLogger.php @@ -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 @@ -435,7 +437,8 @@ public function serialize() $this->debugOutput, $this->clientConfig, $this->batchMethod, - $this->logName + $this->logName, + $this->debugOutputResource ]); } @@ -454,7 +457,8 @@ public function unserialize($data) $this->debugOutput, $this->clientConfig, $this->batchMethod, - $this->logName + $this->logName, + $this->debugOutputResource ) = unserialize($data); } From 01f2c61efed9e473bd5a0598d9149d8752a955e1 Mon Sep 17 00:00:00 2001 From: Dave Supplee Date: Tue, 19 Jun 2018 18:16:16 -0400 Subject: [PATCH 2/4] serialize resource metadata and add tests --- Logging/src/PsrLogger.php | 20 +++++++++-- Logging/tests/Unit/LoggingClientTest.php | 37 ++++++++++++++++++++- Logging/tests/Unit/PsrLoggerTest.php | 42 ++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 3 deletions(-) diff --git a/Logging/src/PsrLogger.php b/Logging/src/PsrLogger.php index 35f98bf42d15..669da8ce6a73 100644 --- a/Logging/src/PsrLogger.php +++ b/Logging/src/PsrLogger.php @@ -430,6 +430,15 @@ 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, @@ -438,7 +447,7 @@ public function serialize() $this->clientConfig, $this->batchMethod, $this->logName, - $this->debugOutputResource + $debugOutputResource ]); } @@ -458,8 +467,15 @@ public function unserialize($data) $this->clientConfig, $this->batchMethod, $this->logName, - $this->debugOutputResource + $debugOutputResource ) = unserialize($data); + + if (is_array($debugOutputResource)) { + $this->debugOutputResource = fopen( + $debugOutputResource['uri'], + $debugOutputResource['mode'] + ); + } } /** diff --git a/Logging/tests/Unit/LoggingClientTest.php b/Logging/tests/Unit/LoggingClientTest.php index 76fe534bbf03..cd4ee332650e 100644 --- a/Logging/tests/Unit/LoggingClientTest.php +++ b/Logging/tests/Unit/LoggingClientTest.php @@ -17,6 +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\Core\Testing\GrpcTestTrait; use Google\Cloud\Logging\Connection\Grpc; use Google\Cloud\Logging\Logger; use Google\Cloud\Logging\LoggingClient; @@ -24,8 +28,8 @@ 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\Assert; use PHPUnit\Framework\TestCase; /** @@ -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, + Assert::readAttribute($psrLogger, $name), + "$name assertion failed." + ); + } + } + public function testGetsLogger() { $this->client->setConnection($this->connection->reveal()); diff --git a/Logging/tests/Unit/PsrLoggerTest.php b/Logging/tests/Unit/PsrLoggerTest.php index fecd2c0aac10..3f66bbd71715 100644 --- a/Logging/tests/Unit/PsrLoggerTest.php +++ b/Logging/tests/Unit/PsrLoggerTest.php @@ -17,11 +17,14 @@ 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\Assert; use PHPUnit\Framework\TestCase; /** @@ -229,4 +232,43 @@ 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( + Assert::readAttribute($psrLogger, 'debugOutputResource') + ); + $expectedDebugResourceMetadata = stream_get_meta_data($expectedDebugResource); + + $this->assertEquals($debugResourceMetadata['uri'], $expectedDebugResourceMetadata['uri']); + $this->assertEquals($debugResourceMetadata['mode'], $expectedDebugResourceMetadata['mode']); + $this->assertEquals(Assert::readAttribute($psrLogger, 'metadataProvider'), $options['metadataProvider']); + $this->assertEquals(Assert::readAttribute($psrLogger, 'batchEnabled'), $options['batchEnabled']); + $this->assertEquals(Assert::readAttribute($psrLogger, 'debugOutput'), $options['debugOutput']); + $this->assertEquals(Assert::readAttribute($psrLogger, 'clientConfig'), $options['clientConfig']); + $this->assertEquals(Assert::readAttribute($psrLogger, 'messageKey'), $options['messageKey']); + $this->assertEquals(Assert::readAttribute($psrLogger, 'batchMethod'), $options['batchMethod']); + $this->assertEquals(Assert::readAttribute($psrLogger, 'logName'), $this->logName); + } } From c031bfa628f03721e96bb196b06416c7ac0bafe7 Mon Sep 17 00:00:00 2001 From: Dave Supplee Date: Tue, 19 Jun 2018 18:33:29 -0400 Subject: [PATCH 3/4] fix cs --- Logging/src/PsrLogger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Logging/src/PsrLogger.php b/Logging/src/PsrLogger.php index 669da8ce6a73..863dd2bd2fab 100644 --- a/Logging/src/PsrLogger.php +++ b/Logging/src/PsrLogger.php @@ -431,7 +431,7 @@ public function getMetadataProvider() public function serialize() { $debugOutputResource = null; - if (is_resource($this->debugOutputResource)) { + if (is_resource($this->debugOutputResource)) { $metadata = stream_get_meta_data($this->debugOutputResource); $debugOutputResource = [ 'uri' => $metadata['uri'], From 033655bca1926e82009161de33ce588f140f4bda Mon Sep 17 00:00:00 2001 From: Dave Supplee Date: Tue, 19 Jun 2018 18:40:10 -0400 Subject: [PATCH 4/4] fix for phpunit 5+ --- Logging/tests/Unit/LoggingClientTest.php | 4 +-- Logging/tests/Unit/PsrLoggerTest.php | 39 ++++++++++++++++++------ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Logging/tests/Unit/LoggingClientTest.php b/Logging/tests/Unit/LoggingClientTest.php index cd4ee332650e..b8ec86c54bfc 100644 --- a/Logging/tests/Unit/LoggingClientTest.php +++ b/Logging/tests/Unit/LoggingClientTest.php @@ -29,8 +29,8 @@ use Google\Cloud\Logging\Sink; use Google\Cloud\Logging\Connection\ConnectionInterface; use Prophecy\Argument; -use PHPUnit\Framework\Assert; use PHPUnit\Framework\TestCase; +use PHPUnit_Framework_Assert; /** * @group logging @@ -339,7 +339,7 @@ public function testOptionsArePassedToPsrLogger() foreach ($options as $name => $value) { $this->assertEquals( $value, - Assert::readAttribute($psrLogger, $name), + PHPUnit_Framework_Assert::readAttribute($psrLogger, $name), "$name assertion failed." ); } diff --git a/Logging/tests/Unit/PsrLoggerTest.php b/Logging/tests/Unit/PsrLoggerTest.php index 3f66bbd71715..db936e3a9144 100644 --- a/Logging/tests/Unit/PsrLoggerTest.php +++ b/Logging/tests/Unit/PsrLoggerTest.php @@ -24,8 +24,8 @@ use Google\Cloud\Logging\PsrLogger; use Google\Cloud\Logging\Connection\ConnectionInterface; use Prophecy\Argument; -use PHPUnit\Framework\Assert; use PHPUnit\Framework\TestCase; +use PHPUnit_Framework_Assert; /** * @group logging @@ -257,18 +257,39 @@ public function testSerializesCorrectly() $options['logName'] = $this->logName; $psrLogger = unserialize(serialize($psrLogger)); $debugResourceMetadata = stream_get_meta_data( - Assert::readAttribute($psrLogger, 'debugOutputResource') + 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(Assert::readAttribute($psrLogger, 'metadataProvider'), $options['metadataProvider']); - $this->assertEquals(Assert::readAttribute($psrLogger, 'batchEnabled'), $options['batchEnabled']); - $this->assertEquals(Assert::readAttribute($psrLogger, 'debugOutput'), $options['debugOutput']); - $this->assertEquals(Assert::readAttribute($psrLogger, 'clientConfig'), $options['clientConfig']); - $this->assertEquals(Assert::readAttribute($psrLogger, 'messageKey'), $options['messageKey']); - $this->assertEquals(Assert::readAttribute($psrLogger, 'batchMethod'), $options['batchMethod']); - $this->assertEquals(Assert::readAttribute($psrLogger, 'logName'), $this->logName); + $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 + ); } }