mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-23 17:46:09 +02:00
Merge remote-tracking branch 'fabpot/raven-improvements'
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user