mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-03 19:57:41 +02:00
Refactored the MailHandler to use the formatter to format the batch message
This commit is contained in:
@@ -13,139 +13,47 @@ namespace Monolog\Handler;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for all mail handlers
|
* Base class for all mail handlers
|
||||||
*
|
*
|
||||||
* @author Gyula Sallai
|
* @author Gyula Sallai
|
||||||
*/
|
*/
|
||||||
abstract class MailHandler extends AbstractHandler
|
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}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function handleBatch(array $records)
|
public function handleBatch(array $records)
|
||||||
{
|
{
|
||||||
$messages = array();
|
$messages = array();
|
||||||
|
|
||||||
foreach ($records as $record) {
|
foreach ($records as $record) {
|
||||||
if ($record['level'] < $this->level) {
|
if ($record['level'] < $this->level) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
$messages[] = $this->processRecord($record);
|
||||||
$record = $this->prepareRecord($record);
|
|
||||||
$messages[] = $record['message'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($messages)) {
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a mail with the given content
|
* Send a mail with the given content
|
||||||
*
|
*
|
||||||
* @param string $content
|
* @param string $content
|
||||||
*/
|
*/
|
||||||
abstract protected function send($content);
|
abstract protected function send($content);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
protected function write(array $record)
|
protected function write(array $record)
|
||||||
{
|
{
|
||||||
$content = $record['message'];
|
$content = $record['message'];
|
||||||
|
|
||||||
$this->send($content);
|
$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%';
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@@ -15,28 +15,25 @@ use Monolog\Logger;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* SwiftMailerHandler uses Swift_Mailer to send the emails
|
* SwiftMailerHandler uses Swift_Mailer to send the emails
|
||||||
*
|
*
|
||||||
* @author Gyula Sallai
|
* @author Gyula Sallai
|
||||||
*/
|
*/
|
||||||
class SwiftMailerHandler extends MailHandler
|
class SwiftMailerHandler extends MailHandler
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $mailer;
|
protected $mailer;
|
||||||
protected $message;
|
protected $message;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Swift_Mailer $mailer The mailer to use
|
* @param \Swift_Mailer $mailer The mailer to use
|
||||||
* @param \Swift_Message $message An example message for real messages,
|
* @param \Swift_Message $message An example message for real messages, only the body will be replaced
|
||||||
* only the body will be replaced
|
|
||||||
* @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
|
||||||
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
|
* @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)
|
public function __construct(\Swift_Mailer $mailer, \Swift_Message $message, $level = Logger::ERROR, $bubble = false)
|
||||||
{
|
{
|
||||||
|
parent::__construct($level, $bubble);
|
||||||
$this->mailer = $mailer;
|
$this->mailer = $mailer;
|
||||||
$this->message = $message;
|
$this->message = $message;
|
||||||
$this->level = $level;
|
|
||||||
$this->bubble = $bubble;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -46,8 +43,7 @@ class SwiftMailerHandler extends MailHandler
|
|||||||
{
|
{
|
||||||
$message = clone $this->message;
|
$message = clone $this->message;
|
||||||
$message->setBody($content);
|
$message->setBody($content);
|
||||||
|
|
||||||
$this->mailer->send($message);
|
$this->mailer->send($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -12,31 +12,27 @@
|
|||||||
namespace Monolog\Handler;
|
namespace Monolog\Handler;
|
||||||
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
|
||||||
use Monolog\TestCase;
|
use Monolog\TestCase;
|
||||||
|
|
||||||
class MailHandlerTest extends TestCase
|
class MailHandlerTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
public function testHandleBatch()
|
public function testHandleBatch()
|
||||||
{
|
{
|
||||||
$records = $this->getMultipleRecords();
|
$formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
|
||||||
|
$formatter->expects($this->once())
|
||||||
$formatter = $this->getMock('Monolog\Formatter\LineFormatter');
|
->method('formatBatch'); // Each record is formatted
|
||||||
$formatter->expects($this->exactly(count($records)))
|
|
||||||
->method('format'); // Each record is formatted
|
|
||||||
|
|
||||||
$handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
|
$handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
|
||||||
$handler->expects($this->once())
|
$handler->expects($this->once())
|
||||||
->method('send');
|
->method('send');
|
||||||
$handler->expects($this->never())
|
$handler->expects($this->never())
|
||||||
->method('write'); // write is for individual records
|
->method('write'); // write is for individual records
|
||||||
|
|
||||||
$handler->setFormatter($formatter);
|
$handler->setFormatter($formatter);
|
||||||
|
|
||||||
$handler->handleBatch($records);
|
$handler->handleBatch($this->getMultipleRecords());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testHandleBatchNotSendsMailIfMessagesAreBelowLevel()
|
public function testHandleBatchNotSendsMailIfMessagesAreBelowLevel()
|
||||||
{
|
{
|
||||||
$records = array(
|
$records = array(
|
||||||
@@ -44,24 +40,22 @@ class MailHandlerTest extends TestCase
|
|||||||
$this->getRecord(Logger::DEBUG, 'debug message 2'),
|
$this->getRecord(Logger::DEBUG, 'debug message 2'),
|
||||||
$this->getRecord(Logger::INFO, 'information'),
|
$this->getRecord(Logger::INFO, 'information'),
|
||||||
);
|
);
|
||||||
|
|
||||||
$handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
|
$handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
|
||||||
$handler->expects($this->never())
|
$handler->expects($this->never())
|
||||||
->method('send');
|
->method('send');
|
||||||
$handler->setLevel(Logger::ERROR);
|
$handler->setLevel(Logger::ERROR);
|
||||||
|
|
||||||
$handler->handleBatch($records);
|
$handler->handleBatch($records);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testHandle()
|
public function testHandle()
|
||||||
{
|
{
|
||||||
$record = $this->getRecord();
|
|
||||||
|
|
||||||
$handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
|
$handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
|
||||||
$handler->expects($this->once())
|
$handler->expects($this->once())
|
||||||
->method('send');
|
->method('send');
|
||||||
|
|
||||||
$handler->handle($record);
|
$handler->handle($this->getRecord());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -41,7 +41,7 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
|||||||
$this->getRecord(Logger::ERROR, 'error')
|
$this->getRecord(Logger::ERROR, 'error')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Monolog\Formatter\FormatterInterface
|
* @return Monolog\Formatter\FormatterInterface
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user