mirror of
https://github.com/Seldaek/monolog.git
synced 2025-07-31 10:20:14 +02:00
Merge branch '1.x'
This commit is contained in:
@@ -21,6 +21,14 @@ use Monolog\Logger;
|
||||
* If the context data contains a `payload` key, that is used as an array
|
||||
* of payload options to RollbarNotifier's report_message/report_exception methods.
|
||||
*
|
||||
* Rollbar's context info will contain the context + extra keys from the log record
|
||||
* merged, and then on top of that a few keys:
|
||||
*
|
||||
* - level (rollbar level name)
|
||||
* - monolog_level (monolog level name, raw level, as rollbar only has 5 but monolog 8)
|
||||
* - channel
|
||||
* - datetime (unix timestamp)
|
||||
*
|
||||
* @author Paul Statezny <paulstatezny@gmail.com>
|
||||
*/
|
||||
class RollbarHandler extends AbstractProcessingHandler
|
||||
@@ -32,6 +40,17 @@ class RollbarHandler extends AbstractProcessingHandler
|
||||
*/
|
||||
protected $rollbarNotifier;
|
||||
|
||||
protected $levelMap = array(
|
||||
Logger::DEBUG => 'debug',
|
||||
Logger::INFO => 'info',
|
||||
Logger::NOTICE => 'info',
|
||||
Logger::WARNING => 'warning',
|
||||
Logger::ERROR => 'error',
|
||||
Logger::CRITICAL => 'critical',
|
||||
Logger::ALERT => 'critical',
|
||||
Logger::EMERGENCY => 'critical',
|
||||
);
|
||||
|
||||
/**
|
||||
* Records whether any log records have been added since the last flush of the rollbar notifier
|
||||
*
|
||||
@@ -56,36 +75,29 @@ class RollbarHandler extends AbstractProcessingHandler
|
||||
*/
|
||||
protected function write(array $record)
|
||||
{
|
||||
if (isset($record['context']['exception']) && $record['context']['exception'] instanceof Exception) {
|
||||
$context = $record['context'];
|
||||
$context = $record['context'];
|
||||
$payload = array();
|
||||
if (isset($context['payload'])) {
|
||||
$payload = $context['payload'];
|
||||
unset($context['payload']);
|
||||
}
|
||||
$context = array_merge($context, $record['extra'], array(
|
||||
'level' => $this->levelMap[$record['level']],
|
||||
'monolog_level' => $record['level_name'],
|
||||
'channel' => $record['channel'],
|
||||
'datetime' => $record['datetime']->format('U'),
|
||||
));
|
||||
|
||||
if (isset($context['exception']) && $context['exception'] instanceof Exception) {
|
||||
$exception = $context['exception'];
|
||||
unset($context['exception']);
|
||||
|
||||
$payload = [];
|
||||
if (isset($context['payload'])) {
|
||||
$payload = $context['payload'];
|
||||
unset($context['payload']);
|
||||
}
|
||||
|
||||
$this->rollbarNotifier->report_exception($exception, $context, $payload);
|
||||
} else {
|
||||
$extraData = [
|
||||
'level' => $record['level'],
|
||||
'channel' => $record['channel'],
|
||||
'datetime' => $record['datetime']->format('U'),
|
||||
];
|
||||
|
||||
$context = $record['context'];
|
||||
$payload = [];
|
||||
if (isset($context['payload'])) {
|
||||
$payload = $context['payload'];
|
||||
unset($context['payload']);
|
||||
}
|
||||
|
||||
$this->rollbarNotifier->report_message(
|
||||
$record['message'],
|
||||
$record['level_name'],
|
||||
array_merge($record['context'], $record['extra'], $extraData),
|
||||
$context['level'],
|
||||
$context,
|
||||
$payload
|
||||
);
|
||||
}
|
||||
|
@@ -53,19 +53,19 @@ class SyslogUdpHandler extends AbstractSyslogHandler
|
||||
$this->socket->close();
|
||||
}
|
||||
|
||||
private function splitMessageIntoLines($message)
|
||||
private function splitMessageIntoLines($message): array
|
||||
{
|
||||
if (is_array($message)) {
|
||||
$message = implode("\n", $message);
|
||||
}
|
||||
|
||||
return preg_split('/$\R?^/m', $message);
|
||||
return preg_split('/$\R?^/m', (string) $message, -1, PREG_SPLIT_NO_EMPTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make common syslog header (see rfc5424)
|
||||
*/
|
||||
protected function makeCommonSyslogHeader($severity)
|
||||
protected function makeCommonSyslogHeader($severity): string
|
||||
{
|
||||
$priority = $severity + $this->facility;
|
||||
|
||||
@@ -75,7 +75,7 @@ class SyslogUdpHandler extends AbstractSyslogHandler
|
||||
/**
|
||||
* Inject your own socket, mainly used for testing
|
||||
*/
|
||||
public function setSocket($socket)
|
||||
public function setSocket(UdpSocket $socket)
|
||||
{
|
||||
$this->socket = $socket;
|
||||
}
|
||||
|
@@ -11,10 +11,12 @@
|
||||
|
||||
namespace Monolog\Handler;
|
||||
|
||||
use Monolog\Test\TestCase;
|
||||
|
||||
/**
|
||||
* @requires extension sockets
|
||||
*/
|
||||
class SyslogUdpHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
class SyslogUdpHandlerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException UnexpectedValueException
|
||||
@@ -45,6 +47,23 @@ class SyslogUdpHandlerTest extends \PHPUnit_Framework_TestCase
|
||||
$handler->handle($this->getRecordWithMessage("hej\nlol"));
|
||||
}
|
||||
|
||||
public function testSplitWorksOnEmptyMsg()
|
||||
{
|
||||
$handler = new SyslogUdpHandler("127.0.0.1", 514, "authpriv");
|
||||
$handler->setFormatter($this->getIdentityFormatter());
|
||||
|
||||
$socket = $this->getMockBuilder('\Monolog\Handler\SyslogUdp\UdpSocket')
|
||||
->setMethods(['write'])
|
||||
->setConstructorArgs(['lol', 'lol'])
|
||||
->getMock();
|
||||
$socket->expects($this->never())
|
||||
->method('write');
|
||||
|
||||
$handler->setSocket($socket);
|
||||
|
||||
$handler->handle($this->getRecordWithMessage(null));
|
||||
}
|
||||
|
||||
protected function getRecordWithMessage($msg)
|
||||
{
|
||||
return ['message' => $msg, 'level' => \Monolog\Logger::WARNING, 'context' => null, 'extra' => [], 'channel' => 'lol'];
|
||||
|
Reference in New Issue
Block a user