1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-02-23 22:42:38 +01:00

Merge remote-tracking branch 'fabpot/raven-improvements'

This commit is contained in:
Jordi Boggiano 2013-07-28 18:48:43 +02:00
commit ec3f56a830
2 changed files with 114 additions and 0 deletions

View File

@ -12,6 +12,7 @@
namespace Monolog\Handler;
use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\FormatterInterface;
use Monolog\Logger;
use Monolog\Handler\AbstractProcessingHandler;
use Raven_Client;
@ -43,6 +44,11 @@ class RavenHandler extends AbstractProcessingHandler
*/
protected $ravenClient;
/**
* @var LineFormatter The formatter to use for the logs generated via handleBatch()
*/
protected $logFormatter;
/**
* @param Raven_Client $ravenClient
* @param integer $level The minimum logging level at which this handler will be triggered
@ -55,6 +61,64 @@ class RavenHandler extends AbstractProcessingHandler
$this->ravenClient = $ravenClient;
}
/**
* {@inheritdoc}
*/
public function handleBatch(array $records)
{
$level = $this->level;
// filter records based on their level
$records = array_filter($records, function($record) use($level) {
return $record['level'] >= $level;
});
// the record with the highest severity is the "main" one
$record = array_reduce($records, function($highest, $record) {
if($record['level'] >= $highest['level']) {
$highest = $record;
return $highest;
}
});
// the other ones are added as a context item
$logs = array();
foreach ($records as $r) {
$logs[] = $this->processRecord($r);
}
if ($logs) {
$record['context']['logs'] = (string) $this->getLogFormatter()->formatBatch($logs);
}
$this->handle($record);
}
/**
* Sets the formatter for the logs generated by handleBatch().
*
* @param FormatterInterface $formatter
*/
public function setLogFormatter(FormatterInterface $formatter)
{
$this->logFormatter = $formatter;
}
/**
* Gets the formatter for the logs generated by handleBatch().
*
* @return FormatterInterface
*/
public function getLogFormatter()
{
if (!$this->logFormatter) {
$this->logFormatter = $this->getDefaultLogFormatter();
}
return $this->logFormatter;
}
/**
* {@inheritdoc}
*/
@ -85,4 +149,14 @@ class RavenHandler extends AbstractProcessingHandler
{
return new LineFormatter('[%channel%] %message%');
}
/**
* Gets the default formatter for the logs generated by handleBatch().
*
* @return FormatterInterface
*/
protected function getDefaultLogFormatter()
{
return new LineFormatter();
}
}

View File

@ -13,6 +13,7 @@ namespace Monolog\Handler;
use Monolog\TestCase;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RavenHandler;
class RavenHandlerTest extends TestCase
@ -88,6 +89,45 @@ class RavenHandlerTest extends TestCase
$this->assertEquals($record['message'], $ravenClient->lastData['message']);
}
public function testHandleBatch()
{
$records = $this->getMultipleRecords();
$logFormatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
$logFormatter->expects($this->once())->method('formatBatch');
$formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
$formatter->expects($this->once())->method('format');
$handler = $this->getHandler($this->getRavenClient());
$handler->setLogFormatter($logFormatter);
$handler->setFormatter($formatter);
$handler->handleBatch($records);
}
public function testHandleBatchDoNothingIfRecordsAreBelowLevel()
{
$records = array(
$this->getRecord(Logger::DEBUG, 'debug message 1'),
$this->getRecord(Logger::DEBUG, 'debug message 2'),
$this->getRecord(Logger::INFO, 'information'),
);
$handler = $this->getMock('Monolog\Handler\RavenHandler', null, array($this->getRavenClient()));
$handler->expects($this->never())->method('handle');
$handler->setLevel(Logger::ERROR);
$handler->handleBatch($records);
}
public function testGetSetLogFormatter()
{
$ravenClient = $this->getRavenClient();
$handler = $this->getHandler($ravenClient);
$handler->setLogFormatter($formatter = new LineFormatter());
$this->assertSame($formatter, $handler->getLogFormatter());
}
private function methodThatThrowsAnException()
{
throw new \Exception('This is an exception');