-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from t3n/feature-MAIN-154-add-json-console-output
feature/MAIN-154: Add new logger backend (json console stdout)
- Loading branch information
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace t3n\FlowLog\Backend; | ||
|
||
use Google\Cloud\BigQuery\Date; | ||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Flow\Log\Backend\ConsoleBackend; | ||
|
||
class JSONConsoleLogger extends ConsoleBackend | ||
{ | ||
/** | ||
* @Flow\InjectConfiguration(package="t3n.FlowLog.serviceContext") | ||
* | ||
* @var mixed[] | ||
*/ | ||
protected $serviceContext; | ||
|
||
public function open(): void | ||
{ | ||
parent::open(); | ||
$this->severityLabels = [ | ||
LOG_EMERG => 'emergency', | ||
LOG_ALERT => 'alert', | ||
LOG_CRIT => 'critical', | ||
LOG_ERR => 'error', | ||
LOG_WARNING => 'warning ', | ||
LOG_NOTICE => 'notice', | ||
LOG_INFO => 'info', | ||
LOG_DEBUG => 'debug', | ||
]; | ||
} | ||
|
||
/** | ||
* @param mixed $additionalData A variable containing more information about the event to be logged | ||
*/ | ||
public function append(string $message, string $packageKey, string $className, string $methodName, int $severity = LOG_INFO, $additionalData = null): void | ||
{ | ||
if ($severity > $this->severityThreshold) { | ||
return; | ||
} | ||
|
||
$additionalData = $additionalData ?? []; | ||
|
||
if ($packageKey !== null) { | ||
$additionalData['packageKey'] = $packageKey; | ||
} | ||
|
||
if ($className !== null) { | ||
$additionalData['className'] = $className; | ||
} | ||
|
||
if ($methodName !== null) { | ||
$additionalData['methodName'] = $methodName; | ||
} | ||
|
||
$severityLabel = $this->severityLabels[$severity] ?? 'unknown'; | ||
try { | ||
$data = [ | ||
'severity' => $severityLabel, | ||
'service' => $this->serviceContext['service'], | ||
'version' => $this->serviceContext['version'], | ||
'message' => $message, | ||
'additionalData' => json_encode($additionalData, JSON_THROW_ON_ERROR), | ||
'date' => new Date(new \DateTime('now')), | ||
'datetime' => new \DateTime('now') | ||
]; | ||
$output = json_encode($data); | ||
} catch (Exception $e) { | ||
$data = [ | ||
'severity' => $this->severityLabels[LOG_WARNING], | ||
'service' => $this->serviceContext['service'], | ||
'version' => $this->serviceContext['version'], | ||
'message' => 'Could not decode additional data of log message.', | ||
'additionalData' => [ | ||
'previousLog' => [ | ||
'message' => $message, | ||
'severity' => $severityLabel, | ||
], | ||
'stackTrace' => $e->getTraceAsString() | ||
], | ||
'date' => new Date(new \DateTime('now')), | ||
'datetime' => new \DateTime('now') | ||
]; | ||
$output = json_encode($data); | ||
} finally { | ||
if (is_resource($this->streamHandle)) { | ||
fwrite($this->streamHandle, $output . PHP_EOL); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters