mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-03 11:47:38 +02:00
Refactored the MailHandler to use the formatter to format the batch message
This commit is contained in:
@@ -18,25 +18,6 @@ namespace Monolog\Handler;
|
||||
*/
|
||||
abstract class MailHandler extends AbstractHandler
|
||||
{
|
||||
|
||||
protected $messageFormat;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(array $record)
|
||||
{
|
||||
if ($record['level'] < $this->level) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$record = $this->prepareRecord($record);
|
||||
|
||||
$this->write($record);
|
||||
|
||||
return false === $this->bubble;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -48,34 +29,15 @@ abstract class MailHandler extends AbstractHandler
|
||||
if ($record['level'] < $this->level) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$record = $this->prepareRecord($record);
|
||||
$messages[] = $record['message'];
|
||||
$messages[] = $this->processRecord($record);
|
||||
}
|
||||
|
||||
if (!empty($messages)) {
|
||||
$this->send($this->createMessage($messages));
|
||||
if (!$this->formatter) {
|
||||
$this->formatter = $this->getDefaultFormatter();
|
||||
}
|
||||
$this->send($this->formatter->formatBatch($messages));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the message format
|
||||
*
|
||||
* @param string $format
|
||||
*/
|
||||
public function setMessageFormat($format)
|
||||
{
|
||||
$this->messageFormat = $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message format
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMessageFormat()
|
||||
{
|
||||
return $this->messageFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,58 +56,4 @@ abstract class MailHandler extends AbstractHandler
|
||||
|
||||
$this->send($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a message to send from the given log entry messages
|
||||
*
|
||||
* @param array $messages
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function createMessage(array $messages)
|
||||
{
|
||||
if (null === $this->messageFormat) {
|
||||
$this->messageFormat = $this->getDefaultMessageFormat();
|
||||
}
|
||||
|
||||
$message = str_replace('%records%', implode('', $messages), $this->messageFormat);
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a record for writing
|
||||
*
|
||||
* This method is just a shortcut for the common handling process (except writing)
|
||||
*
|
||||
* @param array $record
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function prepareRecord(array $record)
|
||||
{
|
||||
if ($this->processors) {
|
||||
foreach ($this->processors as $processor) {
|
||||
$record = call_user_func($processor, $record);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->formatter) {
|
||||
$this->formatter = $this->getDefaultFormatter();
|
||||
}
|
||||
$record = $this->formatter->format($record);
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default mail message format
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getDefaultMessageFormat()
|
||||
{
|
||||
return 'Application logs:\n %records%';
|
||||
}
|
||||
|
||||
}
|
@@ -20,23 +20,20 @@ use Monolog\Logger;
|
||||
*/
|
||||
class SwiftMailerHandler extends MailHandler
|
||||
{
|
||||
|
||||
protected $mailer;
|
||||
protected $message;
|
||||
|
||||
/**
|
||||
* @param \Swift_Mailer $mailer The mailer to use
|
||||
* @param \Swift_Message $message An example message for real messages,
|
||||
* only the body will be replaced
|
||||
* @param \Swift_Message $message An example message for real messages, only the body will be replaced
|
||||
* @param integer $level The minimum logging level at which this handler will be triggered
|
||||
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
|
||||
*/
|
||||
public function __construct(\Swift_Mailer $mailer, \Swift_Message $message, $level = Logger::ERROR, $bubble = false)
|
||||
{
|
||||
parent::__construct($level, $bubble);
|
||||
$this->mailer = $mailer;
|
||||
$this->message = $message;
|
||||
$this->level = $level;
|
||||
$this->bubble = $bubble;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,5 +46,4 @@ class SwiftMailerHandler extends MailHandler
|
||||
|
||||
$this->mailer->send($message);
|
||||
}
|
||||
|
||||
}
|
@@ -12,19 +12,15 @@
|
||||
namespace Monolog\Handler;
|
||||
|
||||
use Monolog\Logger;
|
||||
|
||||
use Monolog\TestCase;
|
||||
|
||||
class MailHandlerTest extends TestCase
|
||||
{
|
||||
|
||||
public function testHandleBatch()
|
||||
{
|
||||
$records = $this->getMultipleRecords();
|
||||
|
||||
$formatter = $this->getMock('Monolog\Formatter\LineFormatter');
|
||||
$formatter->expects($this->exactly(count($records)))
|
||||
->method('format'); // Each record is formatted
|
||||
$formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
|
||||
$formatter->expects($this->once())
|
||||
->method('formatBatch'); // Each record is formatted
|
||||
|
||||
$handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
|
||||
$handler->expects($this->once())
|
||||
@@ -34,7 +30,7 @@ class MailHandlerTest extends TestCase
|
||||
|
||||
$handler->setFormatter($formatter);
|
||||
|
||||
$handler->handleBatch($records);
|
||||
$handler->handleBatch($this->getMultipleRecords());
|
||||
}
|
||||
|
||||
public function testHandleBatchNotSendsMailIfMessagesAreBelowLevel()
|
||||
@@ -55,13 +51,11 @@ class MailHandlerTest extends TestCase
|
||||
|
||||
public function testHandle()
|
||||
{
|
||||
$record = $this->getRecord();
|
||||
|
||||
$handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
|
||||
$handler->expects($this->once())
|
||||
->method('send');
|
||||
|
||||
$handler->handle($record);
|
||||
$handler->handle($this->getRecord());
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user