Skip to content

Commit ff0103b

Browse files
authored
fix(Logging): setting of LogSeverity (#9103)
1 parent 44bdbf0 commit ff0103b

5 files changed

Lines changed: 197 additions & 46 deletions

File tree

Logging/src/Connection/Gapic.php

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,32 @@ class Gapic
6363
*/
6464
public function __construct(private array $config = [])
6565
{
66-
$this->serializer = new Serializer([
67-
'timestamp' => function ($v) {
68-
return $this->formatTimestampFromApi($v);
69-
},
70-
'severity' => function ($v) {
71-
return Logger::getLogLevelMap()[$v];
72-
},
73-
'json_payload' => function ($v) {
74-
return $this->unpackStructFromApi($v);
75-
}
76-
], [], [
77-
'json_payload' => function ($v) {
78-
return $this->formatStructForApi($v);
79-
},
80-
'severity' => function ($v) {
81-
return array_flip(Logger::getLogLevelMap())[strtoupper($v)];
82-
}
83-
], [
84-
'google.protobuf.Duration' => function ($v) {
85-
return $this->formatDurationForApi($v);
86-
},
87-
'google.protobuf.Timestamp' => function ($v) {
88-
return $this->formatTimestampForApi($v);
89-
}
90-
]);
66+
$this->serializer = new Serializer(
67+
fieldTransformers: [
68+
'timestamp' => function ($v) {
69+
return $this->formatTimestampFromApi($v);
70+
},
71+
'severity' => function ($v) {
72+
return Logger::getLogLevelMap()[$v];
73+
},
74+
'json_payload' => function ($v) {
75+
return $this->unpackStructFromApi($v);
76+
}
77+
],
78+
decodeFieldTransformers: [
79+
'json_payload' => function ($v) {
80+
return $this->formatStructForApi($v);
81+
},
82+
],
83+
decodeMessageTypeTransformers: [
84+
'google.protobuf.Duration' => function ($v) {
85+
return $this->formatDurationForApi($v);
86+
},
87+
'google.protobuf.Timestamp' => function ($v) {
88+
return $this->formatTimestampForApi($v);
89+
}
90+
]
91+
);
9192
$this->optionsValidator = new OptionsValidator($this->serializer);
9293
}
9394

@@ -186,11 +187,13 @@ public function getSink(array $args = [])
186187
{
187188
/**
188189
* @var GetSinkRequest $getSinkRequest
190+
* @var LogSink $_logSink
189191
* @var array $callOptions
190192
*/
191-
[$getSinkRequest, $callOptions] = $this->validateOptions(
193+
[$getSinkRequest, $_sink, $callOptions] = $this->validateOptions(
192194
$args,
193195
new GetSinkRequest(),
196+
new LogSink(), // unused, for backwards compatibility
194197
CallOptions::class
195198
);
196199
return $this->handleResponse(

Logging/src/PsrLogger.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public function debug(Stringable|string $message, array $context = []): void
374374
*/
375375
public function log($level, Stringable|string $message, array $context = []): void
376376
{
377-
$this->validateLogLevel($level);
377+
$level = $this->validateLogLevel($level);
378378
$options = [];
379379

380380
if (isset($context['exception'])
@@ -520,16 +520,21 @@ protected function getCallback()
520520
* Validates whether or not the provided log level exists.
521521
*
522522
* @param string|int $level The severity of the log entry.
523-
* @return bool
523+
* @return int
524524
* @throws \InvalidArgumentException
525525
*/
526-
private function validateLogLevel($level)
526+
private function validateLogLevel($level): int
527527
{
528528
$map = Logger::getLogLevelMap();
529529
$level = (string) $level;
530530

531-
if (isset($map[$level]) || isset(array_flip($map)[strtoupper($level)])) {
532-
return true;
531+
if (isset($map[$level])) {
532+
return $level;
533+
}
534+
535+
$levelFromString = array_flip($map)[strtoupper($level)] ?? null;
536+
if (!is_null($levelFromString)) {
537+
return $levelFromString;
533538
}
534539

535540
throw new InvalidArgumentException("Severity level '$level' is not defined.");

Logging/tests/Unit/Connection/GapicTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717

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

20+
use Google\ApiCore\GPBType;
2021
use Google\ApiCore\Page;
2122
use Google\ApiCore\PagedListResponse;
2223
use Google\Cloud\Logging\Connection\Grpc;
2324
use Google\Cloud\Core\Testing\GrpcTestTrait;
2425
use Google\Cloud\Core\GrpcRequestWrapper;
2526
use Google\ApiCore\Serializer;
2627
use Google\Cloud\Logging\Connection\Gapic;
28+
use Google\Cloud\Logging\Logger;
29+
use Google\Cloud\Logging\Type\LogSeverity;
2730
use Google\Cloud\Logging\V2\Client\ConfigServiceV2Client;
2831
use Google\Cloud\Logging\V2\Client\LoggingServiceV2Client;
2932
use Google\Cloud\Logging\V2\Client\MetricsServiceV2Client;
@@ -45,8 +48,11 @@
4548
use Google\Cloud\Logging\V2\UpdateSinkRequest;
4649
use Google\Cloud\Logging\V2\WriteLogEntriesRequest;
4750
use Google\Cloud\Logging\V2\WriteLogEntriesResponse;
51+
use Google\Protobuf\Internal\MapField;
4852
use Google\Protobuf\Internal\Message;
49-
use Google\Protobuf\RepeatedField;
53+
use Google\Protobuf\Struct;
54+
use Google\Protobuf\Timestamp;
55+
use Google\Protobuf\Value;
5056
use PHPUnit\Framework\TestCase;
5157
use Prophecy\Argument;
5258
use Prophecy\PhpUnit\ProphecyTrait;
@@ -167,7 +173,7 @@ public function methodProvider()
167173
'latency' => '1.0s'
168174
],
169175
'timestamp' => date('Y-m-d H:i:s', 100),
170-
'severity' => 'DEBUG',
176+
'severity' => Logger::DEBUG,
171177
]
172178
]
173179
],
@@ -261,4 +267,37 @@ public function methodProvider()
261267
]
262268
];
263269
}
270+
271+
public function testSerializedEntry()
272+
{
273+
$map = new MapField(GPBType::STRING, GPBType::MESSAGE, Value::class);
274+
$map['message'] = new Value(['string_value' => 'aPayload']);
275+
276+
$entry = new LogEntry([
277+
'severity' => LogSeverity::DEBUG,
278+
'timestamp' => new Timestamp(['seconds' => 100]),
279+
'json_payload' => new Struct(['fields' => $map]),
280+
]);
281+
282+
$page = $this->prophesize(Page::class);
283+
$page->getResponseObject()->willReturn(new ListLogEntriesResponse(['entries' => [$entry]]));
284+
$pagedListResponse = $this->prophesize(PagedListResponse::class);
285+
$pagedListResponse->getPage()
286+
->willReturn($page->reveal());
287+
288+
$this->loggingGapicClient->listLogEntries(
289+
new ListLogEntriesRequest(['filter' => 'logName=foo']),
290+
Argument::type('array')
291+
)
292+
->shouldBeCalledOnce()
293+
->willReturn($pagedListResponse->reveal());
294+
295+
$connection = new Gapic(['loggingGapicClient' => $this->loggingGapicClient->reveal()]);
296+
297+
$entries = $connection->listLogEntries(['filter' => 'logName=foo']);
298+
$info = $entries['entries'][0];
299+
$this->assertEquals('DEBUG', $info['severity']);
300+
$this->assertEquals(date('Y-m-d\TH:i:s.u\Z', 100), $info['timestamp']);
301+
$this->assertEquals(['message' => 'aPayload'], $info['jsonPayload']);
302+
}
264303
}

