mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-06 13:16:39 +02:00
Added an abstract MailHandler class
This commit is contained in:
139
src/Monolog/Handler/MailHandler.php
Normal file
139
src/Monolog/Handler/MailHandler.php
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Monolog package.
|
||||||
|
*
|
||||||
|
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Monolog\Handler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for all mail handlers
|
||||||
|
*
|
||||||
|
* @author Gyula Sallai
|
||||||
|
*/
|
||||||
|
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}
|
||||||
|
*/
|
||||||
|
public function handleBatch(array $records)
|
||||||
|
{
|
||||||
|
$messages = array();
|
||||||
|
|
||||||
|
foreach ($records as $record) {
|
||||||
|
if ($record['level'] < $this->level) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$record = $this->prepareRecord($record);
|
||||||
|
$messages[] = $record['message'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->send($this->createMessage($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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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%';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a mail with the given message
|
||||||
|
*
|
||||||
|
* @param string $message
|
||||||
|
*/
|
||||||
|
abstract protected function send($message);
|
||||||
|
|
||||||
|
}
|
42
tests/Monolog/Handler/MailHandlerTest.php
Normal file
42
tests/Monolog/Handler/MailHandlerTest.php
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
$handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
|
||||||
|
$handler->expects($this->once())
|
||||||
|
->method('send');
|
||||||
|
$handler->expects($this->never())
|
||||||
|
->method('write'); // write is for individual records
|
||||||
|
|
||||||
|
$handler->setFormatter($formatter);
|
||||||
|
|
||||||
|
$handler->handleBatch($records);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHandle()
|
||||||
|
{
|
||||||
|
$record = $this->getRecord();
|
||||||
|
|
||||||
|
$handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
|
||||||
|
$handler->expects($this->once())
|
||||||
|
->method('write');
|
||||||
|
|
||||||
|
$this->assertTrue($handler->handle($record));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -28,6 +28,20 @@ class TestCase extends \PHPUnit_Framework_TestCase
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getMultipleRecords()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
$this->getRecord(Logger::DEBUG, 'debug message 1'),
|
||||||
|
$this->getRecord(Logger::DEBUG, 'debug message 2'),
|
||||||
|
$this->getRecord(Logger::INFO, 'information'),
|
||||||
|
$this->getRecord(Logger::WARNING, 'warning'),
|
||||||
|
$this->getRecord(Logger::ERROR, 'error')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Monolog\Formatter\FormatterInterface
|
* @return Monolog\Formatter\FormatterInterface
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user