mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-05 12:47:39 +02:00
Merge pull request #24 from stof/mailhandler
Added MailHandler, NativeMailHandler and SwiftMailerHandler
This commit is contained in:
@@ -62,10 +62,7 @@ abstract class AbstractHandler implements HandlerInterface
|
|||||||
|
|
||||||
$record = $this->processRecord($record);
|
$record = $this->processRecord($record);
|
||||||
|
|
||||||
if (!$this->formatter) {
|
$record['message'] = $this->getFormatter()->format($record);
|
||||||
$this->formatter = $this->getDefaultFormatter();
|
|
||||||
}
|
|
||||||
$record['message'] = $this->formatter->format($record);
|
|
||||||
|
|
||||||
$this->write($record);
|
$this->write($record);
|
||||||
|
|
||||||
@@ -120,6 +117,10 @@ abstract class AbstractHandler implements HandlerInterface
|
|||||||
*/
|
*/
|
||||||
public function getFormatter()
|
public function getFormatter()
|
||||||
{
|
{
|
||||||
|
if (!$this->formatter) {
|
||||||
|
$this->formatter = $this->getDefaultFormatter();
|
||||||
|
}
|
||||||
|
|
||||||
return $this->formatter;
|
return $this->formatter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
56
src/Monolog/Handler/MailHandler.php
Normal file
56
src/Monolog/Handler/MailHandler.php
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
<?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
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function handleBatch(array $records)
|
||||||
|
{
|
||||||
|
$messages = array();
|
||||||
|
|
||||||
|
foreach ($records as $record) {
|
||||||
|
if ($record['level'] < $this->level) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$messages[] = $this->processRecord($record);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($messages)) {
|
||||||
|
$this->send($this->getFormatter()->formatBatch($messages));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a mail with the given content
|
||||||
|
*
|
||||||
|
* @param string $content
|
||||||
|
*/
|
||||||
|
abstract protected function send($content);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function write(array $record)
|
||||||
|
{
|
||||||
|
$content = $record['message'];
|
||||||
|
|
||||||
|
$this->send($content);
|
||||||
|
}
|
||||||
|
}
|
49
src/Monolog/Handler/NativeMailerHandler.php
Normal file
49
src/Monolog/Handler/NativeMailerHandler.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
use Monolog\Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NativeMailerHandler uses the mail() function to send the emails
|
||||||
|
*
|
||||||
|
* @author Christophe Coevoet <stof@notk.org>
|
||||||
|
*/
|
||||||
|
class NativeMailerHandler extends MailHandler
|
||||||
|
{
|
||||||
|
protected $to;
|
||||||
|
protected $subject;
|
||||||
|
protected $headers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $to The receiver of the mail
|
||||||
|
* @param string $subject The subject of the mail
|
||||||
|
* @param string $from The sender of the mail
|
||||||
|
* @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($to, $subject, $from, $level = Logger::ERROR, $bubble = false)
|
||||||
|
{
|
||||||
|
parent::__construct($level, $bubble);
|
||||||
|
$this->to = $to;
|
||||||
|
$this->subject = $subject;
|
||||||
|
$this->headers = sprintf("From: %s\r\nContent-type: text/plain; charset=utf-8\r\n", $from);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function send($content)
|
||||||
|
{
|
||||||
|
mail($this->to, $this->subject, wordwrap($content, 70), $this->headers);
|
||||||
|
}
|
||||||
|
}
|
49
src/Monolog/Handler/SwiftMailerHandler.php
Normal file
49
src/Monolog/Handler/SwiftMailerHandler.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
use Monolog\Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SwiftMailerHandler uses Swift_Mailer to send the emails
|
||||||
|
*
|
||||||
|
* @author Gyula Sallai
|
||||||
|
*/
|
||||||
|
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 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function send($content)
|
||||||
|
{
|
||||||
|
$message = clone $this->message;
|
||||||
|
$message->setBody($content);
|
||||||
|
|
||||||
|
$this->mailer->send($message);
|
||||||
|
}
|
||||||
|
}
|
61
tests/Monolog/Handler/MailHandlerTest.php
Normal file
61
tests/Monolog/Handler/MailHandlerTest.php
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
use Monolog\Logger;
|
||||||
|
use Monolog\TestCase;
|
||||||
|
|
||||||
|
class MailHandlerTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testHandleBatch()
|
||||||
|
{
|
||||||
|
$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())
|
||||||
|
->method('send');
|
||||||
|
$handler->expects($this->never())
|
||||||
|
->method('write'); // write is for individual records
|
||||||
|
|
||||||
|
$handler->setFormatter($formatter);
|
||||||
|
|
||||||
|
$handler->handleBatch($this->getMultipleRecords());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHandleBatchNotSendsMailIfMessagesAreBelowLevel()
|
||||||
|
{
|
||||||
|
$records = array(
|
||||||
|
$this->getRecord(Logger::DEBUG, 'debug message 1'),
|
||||||
|
$this->getRecord(Logger::DEBUG, 'debug message 2'),
|
||||||
|
$this->getRecord(Logger::INFO, 'information'),
|
||||||
|
);
|
||||||
|
|
||||||
|
$handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
|
||||||
|
$handler->expects($this->never())
|
||||||
|
->method('send');
|
||||||
|
$handler->setLevel(Logger::ERROR);
|
||||||
|
|
||||||
|
$handler->handleBatch($records);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHandle()
|
||||||
|
{
|
||||||
|
$handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
|
||||||
|
$handler->expects($this->once())
|
||||||
|
->method('send');
|
||||||
|
|
||||||
|
$handler->handle($this->getRecord());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -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