Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility with monolog 3.0 #1

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
"require": {
"php": "^8.0",
"ext-curl": "*",
"monolog/monolog": ">=1.0 <3.0",
"monolog/monolog": "^3.0",
"guzzlehttp/guzzle": "^7.5"
},
"autoload": {
"psr-4": {
"sgoettsch\\MonologDatadog\\": "src/Monolog"
}
}
}
}
28 changes: 10 additions & 18 deletions src/Monolog/Formatter/DatadogFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Copy link
Collaborator Author

@BernhardK91 BernhardK91 Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger::DEBUG and the other constants are deprecated. Monolog now uses the Level enum. For PHP 8.0 compatibility, we are unable to use enum cases as array keys. This is only supported with PHP 8.2.

For that reason, I have removed the DATADOG_LEVEL_MAP and implemented the Monolog to Datadog level conversion directly in the method below as a match clause.

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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Monolog changed the array to a class called LogRecord that implements ArrayAccess. So, it can be easily changed and still used as an array, at least for accessing the values.

{
$normalized = $this->normalize($record);

Expand All @@ -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',
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new implementation of the level converter from Monolog levels to Datadog levels.

In the last version, we also only provided 3 Datadog levels. I think more differentiated levels are possible, but for now, I kept it as it was before. We can add more Datadog levels later.


return $this->toJson($normalized, true);
}
Expand Down
22 changes: 12 additions & 10 deletions src/Monolog/Handler/DatadogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,15 +28,15 @@ 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
*/
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')) {
Expand All @@ -51,23 +53,23 @@ 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);
}

/**
* 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',
Expand Down Expand Up @@ -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'];
}
Expand All @@ -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'];

Expand Down