fix(Logging): setting of LogSeverity by bshaffer · Pull Request #9103 · googleapis/google-cloud-php · GitHub
Skip to content
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
55 changes: 29 additions & 26 deletions Logging/src/Connection/Gapic.php
15 changes: 10 additions & 5 deletions Logging/src/PsrLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public function debug(Stringable|string $message, array $context = []): void
*/
public function log($level, Stringable|string $message, array $context = []): void
{
$this->validateLogLevel($level);
$level = $this->validateLogLevel($level);
$options = [];

if (isset($context['exception'])
Expand Down Expand Up @@ -520,16 +520,21 @@ protected function getCallback()
* Validates whether or not the provided log level exists.
*
* @param string|int $level The severity of the log entry.
* @return bool
* @return int
* @throws \InvalidArgumentException
*/
private function validateLogLevel($level)
private function validateLogLevel($level): int
{
$map = Logger::getLogLevelMap();
$level = (string) $level;

if (isset($map[$level]) || isset(array_flip($map)[strtoupper($level)])) {
return true;
if (isset($map[$level])) {
return $level;
}

$levelFromString = array_flip($map)[strtoupper($level)] ?? null;
if (!is_null($levelFromString)) {
return $levelFromString;
}

throw new InvalidArgumentException("Severity level '$level' is not defined.");
Expand Down
43 changes: 41 additions & 2 deletions Logging/tests/Unit/Connection/GapicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

namespace Google\Cloud\Logging\Tests\Unit\Connection;

use Google\ApiCore\GPBType;
use Google\ApiCore\Page;
use Google\ApiCore\PagedListResponse;
use Google\Cloud\Logging\Connection\Grpc;
use Google\Cloud\Core\Testing\GrpcTestTrait;
use Google\Cloud\Core\GrpcRequestWrapper;
use Google\ApiCore\Serializer;
use Google\Cloud\Logging\Connection\Gapic;
use Google\Cloud\Logging\Logger;
use Google\Cloud\Logging\Type\LogSeverity;
use Google\Cloud\Logging\V2\Client\ConfigServiceV2Client;
use Google\Cloud\Logging\V2\Client\LoggingServiceV2Client;
use Google\Cloud\Logging\V2\Client\MetricsServiceV2Client;
Expand All @@ -45,8 +48,11 @@
use Google\Cloud\Logging\V2\UpdateSinkRequest;
use Google\Cloud\Logging\V2\WriteLogEntriesRequest;
use Google\Cloud\Logging\V2\WriteLogEntriesResponse;
use Google\Protobuf\Internal\MapField;
use Google\Protobuf\Internal\Message;
use Google\Protobuf\RepeatedField;
use Google\Protobuf\Struct;
use Google\Protobuf\Timestamp;
use Google\Protobuf\Value;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
Expand Down Expand Up @@ -167,7 +173,7 @@ public function methodProvider()
'latency' => '1.0s'
],
'timestamp' => date('Y-m-d H:i:s', 100),
'severity' => 'DEBUG',
'severity' => Logger::DEBUG,
]
]
],
Expand Down Expand Up @@ -261,4 +267,37 @@ public function methodProvider()
]
];
}

public function testSerializedEntry()
{
$map = new MapField(GPBType::STRING, GPBType::MESSAGE, Value::class);
$map['message'] = new Value(['string_value' => 'aPayload']);

$entry = new LogEntry([
'severity' => LogSeverity::DEBUG,
'timestamp' => new Timestamp(['seconds' => 100]),
'json_payload' => new Struct(['fields' => $map]),
]);

$page = $this->prophesize(Page::class);
$page->getResponseObject()->willReturn(new ListLogEntriesResponse(['entries' => [$entry]]));
$pagedListResponse = $this->prophesize(PagedListResponse::class);
$pagedListResponse->getPage()
->willReturn($page->reveal());

$this->loggingGapicClient->listLogEntries(
new ListLogEntriesRequest(['filter' => 'logName=foo']),
Argument::type('array')
)
->shouldBeCalledOnce()
->willReturn($pagedListResponse->reveal());

$connection = new Gapic(['loggingGapicClient' => $this->loggingGapicClient->reveal()]);

$entries = $connection->listLogEntries(['filter' => 'logName=foo']);
$info = $entries['entries'][0];
$this->assertEquals('DEBUG', $info['severity']);
$this->assertEquals(date('Y-m-d\TH:i:s.u\Z', 100), $info['timestamp']);
$this->assertEquals(['message' => 'aPayload'], $info['jsonPayload']);
}
}
70 changes: 57 additions & 13 deletions Logging/tests/Unit/PsrLoggerTest.php
Loading
Loading