Logging/tests/Unit/PsrLoggerTest.php

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@
1717

1818
namespace Google\Cloud\Logging\Tests\Unit;
1919

20+
use Google\Api\MonitoredResource;
2021
use Google\Cloud\Core\Batch\OpisClosureSerializerV4;
2122
use Google\Cloud\Core\Report\EmptyMetadataProvider;
2223
use Google\Cloud\Logging\Logger;
2324
use Google\Cloud\Logging\PsrLogger;
2425
use Google\Cloud\Logging\Connection\Gapic;
26+
use Google\Cloud\Logging\V2\Client\LoggingServiceV2Client;
27+
use Google\Cloud\Logging\V2\LogEntry;
28+
use Google\Cloud\Logging\V2\WriteLogEntriesRequest;
29+
use Google\Cloud\Logging\V2\WriteLogEntriesResponse;
30+
use Google\Protobuf\Internal\GPBType;
31+
use Google\Protobuf\Internal\MapField;
32+
use Google\Protobuf\Struct;
33+
use Google\Protobuf\Timestamp;
34+
use Google\Protobuf\Value;
2535
use PHPUnit\Framework\TestCase;
2636
use Psr\Log\InvalidArgumentException;
2737
use Prophecy\PhpUnit\ProphecyTrait;
@@ -49,7 +59,7 @@ public function setUp(): void
4959

