Skip to content

Commit

Permalink
Structure logs for Google Cloud Error Reporting (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewKostka authored Feb 2, 2024
1 parent 1a29f73 commit 7e02c75
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
30 changes: 28 additions & 2 deletions dist-persist/wbstack/src/Logging/CustomLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,34 @@ public function log( $level, $message, array $context = [] ) {
}

private function doLog( $level, $message, $context ) {
fwrite( STDERR, "[$level] " .
LegacyLogger::format( $this->channel, $message, $context ) );
$payload = false;

if ( str_contains( $this->channel, 'json' ) ) {
$decoded = json_decode( $message, true );
if ( json_last_error() === JSON_ERROR_NONE ) {
$payload = $decoded;
}
}

if ( !$payload ) {
$payload = [
'message' => LegacyLogger::format( $this->channel, $message, $context )
];
}

$payload[ '@type' ] = 'type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent';
$payload[ 'severity' ] = $level;
$payload[ 'serviceContext' ] = [
'service' => 'WBaaS MediaWiki',
'version' => '1.0.0'
];

$output = json_encode( $payload );
if ( json_last_error() !== JSON_ERROR_NONE ) {
$output = "[$level] " . LegacyLogger::format( $this->channel, $message, $context );
}

fwrite( STDERR, $output . PHP_EOL );
}

}
30 changes: 28 additions & 2 deletions dist/wbstack/src/Logging/CustomLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,34 @@ public function log( $level, $message, array $context = [] ) {
}

private function doLog( $level, $message, $context ) {
fwrite( STDERR, "[$level] " .
LegacyLogger::format( $this->channel, $message, $context ) );
$payload = false;

if ( str_contains( $this->channel, 'json' ) ) {
$decoded = json_decode( $message, true );
if ( json_last_error() === JSON_ERROR_NONE ) {
$payload = $decoded;
}
}

if ( !$payload ) {
$payload = [
'message' => LegacyLogger::format( $this->channel, $message, $context )
];
}

$payload[ '@type' ] = 'type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent';
$payload[ 'severity' ] = $level;
$payload[ 'serviceContext' ] = [
'service' => 'WBaaS MediaWiki',
'version' => '1.0.0'
];

$output = json_encode( $payload );
if ( json_last_error() !== JSON_ERROR_NONE ) {
$output = "[$level] " . LegacyLogger::format( $this->channel, $message, $context );
}

fwrite( STDERR, $output . PHP_EOL );
}

}

0 comments on commit 7e02c75

Please sign in to comment.