-
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.
feat(logging): add json console logger backend
cherry-picked the JSONConsoleLogger from the main branch in order to release a tag for lower flow versions of this package
- Loading branch information
1 parent
f5c9b5f
commit 81dfd00
Showing
2 changed files
with
114 additions
and
1 deletion.
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,99 @@ | ||
<?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, | ||
int $severity = LOG_INFO, | ||
$additionalData = null, | ||
?string $packageKey = null, | ||
?string $className = null, | ||
?string $methodName = 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