1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-09 14:46:46 +02:00

Refactored the AbstractHandler to avoid having some dummy write() methods

This commit is contained in:
Christophe Coevoet
2011-05-11 18:43:04 +02:00
parent 1d6697294d
commit 0defabb726
11 changed files with 103 additions and 82 deletions

View File

@@ -51,24 +51,6 @@ abstract class AbstractHandler implements HandlerInterface
return $record['level'] >= $this->level; return $record['level'] >= $this->level;
} }
/**
* {@inheritdoc}
*/
public function handle(array $record)
{
if ($record['level'] < $this->level) {
return false;
}
$record = $this->processRecord($record);
$record['message'] = $this->getFormatter()->format($record);
$this->write($record);
return false === $this->bubble;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@@ -171,14 +153,6 @@ abstract class AbstractHandler implements HandlerInterface
$this->close(); $this->close();
} }
/**
* Writes the record down to the log of the implementing handler
*
* @param array $record
* @return void
*/
abstract protected function write(array $record);
/** /**
* Gets the default formatter. * Gets the default formatter.
* *
@@ -188,21 +162,4 @@ abstract class AbstractHandler implements HandlerInterface
{ {
return new LineFormatter(); return new LineFormatter();
} }
/**
* Processes a record.
*
* @param array $record
* @return array
*/
protected function processRecord(array $record)
{
if ($this->processors) {
foreach ($this->processors as $processor) {
$record = call_user_func($processor, $record);
}
}
return $record;
}
} }

View File

@@ -0,0 +1,80 @@
<?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\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
/**
* Base Handler class providing the Handler structure
*
* Classes extending it should (in most cases) only implement write($record)
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Christophe Coevoet <stof@notk.org>
*/
abstract class AbstractProcessingHandler extends AbstractHandler
{
/**
* {@inheritdoc}
*/
public function handle(array $record)
{
if ($record['level'] < $this->level) {
return false;
}
$record = $this->processRecord($record);
$record['message'] = $this->getFormatter()->format($record);
$this->write($record);
return false === $this->bubble;
}
/**
* {@inheritdoc}
*/
public function handleBatch(array $records)
{
foreach ($records as $record) {
$this->handle($record);
}
}
/**
* Writes the record down to the log of the implementing handler
*
* @param array $record
* @return void
*/
abstract protected function write(array $record);
/**
* Processes a record.
*
* @param array $record
* @return array
*/
protected function processRecord(array $record)
{
if ($this->processors) {
foreach ($this->processors as $processor) {
$record = call_user_func($processor, $record);
}
}
return $record;
}
}

View File

@@ -38,7 +38,6 @@ class BufferHandler extends AbstractHandler
parent::__construct($level, $bubble); parent::__construct($level, $bubble);
$this->handler = $handler; $this->handler = $handler;
$this->bufferSize = $bufferSize; $this->bufferSize = $bufferSize;
$this->bubble = $bubble;
} }
/** /**
@@ -65,12 +64,4 @@ class BufferHandler extends AbstractHandler
{ {
$this->handler->handleBatch($this->buffer); $this->handler->handleBatch($this->buffer);
} }
/**
* Implemented to comply with the AbstractHandler requirements. Can not be called.
*/
protected function write(array $record)
{
throw new \BadMethodCallException('This method should not be called directly on the BufferHandler.');
}
} }

View File

@@ -47,6 +47,14 @@ class FingersCrossedHandler extends AbstractHandler
$this->stopBuffering = $stopBuffering; $this->stopBuffering = $stopBuffering;
} }
/**
* {@inheritdoc}
*/
public function isHandling(array $record)
{
return true;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@@ -84,12 +92,4 @@ class FingersCrossedHandler extends AbstractHandler
{ {
$this->buffering = true; $this->buffering = true;
} }
/**
* Implemented to comply with the AbstractHandler requirements. Can not be called.
*/
protected function write(array $record)
{
throw new \BadMethodCallException('This method should not be called directly on the FingersCrossedHandler.');
}
} }

View File

@@ -19,7 +19,7 @@ use Monolog\Formatter\WildfireFormatter;
* *
* @author Eric Clemmons (@ericclemmons) <eric@uxdriven.com> * @author Eric Clemmons (@ericclemmons) <eric@uxdriven.com>
*/ */
class FirePHPHandler extends AbstractHandler class FirePHPHandler extends AbstractProcessingHandler
{ {
/** /**
* WildFire JSON header message format * WildFire JSON header message format

View File

@@ -23,7 +23,7 @@ class GroupHandler extends AbstractHandler
protected $handlers; protected $handlers;
/** /**
* @param Array $handlers Array of Handlers or factory callbacks($record, $fingersCrossedHandler). * @param array $handlers Array of Handlers.
* @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(array $handlers, $bubble = false) public function __construct(array $handlers, $bubble = false)
@@ -32,6 +32,14 @@ class GroupHandler extends AbstractHandler
$this->bubble = $bubble; $this->bubble = $bubble;
} }
/**
* {@inheritdoc}
*/
public function isHandling(array $record)
{
return true;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@@ -52,12 +60,4 @@ class GroupHandler extends AbstractHandler
$handler->handleBatch($records); $handler->handleBatch($records);
} }
} }
/**
* Implemented to comply with the AbstractHandler requirements. Can not be called.
*/
protected function write(array $record)
{
throw new \BadMethodCallException('This method should not be called directly on the GroupHandler.');
}
} }

View File

@@ -16,7 +16,7 @@ namespace Monolog\Handler;
* *
* @author Gyula Sallai * @author Gyula Sallai
*/ */
abstract class MailHandler extends AbstractHandler abstract class MailHandler extends AbstractProcessingHandler
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}

View File

@@ -34,11 +34,4 @@ class NullHandler extends AbstractHandler
return false === $this->bubble; return false === $this->bubble;
} }
/**
* {@inheritdoc}
*/
protected function write(array $record)
{
}
} }

View File

@@ -21,7 +21,7 @@ use Monolog\Logger;
* *
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
*/ */
class StreamHandler extends AbstractHandler class StreamHandler extends AbstractProcessingHandler
{ {
protected $stream; protected $stream;
protected $url; protected $url;

View File

@@ -27,7 +27,7 @@ use Monolog\Logger;
* *
* @author Sven Paulus <sven@karlsruhe.org> * @author Sven Paulus <sven@karlsruhe.org>
*/ */
class SyslogHandler extends AbstractHandler class SyslogHandler extends AbstractProcessingHandler
{ {
/** /**
* Translates Monolog log levels to syslog log priorities. * Translates Monolog log levels to syslog log priorities.

View File

@@ -20,7 +20,7 @@ use Monolog\Logger;
* *
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
*/ */
class TestHandler extends AbstractHandler class TestHandler extends AbstractProcessingHandler
{ {
protected $records; protected $records;
protected $recordsByLevel; protected $recordsByLevel;