Skip to content

Commit

Permalink
feat(logging): add json console logger backend
Browse files Browse the repository at this point in the history
cherry-picked the JSONConsoleLogger from the main branch in order to release a tag for lower flow versions of this package
  • Loading branch information
das-nagnag committed Jun 12, 2024
1 parent f5c9b5f commit 81dfd00
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
99 changes: 99 additions & 0 deletions Classes/Backend/JSONConsoleLogger.php
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);
}
}
}
}
16 changes: 15 additions & 1 deletion Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,18 @@ t3n:
service: 'flow-app'
version: 'master'


Neos:
Flow:
log:
psr3:
'Neos\Flow\Log\PsrLoggerFactory':
bigQueryLogger:
default:
class: 't3n\FlowLog\Backend\BigQueryLogger'
options:
loggerName: 'bigQueryLogger'
jsonConsoleLogger:
default:
class: 't3n\FlowLog\Backend\JSONConsoleLogger'
options:
loggerName: 'jsonConsoleLogger'

0 comments on commit 81dfd00

Please sign in to comment.