mirror of
https://github.com/Seldaek/monolog.git
synced 2025-02-24 06:52:34 +01:00
Improved the Raven handler when using a Buffer handler
This commit is contained in:
parent
a43f926ffe
commit
29ae147226
@ -12,6 +12,7 @@
|
|||||||
namespace Monolog\Handler;
|
namespace Monolog\Handler;
|
||||||
|
|
||||||
use Monolog\Formatter\LineFormatter;
|
use Monolog\Formatter\LineFormatter;
|
||||||
|
use Monolog\Formatter\FormatterInterface;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Monolog\Handler\AbstractProcessingHandler;
|
use Monolog\Handler\AbstractProcessingHandler;
|
||||||
use Raven_Client;
|
use Raven_Client;
|
||||||
@ -43,6 +44,11 @@ class RavenHandler extends AbstractProcessingHandler
|
|||||||
*/
|
*/
|
||||||
protected $ravenClient;
|
protected $ravenClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var LineFormatter The formatter to use for the logs generated via handleBatch()
|
||||||
|
*/
|
||||||
|
protected $logFormatter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Raven_Client $ravenClient
|
* @param Raven_Client $ravenClient
|
||||||
* @param integer $level The minimum logging level at which this handler will be triggered
|
* @param integer $level The minimum logging level at which this handler will be triggered
|
||||||
@ -55,6 +61,59 @@ class RavenHandler extends AbstractProcessingHandler
|
|||||||
$this->ravenClient = $ravenClient;
|
$this->ravenClient = $ravenClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function handleBatch(array $records)
|
||||||
|
{
|
||||||
|
// the last records is the "main" one
|
||||||
|
$record = array_pop($records);
|
||||||
|
|
||||||
|
// no need to go further if the main record is not at the right level
|
||||||
|
if ($record['level'] < $this->level) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the other ones are added as a context item
|
||||||
|
$logs = array();
|
||||||
|
foreach ($records as $r) {
|
||||||
|
if ($r['level'] < $this->level) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$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}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
@ -85,4 +144,14 @@ class RavenHandler extends AbstractProcessingHandler
|
|||||||
{
|
{
|
||||||
return new LineFormatter('[%channel%] %message%');
|
return new LineFormatter('[%channel%] %message%');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the default formatter for the logs generated by handleBatch().
|
||||||
|
*
|
||||||
|
* @return FormatterInterface
|
||||||
|
*/
|
||||||
|
protected function getDefaultLogFormatter()
|
||||||
|
{
|
||||||
|
return new LineFormatter();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ namespace Monolog\Handler;
|
|||||||
|
|
||||||
use Monolog\TestCase;
|
use Monolog\TestCase;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
use Monolog\Formatter\LineFormatter;
|
||||||
use Monolog\Handler\RavenHandler;
|
use Monolog\Handler\RavenHandler;
|
||||||
|
|
||||||
class RavenHandlerTest extends TestCase
|
class RavenHandlerTest extends TestCase
|
||||||
@ -88,6 +89,45 @@ class RavenHandlerTest extends TestCase
|
|||||||
$this->assertEquals($record['message'], $ravenClient->lastData['message']);
|
$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()
|
private function methodThatThrowsAnException()
|
||||||
{
|
{
|
||||||
throw new \Exception('This is an exception');
|
throw new \Exception('This is an exception');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user