Skip to content

Commit

Permalink
Add compatibility with monolog 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardK91 committed Apr 4, 2023
1 parent 18e004b commit 182a838
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
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',
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);

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',
};

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

0 comments on commit 182a838

Please sign in to comment.