From 299a447cabd71802d3a9960155f62a2fded220f3 Mon Sep 17 00:00:00 2001 From: Bernhard Kraemer Date: Thu, 6 Apr 2023 10:55:56 +0200 Subject: [PATCH] Full status compatibility with datadog --- README.md | 2 +- composer.json | 2 +- src/Monolog/Formatter/DatadogFormatter.php | 35 ---------------------- src/Monolog/Handler/DatadogHandler.php | 13 ++++---- 4 files changed, 10 insertions(+), 42 deletions(-) delete mode 100644 src/Monolog/Formatter/DatadogFormatter.php diff --git a/README.md b/README.md index eb64d8f..3ac4f75 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Monolog Handler to forward logs to Datadog using async requests. ## Requirements -- PHP 8.0+ +- PHP 8.1+ - PHP Curl ## Installation diff --git a/composer.json b/composer.json index eabbc96..b34ac4c 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ } ], "require": { - "php": "^8.0", + "php": "^8.1", "ext-curl": "*", "monolog/monolog": "^3.0", "guzzlehttp/guzzle": "^7.5" diff --git a/src/Monolog/Formatter/DatadogFormatter.php b/src/Monolog/Formatter/DatadogFormatter.php deleted file mode 100644 index bcb83ff..0000000 --- a/src/Monolog/Formatter/DatadogFormatter.php +++ /dev/null @@ -1,35 +0,0 @@ -normalize($record); - - if (isset($normalized['context']) && $normalized['context'] === []) { - $normalized['context'] = new stdClass; - } - - if (isset($normalized['extra']) && $normalized['extra'] === []) { - $normalized['extra'] = new stdClass; - } - - $normalized['status'] = match ($record['level']) { - Level::Debug, Level::Info => 'info', - Level::Notice, Level::Warning => 'warning', - Level::Error, Level::Alert, Level::Critical, Level::Emergency => 'error', - }; - - return $this->toJson($normalized, true); - } -} diff --git a/src/Monolog/Handler/DatadogHandler.php b/src/Monolog/Handler/DatadogHandler.php index 3654d7c..0ef3cdd 100644 --- a/src/Monolog/Handler/DatadogHandler.php +++ b/src/Monolog/Handler/DatadogHandler.php @@ -3,13 +3,13 @@ namespace sgoettsch\MonologDatadog\Handler; use JsonException; +use Monolog\Formatter\JsonFormatter; use Monolog\Handler\MissingExtensionException; use Monolog\Level; use Monolog\Logger; use Monolog\Handler\AbstractProcessingHandler; use Monolog\Formatter\FormatterInterface; use Monolog\LogRecord; -use sgoettsch\MonologDatadog\Formatter\DatadogFormatter; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; @@ -83,7 +83,12 @@ protected function send(LogRecord $record): void $url = $this->host . '/api/v2/logs'; - $payLoad = json_decode($record['formatted'], true, 512, JSON_THROW_ON_ERROR); + $formated = json_decode($record->formatted, true, 512, JSON_THROW_ON_ERROR); + + // Datadog requires the log level in the status attribute. + $formated['status'] = strtolower($record['level_name']); + + $payLoad = $formated; $payLoad['ddsource'] = $source; $payLoad['ddtags'] = $tags; $payLoad['hostname'] = $hostname; @@ -157,11 +162,9 @@ protected function getTags(LogRecord $record): string /** * Returns the default formatter to use with this handler - * - * @return DatadogFormatter */ protected function getDefaultFormatter(): FormatterInterface { - return new DatadogFormatter(); + return new JsonFormatter(); } }