Skip to content

Commit

Permalink
Issue #2 - WIP PR. Switching how stdout json log is built.
Browse files Browse the repository at this point in the history
  • Loading branch information
zipymonkey committed Dec 16, 2024
1 parent 6267889 commit 0e2e381
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/Logger/SplunkLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Drupal\psul_splunk_logger\Logger;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Serialization\Json;
use Drupal\Core\GeneratedLink;
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Logger\RfcLoggerTrait;
Expand All @@ -30,8 +31,6 @@ public function __construct(
* {@inheritdoc}
*/
public function log($level, string|\Stringable $message, array $context = []): void {
global $base_url;

$output = fopen('php://stdout', 'w');
$severity = RfcLogLevel::getLevels()[$level];

Expand All @@ -45,6 +44,9 @@ public function log($level, string|\Stringable $message, array $context = []): v
elseif ($message instanceof FormattableMarkup) {
$message = $message->__toString();
}
elseif ($json_string = json_decode($message)) {
$message = $json_string;
}
else {
// This is a string so replace placeholders.
$message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders);
Expand All @@ -56,22 +58,20 @@ public function log($level, string|\Stringable $message, array $context = []): v
$link = $link->__toString();
}

// JSON formatted needed for splunk.
$format = '{"app": "drupal", "severity": "!severity", "type": "!type", "date": "!date", "message": "!message", "uid": "!uid", "request-uri": "!request_uri", "refer": "!referer", "ip": "!ip", "link": "!link"}';

$entry = strtr($format, [
'!base_url' => $base_url,
'!timestamp' => $context['timestamp'],
'!severity' => $severity,
'!type' => $context['channel'],
'!message' => strip_tags($message),
'!uid' => $context['uid'],
'!request_uri' => $context['request_uri'],
'!referer' => $context['referer'],
'!ip' => $context['ip'],
'!link' => strip_tags($link),
'!date' => date('Y-m-d\TH:i:s', $context['timestamp']),
]);
$values = [
"app" => "drupal",
"severity" => $severity,
"type" => $context['channel'],
"date" => date('Y-m-d\TH:i:s', $context['timestamp']),
"message" => strip_tags($message),
"uid" => $context['uid'],
"request-uri" => $context['request_uri'],
"refer" => $context['referer'],
"ip" => $context['ip'],
"link " => strip_tags($link),
];

$entry = Json::encode($values);

fwrite($output, $entry . "\r\n");
fclose($output);
Expand Down

0 comments on commit 0e2e381

Please sign in to comment.