From 182a838dc21773b27c3312fb0b44117ac79103e5 Mon Sep 17 00:00:00 2001 From: Bernhard Kraemer Date: Tue, 4 Apr 2023 18:43:12 +0200 Subject: [PATCH] Add compatibility with monolog 3.0 --- composer.json | 4 ++-- src/Monolog/Formatter/DatadogFormatter.php | 28 ++++++++-------------- src/Monolog/Handler/DatadogHandler.php | 22 +++++++++-------- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/composer.json b/composer.json index 523b324..eabbc96 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "require": { "php": "^8.0", "ext-curl": "*", - "monolog/monolog": ">=1.0 <3.0", + "monolog/monolog": "^3.0", "guzzlehttp/guzzle": "^7.5" }, "autoload": { @@ -23,4 +23,4 @@ "sgoettsch\\MonologDatadog\\": "src/Monolog" } } -} \ No newline at end of file +} diff --git a/src/Monolog/Formatter/DatadogFormatter.php b/src/Monolog/Formatter/DatadogFormatter.php index 49dbfb0..bcb83ff 100644 --- a/src/Monolog/Formatter/DatadogFormatter.php +++ b/src/Monolog/Formatter/DatadogFormatter.php @@ -3,28 +3,16 @@ namespace sgoettsch\MonologDatadog\Formatter; use Monolog\Formatter\JsonFormatter; +use Monolog\Level; use Monolog\Logger; +use Monolog\LogRecord; use stdClass; class DatadogFormatter extends JsonFormatter { - protected $includeStacktraces = true; - - /** - * Map Monolog\Logger levels to Datadog's default status type - */ - private const DATADOG_LEVEL_MAP = [ - Logger::DEBUG => 'info', - Logger::INFO => 'info', - Logger::NOTICE => 'warning', - Logger::WARNING => 'warning', - Logger::ERROR => 'error', - Logger::ALERT => 'error', - Logger::CRITICAL => 'error', - Logger::EMERGENCY => 'error', - ]; - - public function format(array $record): string + protected bool $includeStacktraces = true; + + public function format(LogRecord $record): string { $normalized = $this->normalize($record); @@ -36,7 +24,11 @@ public function format(array $record): string $normalized['extra'] = new stdClass; } - $normalized['status'] = static::DATADOG_LEVEL_MAP[$record['level']]; + $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 d57b720..3654d7c 100644 --- a/src/Monolog/Handler/DatadogHandler.php +++ b/src/Monolog/Handler/DatadogHandler.php @@ -4,9 +4,11 @@ use JsonException; 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; @@ -26,7 +28,7 @@ class DatadogHandler extends AbstractProcessingHandler * @param string $apiKey Datadog API-Key * @param string $host Datadog API host * @param array $attributes Datadog optional attributes - * @param int|string $level The minimum logging level at which this handler will be triggered + * @param Level $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @throws MissingExtensionException */ @@ -34,7 +36,7 @@ public function __construct( string $apiKey, string $host = 'https://http-intake.logs.datadoghq.com', array $attributes = [], - int|string $level = Logger::DEBUG, + Level $level = Level::Debug, bool $bubble = true ) { if (!extension_loaded('curl')) { @@ -51,11 +53,11 @@ public function __construct( /** * Writes the record down to the log of the implementing handler * - * @param array $record + * @param LogRecord $record * @return void * @throws JsonException */ - protected function write(array $record): void + protected function write(LogRecord $record): void { $this->send($record); } @@ -63,11 +65,11 @@ protected function write(array $record): void /** * Send request to Datadog * - * @param array $record + * @param LogRecord $record * @throws JsonException * @noinspection SpellCheckingInspection */ - protected function send(array $record): void + protected function send(LogRecord $record): void { $headers = [ 'Content-Type' => 'application/json', @@ -107,11 +109,11 @@ protected function getSource(): string /** * Get Datadog Service from $attributes params. * - * @param array $record + * @param LogRecord $record * * @return string */ - protected function getService(array $record): string + protected function getService(LogRecord $record): string { return $this->attributes['service'] ?? $record['channel']; } @@ -129,11 +131,11 @@ protected function getHostname(): string /** * Get Datadog Tags from $attributes params. * - * @param array $record + * @param LogRecord $record * * @return string */ - protected function getTags(array $record): string + protected function getTags(LogRecord $record): string { $defaultTag = 'level:' . $record['level_name'];