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

feat: add StdOutLogger and LoggingTrait #578

Merged
merged 53 commits into from
Dec 11, 2024
Merged
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
5de2301
Add the StdOutLogger and Logging Trait
Hectorhammett Sep 4, 2024
f393157
Fix types and style
Hectorhammett Sep 4, 2024
e5540f6
fix typing for PSR-3 logger interface
Hectorhammett Sep 5, 2024
ff21a95
Add logging to the Guzzle6HttpHandler
Hectorhammett Sep 9, 2024
0c65a32
Change logic for getDefaultLogger
Hectorhammett Sep 9, 2024
8c7c101
Fix style issues
Hectorhammett Sep 9, 2024
301d28e
Fix phpstan typing for getJwtToken
Hectorhammett Sep 9, 2024
5ae7c1e
Add a flag for disabling logger
Hectorhammett Sep 13, 2024
7aa7ee2
Add logging to the invoke method for apiary support
Hectorhammett Sep 16, 2024
331e97c
Fix style issue on build method
Hectorhammett Sep 16, 2024
35f8dde
Change logic for disabling logging
Hectorhammett Sep 20, 2024
36fd1ac
Fix logic for disabling logging
Hectorhammett Sep 20, 2024
2ff7d1a
Add a function to log a gRPC status only
Hectorhammett Sep 25, 2024
dbfb61c
Removed unused method
Hectorhammett Sep 25, 2024
2f87636
Fix style for LoggingTrait
Hectorhammett Sep 25, 2024
0cb0c27
Fix typing
Hectorhammett Sep 25, 2024
8e4a7e5
Updated type annotations
Hectorhammett Sep 25, 2024
6ca6bc7
Change logic for logging responses to exclude the info log
Hectorhammett Sep 27, 2024
16d9372
Change logic for logResponse
Hectorhammett Sep 27, 2024
b1ff503
Handle false for the case when json_encode returns false
Hectorhammett Oct 8, 2024
b9fb817
Fix serialization bug
Hectorhammett Oct 16, 2024
33c156a
Add support for logger in
Hectorhammett Oct 18, 2024
2a8f880
Update the StdOutLogger to use Psr the LoggerTrait
Hectorhammett Nov 11, 2024
13c516e
Fix style and improved readability
Hectorhammett Nov 11, 2024
e5ff048
Fix style issues
Hectorhammett Nov 11, 2024
374c859
Add milliseconds via microtime to the LogEvent class for latency calc…
Hectorhammett Nov 18, 2024
6655bd6
Fix unit testing for latency
Hectorhammett Nov 18, 2024
5bdbc7d
Update LoggingTrait to check fo is_null instead of falsey
Hectorhammett Nov 19, 2024
7aea4e9
Use the passed requestId option inside the options array
Hectorhammett Nov 21, 2024
7e2df5c
Change LogEvent name to RpcLogEvent
Hectorhammett Nov 22, 2024
1591d59
Use the round method to store miliseconds on RPCLogEvent
Hectorhammett Nov 25, 2024
53ce7fa
Change the array type of payload to string
Hectorhammett Nov 25, 2024
b05fd63
Fix null payload bug on the truncatePayload method
Hectorhammett Nov 25, 2024
a897b0b
Change the debug flag to GOOGLE_SDK_PHP_LOGGING
Hectorhammett Nov 26, 2024
027096e
Cast a response url to String
Hectorhammett Nov 30, 2024
db765ec
Fix type notation on phpDoc
Hectorhammett Dec 2, 2024
f35e295
Add the internal tag to internal classes
Hectorhammett Dec 3, 2024
636f650
Swap logic to log the info level log
Hectorhammett Dec 4, 2024
3bbac24
Fix the LoggingTrait test
Hectorhammett Dec 5, 2024
1a7523b
Fix Style
Hectorhammett Dec 5, 2024
876f14a
Change the client ID field to represent a Gapic Client
Hectorhammett Dec 5, 2024
7869f8c
Change clientId for processId in rpcLogEvent
Hectorhammett Dec 9, 2024
7d3a494
Change the httpHandler requestId fallback to a hash
Hectorhammett Dec 10, 2024
8d9ade7
Handle the getmypid false case
Hectorhammett Dec 10, 2024
ffaff38
Remove the INFO level log
Hectorhammett Dec 10, 2024
6aaf4de
Move logging logic of guzzle logging to private methods
Hectorhammett Dec 11, 2024
d611f8e
Update max lenght to be in a variable
Hectorhammett Dec 11, 2024
1df9dec
Update type doc for requestLog
Hectorhammett Dec 11, 2024
561d198
Remove unused mock class
Hectorhammett Dec 11, 2024
dbca5c8
Merge branch 'main' into http-logging
bshaffer Dec 11, 2024
79fbf1b
Apply suggestions from code review
bshaffer Dec 11, 2024
679f1bd
Remove readonly properties
Hectorhammett Dec 11, 2024
82388a7
Merge branch 'http-logging' of https://github.com/googleapis/google-a…
Hectorhammett Dec 11, 2024
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
Prev Previous commit
Next Next commit
Add a function to log a gRPC status only
  • Loading branch information
