-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Monolog changed the array to a class called |
||
{ | ||
$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', | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
There was a problem hiding this comment.
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 theLevel
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.