Skip to content

Commit

Permalink
Merge pull request #6 from t3n/feature-MAIN-154-add-json-console-output
Browse files Browse the repository at this point in the history
feature/MAIN-154: Add new logger backend (json console stdout)
  • Loading branch information
w3z315 authored Jan 26, 2023
2 parents 16a010a + 09fe91a commit e4131a8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
93 changes: 93 additions & 0 deletions Classes/Backend/JSONConsoleLogger.php
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);
}
}
}
}
3 changes: 3 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ Neos:
class: 't3n\FlowLog\Backend\BigQueryLogger'
options:
loggerName: 'bigQueryLogger'
jsonConsoleLogger:
default:
class: 't3n\FlowLog\Backend\JSONConsoleLogger'

0 comments on commit e4131a8

Please sign in to comment.