mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-24 10:06:08 +02:00
Add graylog2/gelf-php 2.x support, fixes #1747
This commit is contained in:
@@ -47,6 +47,11 @@ class GelfMessageFormatter extends NormalizerFormatter
|
||||
*/
|
||||
protected $maxLength;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $gelfVersion = 2;
|
||||
|
||||
/**
|
||||
* Translates Monolog log levels to Graylog2 log priorities.
|
||||
*
|
||||
@@ -78,6 +83,10 @@ class GelfMessageFormatter extends NormalizerFormatter
|
||||
$this->extraPrefix = is_null($extraPrefix) ? '' : $extraPrefix;
|
||||
$this->contextPrefix = $contextPrefix;
|
||||
$this->maxLength = is_null($maxLength) ? self::DEFAULT_MAX_LENGTH : $maxLength;
|
||||
|
||||
if (method_exists(Message::class, 'setFacility')) {
|
||||
$this->gelfVersion = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,16 +122,20 @@ class GelfMessageFormatter extends NormalizerFormatter
|
||||
$message->setShortMessage(Utils::substr($record['message'], 0, $this->maxLength));
|
||||
}
|
||||
|
||||
if (isset($record['channel'])) {
|
||||
$message->setFacility($record['channel']);
|
||||
}
|
||||
if (isset($extra['line'])) {
|
||||
$message->setLine($extra['line']);
|
||||
unset($extra['line']);
|
||||
}
|
||||
if (isset($extra['file'])) {
|
||||
$message->setFile($extra['file']);
|
||||
unset($extra['file']);
|
||||
if ($this->gelfVersion === 1) {
|
||||
if (isset($record['channel'])) {
|
||||
$message->setFacility($record['channel']);
|
||||
}
|
||||
if (isset($extra['line'])) {
|
||||
$message->setLine($extra['line']);
|
||||
unset($extra['line']);
|
||||
}
|
||||
if (isset($extra['file'])) {
|
||||
$message->setFile($extra['file']);
|
||||
unset($extra['file']);
|
||||
}
|
||||
} else {
|
||||
$message->setAdditional('channel', $record['channel']);
|
||||
}
|
||||
|
||||
foreach ($extra as $key => $val) {
|
||||
@@ -147,11 +160,13 @@ class GelfMessageFormatter extends NormalizerFormatter
|
||||
$message->setAdditional($this->contextPrefix . $key, $val);
|
||||
}
|
||||
|
||||
/** @phpstan-ignore-next-line */
|
||||
if (null === $message->getFile() && isset($context['exception']['file'])) {
|
||||
if (preg_match("/^(.+):([0-9]+)$/", $context['exception']['file'], $matches)) {
|
||||
$message->setFile($matches[1]);
|
||||
$message->setLine($matches[2]);
|
||||
if ($this->gelfVersion === 1) {
|
||||
/** @phpstan-ignore-next-line */
|
||||
if (null === $message->getFile() && isset($context['exception']['file'])) {
|
||||
if (preg_match("/^(.+):([0-9]+)$/", $context['exception']['file'], $matches)) {
|
||||
$message->setFile($matches[1]);
|
||||
$message->setLine($matches[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user