1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-21 00:26:10 +02:00

Improved the Raven handler when using a Buffer handler

This commit is contained in:
Fabien Potencier
2013-06-15 15:16:38 +02:00
parent a43f926ffe
commit 29ae147226
2 changed files with 109 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,59 @@ class RavenHandler extends AbstractProcessingHandler
$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}
*/
@@ -85,4 +144,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();
}
}