Hectorhammett committed Dec 2, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 2ff7d1aafa722d240a3bf3a9296f574c7c70c9df
10 changes: 5 additions & 5 deletions src/Logging/LogEvent.php
Original file line number Diff line number Diff line change
@@ -87,21 +87,21 @@ class LogEvent
*
* @var null|string $serviceName
*/
public null|string $serviceName;
public null|string $serviceName = null;

/**
* The Client Id for easy trace
*
* @var int $clientId
* @var null|int $clientId
*/
public int $clientId;
public null|int $clientId = null;

/**
* The Request id for easy trace
*
* @var int $requestId;
* @var null|int $requestId;
Hectorhammett marked this conversation as resolved.
Show resolved Hide resolved
*/
public int $requestId;
public null|int $requestId = null;

/**
* Creates an object with all the fields required for logging
60 changes: 57 additions & 3 deletions src/Logging/LoggingTrait.php
Original file line number Diff line number Diff line change
@@ -30,6 +30,8 @@ private function logRequest(LogEvent $event): void
'requestId' => $event->requestId ?? null,
];

$debugEvent = array_filter($debugEvent, fn($value) => !is_null($value));

$jsonPayload = [
'request.method' => $event->method,
'request.url' => $event->url,
@@ -39,7 +41,7 @@ private function logRequest(LogEvent $event): void
'retryAttempt' => $event->retryAttempt
];

$debugEvent['jsonPayload'] = array_filter($jsonPayload);
$debugEvent['jsonPayload'] = array_filter($jsonPayload, fn($value) => !is_null($value));

$this->logger->debug((string) json_encode($debugEvent));
}
@@ -58,7 +60,11 @@ private function logResponse(LogEvent $event): void
]
];

$debugEvent['jsonPayload'] = array_filter($debugEvent['jsonPayload']);
$debugEvent = array_filter($debugEvent, fn($value) => !is_null($value));
$debugEvent['jsonPayload'] = array_filter(
$debugEvent['jsonPayload'],
fn($value) => !is_null($value)
);
$this->logger->debug((string) json_encode($debugEvent));

$infoEvent = [
@@ -71,7 +77,36 @@ private function logResponse(LogEvent $event): void
]
];

$infoEvent['jsonPayload'] = array_filter($infoEvent['jsonPayload']);
$infoEvent = array_filter($infoEvent, fn($value) => !is_null($value));
$infoEvent['jsonPayload'] = array_filter(
$infoEvent['jsonPayload'],
fn($value) => !is_null($value)
);

$this->logger->info((string) json_encode($infoEvent));
}

/**
* @param LogEvent $status
*/
private function logStatus(LogEvent $event): void
{
$infoEvent = [
'timestamp' => $event->timestamp,
'severity' => LogLevel::INFO,
'clientId' => $event->clientId,
'requestId' => $event->requestId ?? null,
'jsonPayload' => [
'response.status' => $event->status
]
];

$infoEvent = array_filter($infoEvent, fn($value) => !is_null($value));
$infoEvent['jsonPayload'] = array_filter(
$infoEvent['jsonPayload'],
fn($value) => !is_null($value)
);

$this->logger->info((string) json_encode($infoEvent));
Hectorhammett marked this conversation as resolved.
Show resolved Hide resolved
}

@@ -98,4 +133,23 @@ private function getJwtToken(array $headers): null|array
'token' => base64_decode($token)
];
}

/**
* @param array<string, mixed> $arr
* @return array<string, mixed>
*/
private function removeNullsFromLog(array $arr): array
{
$result = [];

foreach ($arr as $key => $value) {
if (is_null($value)) {
continue;
}

if (is_array($value)) {

}
}
}
}