5060
public function getPsrLogger($connection, ?array $resource = null, ?array $labels = null, $messageKey = 'message')
5161
{
52-
$logger = new Logger($connection->reveal(), $this->logName, $this->projectId, $resource, $labels);
62+
$logger = new Logger($connection, $this->logName, $this->projectId, $resource, $labels);
5363
return new PsrLogger($logger, $messageKey, ['metadataProvider' => new EmptyMetadataProvider()]);
5464
}
5565

@@ -71,7 +81,7 @@ public function testWritesEntryWithDefinedLevels($level)
7181
])
7282
->willReturn([])
7383
->shouldBeCalledTimes(1);
74-
$psrLogger = $this->getPsrLogger($this->connection);
84+
$psrLogger = $this->getPsrLogger($this->connection->reveal());
7585

7686
$this->assertNull(
7787
$psrLogger->$level($this->textPayload, [
@@ -80,6 +90,40 @@ public function testWritesEntryWithDefinedLevels($level)
8090
);
8191
}
8292

93+
/**
94+
* @dataProvider levelProvider
95+
*/
96+
public function testWritesEntryRequestWithDefinedLevels($level)
97+
{
98+
$map = new MapField(GPBType::STRING, GPBType::MESSAGE, Value::class);
99+
$map['message'] = new Value(['string_value' => $this->textPayload]);
100+
$entry = new LogEntry([
101+
'severity' => array_flip(Logger::getLogLevelMap())[$level],
102+
'log_name' => $this->formattedName,
103+
'resource' => new MonitoredResource($this->resource),
104+
'timestamp' => new Timestamp(['seconds' => 100]),
105+
'json_payload' => new Struct(['fields' => $map])
106+
]);
107+
$request = new WriteLogEntriesRequest(['entries' => [$entry]]);
108+
109+
$loggingClient = $this->prophesize(LoggingServiceV2Client::class);
110+
$loggingClient->writeLogEntries($request, [])
111+
->shouldBeCalledOnce()
112+
->willReturn(new WriteLogEntriesResponse());
113+
114+
$connection = new Gapic([
115+
'loggingGapicClient' => $loggingClient->reveal()
116+
]);
117+
118+
$psrLogger = $this->getPsrLogger($connection);
119+
120+
$this->assertNull(
121+
$psrLogger->$level($this->textPayload, [
122+
'stackdriverOptions' => ['timestamp' => date('Y-m-d H:i:s', 100)]
123+
])
124+
);
125+
}
126+
83127
public function levelProvider()
84128
{
85129
return [
@@ -99,7 +143,7 @@ public function testWritesEntry()
99143
$this->connection->writeLogEntries([
100144
'entries' => [
101145
[
102-
'severity' => $this->severity,
146+
'severity' => array_flip(Logger::getLogLevelMap())[$this->severity],
103147
'jsonPayload' => ['message' => $this->textPayload],
104148
'logName' => $this->formattedName,
105149
'resource' => $this->resource,
@@ -109,7 +153,7 @@ public function testWritesEntry()
109153
])
110154
->willReturn([])
111155
->shouldBeCalledTimes(1);
112-
$psrLogger = $this->getPsrLogger($this->connection);
156+
$psrLogger = $this->getPsrLogger($this->connection->reveal());
113157

114158
$this->assertNull(
115159
$psrLogger->log($this->severity, $this->textPayload, [
@@ -125,7 +169,7 @@ public function testPsrLoggerUsesDefaults()
125169
$this->connection->writeLogEntries([
126170
'entries' => [
127171
[
128-
'severity' => $this->severity,
172+
'severity' => array_flip(Logger::getLogLevelMap())[$this->severity],
129173
'jsonPayload' => ['message' => $this->textPayload],
130174
'logName' => $this->formattedName,
131175
'resource' => $resource,
@@ -136,7 +180,7 @@ public function testPsrLoggerUsesDefaults()
136180
])
137181
->willReturn([])
138182
->shouldBeCalledTimes(1);
139-
$psrLogger = $this->getPsrLogger($this->connection, $resource, $labels);
183+
$psrLogger = $this->getPsrLogger($this->connection->reveal(), $resource, $labels);
140184

141185
$this->assertNull(
142186
$psrLogger->log($this->severity, $this->textPayload, [
@@ -153,7 +197,7 @@ public function testOverridePsrLoggerDefaults()
153197
$this->connection->writeLogEntries([
154198
'entries' => [
155199
[
156-
'severity' => $this->severity,
200+
'severity' => array_flip(Logger::getLogLevelMap())[$this->severity],
157201
'jsonPayload' => ['message' => $this->textPayload],
158202
'logName' => $this->formattedName,
159203
'resource' => $newResource,
@@ -164,7 +208,7 @@ public function testOverridePsrLoggerDefaults()
164208
])
165209
->willReturn([])
166210
->shouldBeCalledTimes(1);
167-
$psrLogger = $this->getPsrLogger($this->connection, null, $defaultLabels);
211+
$psrLogger = $this->getPsrLogger($this->connection->reveal(), null, $defaultLabels);
168212

169213
$this->assertNull(
170214
$psrLogger->log($this->severity, $this->textPayload, [
@@ -181,7 +225,7 @@ public function testLogThrowsExceptionWithInvalidLevel()
181225
{
182226
$this->expectException(InvalidArgumentException::class);
183227

184-
$psrLogger = $this->getPsrLogger($this->connection);
228+
$psrLogger = $this->getPsrLogger($this->connection->reveal());
185229
$psrLogger->log('INVALID-LEVEL', $this->textPayload);
186230
}
187231

@@ -205,7 +249,7 @@ public function testUsesCustomMessageKey()
205249
$this->connection->writeLogEntries([
206250
'entries' => [
207251
[
208-
'severity' => $this->severity,
252+
'severity' => array_flip(Logger::getLogLevelMap())[$this->severity],
209253
'jsonPayload' => [$customKey => $this->textPayload],
210254
'logName' => $this->formattedName,
211255
'resource' => $this->resource,
@@ -215,7 +259,7 @@ public function testUsesCustomMessageKey()
215259
])
216260
->willReturn([])
217261
->shouldBeCalledTimes(1);
218-
$psrLogger = $this->getPsrLogger($this->connection, null, null, $customKey);
262+
$psrLogger = $this->getPsrLogger($this->connection->reveal(), null, null, $customKey);
219263
$psrLogger->log($this->severity, $this->textPayload, [
220264
'stackdriverOptions' => ['timestamp' => null]
221265
]);
@@ -278,7 +322,7 @@ private function expectLogWithExceptionInContext($throwable)
278322
$this->connection->writeLogEntries([
279323
'entries' => [
280324
[
281-
'severity' => $this->severity,
325+
'severity' => array_flip(Logger::getLogLevelMap())[$this->severity],
282326
'jsonPayload' => [
283327
'message' => $this->textPayload,
284328
'exception' => (string) $throwable
@@ -291,7 +335,7 @@ private function expectLogWithExceptionInContext($throwable)
291335
])
292336
->willReturn([])
293337
->shouldBeCalledTimes(1);
294-
$psrLogger = $this->getPsrLogger($this->connection);
338+
$psrLogger = $this->getPsrLogger($this->connection->reveal());
295339
$psrLogger->log($this->severity, $this->textPayload, [
296340
'exception' => $throwable,
297341
'stackdriverOptions' => ['timestamp' => null]

0 commit comments

Comments
 (0)