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

Add some return type hints and fix up HandlerWrapper

This commit is contained in:
Jordi Boggiano
2016-03-01 17:35:54 +00:00
parent ba995ffd1e
commit 3d30ba5ecc
26 changed files with 89 additions and 87 deletions

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter; use Monolog\Formatter\LineFormatter;
/** /**
@@ -94,7 +95,7 @@ abstract class AbstractSyslogHandler extends AbstractProcessingHandler
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new LineFormatter('%channel%.%level_name%: %message% %context% %extra%'); return new LineFormatter('%channel%.%level_name%: %message% %context% %extra%');
} }

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\JsonFormatter; use Monolog\Formatter\JsonFormatter;
use PhpAmqpLib\Message\AMQPMessage; use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Channel\AMQPChannel; use PhpAmqpLib\Channel\AMQPChannel;
@@ -140,7 +141,7 @@ class AmqpHandler extends AbstractProcessingHandler
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false); return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
} }

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Formatter\LineFormatter; use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\FormatterInterface;
/** /**
* Handler sending logs to browser's javascript console with no browser extension required * Handler sending logs to browser's javascript console with no browser extension required
@@ -32,7 +33,7 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
* *
* You can do [[blue text]]{color: blue} or [[green background]]{background-color: green; color: white} * You can do [[blue text]]{color: blue} or [[green background]]{background-color: green; color: white}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new LineFormatter('[[%channel%]]{macro: autolabel} [[%level_name%]]{font-weight: bold} %message%'); return new LineFormatter('[[%channel%]]{macro: autolabel} [[%level_name%]]{font-weight: bold} %message%');
} }

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Formatter\ChromePHPFormatter; use Monolog\Formatter\ChromePHPFormatter;
use Monolog\Formatter\FormatterInterface;
use Monolog\Logger; use Monolog\Logger;
/** /**
@@ -88,7 +89,7 @@ class ChromePHPHandler extends AbstractProcessingHandler
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new ChromePHPFormatter(); return new ChromePHPFormatter();
} }

View File

@@ -11,6 +11,7 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\JsonFormatter; use Monolog\Formatter\JsonFormatter;
use Monolog\Logger; use Monolog\Logger;
@@ -65,7 +66,7 @@ class CouchDBHandler extends AbstractProcessingHandler
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false); return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
} }

View File

@@ -13,6 +13,7 @@ namespace Monolog\Handler;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Formatter\NormalizerFormatter; use Monolog\Formatter\NormalizerFormatter;
use Monolog\Formatter\FormatterInterface;
use Doctrine\CouchDB\CouchDBClient; use Doctrine\CouchDB\CouchDBClient;
/** /**
@@ -38,7 +39,7 @@ class DoctrineCouchDBHandler extends AbstractProcessingHandler
$this->client->postDocument($record['formatted']); $this->client->postDocument($record['formatted']);
} }
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new NormalizerFormatter; return new NormalizerFormatter;
} }

View File

@@ -13,6 +13,7 @@ namespace Monolog\Handler;
use Aws\Common\Aws; use Aws\Common\Aws;
use Aws\DynamoDb\DynamoDbClient; use Aws\DynamoDb\DynamoDbClient;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\ScalarFormatter; use Monolog\Formatter\ScalarFormatter;
use Monolog\Logger; use Monolog\Logger;
@@ -82,7 +83,7 @@ class DynamoDbHandler extends AbstractProcessingHandler
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new ScalarFormatter(self::DATE_FORMAT); return new ScalarFormatter(self::DATE_FORMAT);
} }

View File

@@ -76,7 +76,7 @@ class ElasticSearchHandler extends AbstractProcessingHandler
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setFormatter(FormatterInterface $formatter) public function setFormatter(FormatterInterface $formatter): HandlerInterface
{ {
if ($formatter instanceof ElasticaFormatter) { if ($formatter instanceof ElasticaFormatter) {
return parent::setFormatter($formatter); return parent::setFormatter($formatter);
@@ -96,7 +96,7 @@ class ElasticSearchHandler extends AbstractProcessingHandler
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new ElasticaFormatter($this->options['index'], $this->options['type']); return new ElasticaFormatter($this->options['index'], $this->options['type']);
} }

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Formatter\LineFormatter; use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\FormatterInterface;
use Monolog\Logger; use Monolog\Logger;
/** /**
@@ -60,7 +61,7 @@ class ErrorLogHandler extends AbstractProcessingHandler
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new LineFormatter('[%datetime%] %channel%.%level_name%: %message% %context% %extra%'); return new LineFormatter('[%datetime%] %channel%.%level_name%: %message% %context% %extra%');
} }

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Formatter\WildfireFormatter; use Monolog\Formatter\WildfireFormatter;
use Monolog\Formatter\FormatterInterface;
/** /**
* Simple FirePHP Handler (http://www.firephp.org/), which uses the Wildfire protocol. * Simple FirePHP Handler (http://www.firephp.org/), which uses the Wildfire protocol.
@@ -87,7 +88,7 @@ class FirePHPHandler extends AbstractProcessingHandler
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new WildfireFormatter(); return new WildfireFormatter();
} }

View File

@@ -11,6 +11,7 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter; use Monolog\Formatter\LineFormatter;
use Monolog\Logger; use Monolog\Logger;
@@ -63,7 +64,7 @@ class FleepHookHandler extends SocketHandler
* *
* @return LineFormatter * @return LineFormatter
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new LineFormatter(null, null, true, true); return new LineFormatter(null, null, true, true);
} }

View File

@@ -53,7 +53,7 @@ class FlowdockHandler extends SocketHandler
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setFormatter(FormatterInterface $formatter) public function setFormatter(FormatterInterface $formatter): HandlerInterface
{ {
if (!$formatter instanceof FlowdockFormatter) { if (!$formatter instanceof FlowdockFormatter) {
throw new \InvalidArgumentException('The FlowdockHandler requires an instance of Monolog\Formatter\FlowdockFormatter to function correctly'); throw new \InvalidArgumentException('The FlowdockHandler requires an instance of Monolog\Formatter\FlowdockFormatter to function correctly');
@@ -67,7 +67,7 @@ class FlowdockHandler extends SocketHandler
* *
* @return FormatterInterface * @return FormatterInterface
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
throw new \InvalidArgumentException('The FlowdockHandler must be configured (via setFormatter) with an instance of Monolog\Formatter\FlowdockFormatter to function correctly'); throw new \InvalidArgumentException('The FlowdockHandler must be configured (via setFormatter) with an instance of Monolog\Formatter\FlowdockFormatter to function correctly');
} }

View File

@@ -24,14 +24,14 @@ interface FormattableHandlerInterface
* Sets the formatter. * Sets the formatter.
* *
* @param FormatterInterface $formatter * @param FormatterInterface $formatter
* @return self * @return HandlerInterface self
*/ */
public function setFormatter(FormatterInterface $formatter); public function setFormatter(FormatterInterface $formatter): HandlerInterface;
/** /**
* Gets the formatter. * Gets the formatter.
* *
* @return FormatterInterface * @return FormatterInterface
*/ */
public function getFormatter(); public function getFormatter(): FormatterInterface;
} }

View File

@@ -30,7 +30,7 @@ trait FormattableHandlerTrait
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setFormatter(FormatterInterface $formatter) public function setFormatter(FormatterInterface $formatter): HandlerInterface
{ {
$this->formatter = $formatter; $this->formatter = $formatter;
@@ -40,7 +40,7 @@ trait FormattableHandlerTrait
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getFormatter() public function getFormatter(): FormatterInterface
{ {
if (!$this->formatter) { if (!$this->formatter) {
$this->formatter = $this->getDefaultFormatter(); $this->formatter = $this->getDefaultFormatter();
@@ -54,7 +54,7 @@ trait FormattableHandlerTrait
* *
* @return FormatterInterface * @return FormatterInterface
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new LineFormatter(); return new LineFormatter();
} }

View File

@@ -17,6 +17,7 @@ use Gelf\Publisher;
use InvalidArgumentException; use InvalidArgumentException;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Formatter\GelfMessageFormatter; use Monolog\Formatter\GelfMessageFormatter;
use Monolog\Formatter\FormatterInterface;
/** /**
* Handler to send messages to a Graylog2 (http://www.graylog2.org) server * Handler to send messages to a Graylog2 (http://www.graylog2.org) server
@@ -66,7 +67,7 @@ class GelfHandler extends AbstractProcessingHandler
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new GelfMessageFormatter(); return new GelfMessageFormatter();
} }

View File

@@ -28,7 +28,7 @@ use Monolog\Formatter\FormatterInterface;
* *
* @author Alexey Karapetov <alexey@karapetov.com> * @author Alexey Karapetov <alexey@karapetov.com>
*/ */
class HandlerWrapper implements HandlerInterface class HandlerWrapper implements HandlerInterface, ProcessableHandlerInterface, FormattableHandlerInterface
{ {
/** /**
* @var HandlerInterface * @var HandlerInterface
@@ -47,7 +47,7 @@ class HandlerWrapper implements HandlerInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isHandling(array $record) public function isHandling(array $record): bool
{ {
return $this->handler->isHandling($record); return $this->handler->isHandling($record);
} }
@@ -55,7 +55,7 @@ class HandlerWrapper implements HandlerInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function handle(array $record) public function handle(array $record): bool
{ {
return $this->handler->handle($record); return $this->handler->handle($record);
} }
@@ -71,34 +71,58 @@ class HandlerWrapper implements HandlerInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function pushProcessor($callback) public function close()
{ {
$this->handler->pushProcessor($callback); return $this->handler->close();
return $this;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function popProcessor() public function pushProcessor(callable $callback): HandlerInterface
{ {
return $this->handler->popProcessor(); if ($this->handler instanceof ProcessableHandlerInterface) {
$this->handler->pushProcessor($callback);
return $this;
}
throw new \LogicException('The wrapped handler does not implement ' . ProcessableHandlerInterface::class);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setFormatter(FormatterInterface $formatter) public function popProcessor(): callable
{ {
$this->handler->setFormatter($formatter); if ($this->handler instanceof ProcessableHandlerInterface) {
return $this; return $this->handler->popProcessor();
}
throw new \LogicException('The wrapped handler does not implement ' . ProcessableHandlerInterface::class);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getFormatter() public function setFormatter(FormatterInterface $formatter): HandlerInterface
{ {
return $this->handler->getFormatter(); if ($this->handler instanceof FormattableHandlerInterface) {
$this->handler->setFormatter($formatter);
}
throw new \LogicException('The wrapped handler does not implement ' . FormattableHandlerInterface::class);
} }
}
/**
* {@inheritdoc}
*/
public function getFormatter(): FormatterInterface
{
if ($this->handler instanceof FormattableHandlerInterface) {
return $this->handler->getFormatter($formatter);
}
throw new \LogicException('The wrapped handler does not implement ' . FormattableHandlerInterface::class);
}
}

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LogglyFormatter; use Monolog\Formatter\LogglyFormatter;
/** /**
@@ -95,7 +96,7 @@ class LogglyHandler extends AbstractProcessingHandler
Curl\Util::execute($ch); Curl\Util::execute($ch);
} }
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new LogglyFormatter(); return new LogglyFormatter();
} }

View File

@@ -15,6 +15,7 @@ use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Manager; use MongoDB\Driver\Manager;
use MongoDB\Client; use MongoDB\Client;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\NormalizerFormatter; use Monolog\Formatter\NormalizerFormatter;
/** /**
@@ -77,7 +78,7 @@ class MongoDBHandler extends AbstractProcessingHandler
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new NormalizerFormatter; return new NormalizerFormatter;
} }

View File

@@ -13,6 +13,7 @@ namespace Monolog\Handler;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Formatter\NormalizerFormatter; use Monolog\Formatter\NormalizerFormatter;
use Monolog\Formatter\FormatterInterface;
/** /**
* Class to record a log on a NewRelic application. * Class to record a log on a NewRelic application.
@@ -191,7 +192,7 @@ class NewRelicHandler extends AbstractProcessingHandler
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new NormalizerFormatter(); return new NormalizerFormatter();
} }

View File

@@ -13,6 +13,7 @@ namespace Monolog\Handler;
use Exception; use Exception;
use Monolog\Formatter\LineFormatter; use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\FormatterInterface;
use Monolog\Logger; use Monolog\Logger;
use PhpConsole\Connector; use PhpConsole\Connector;
use PhpConsole\Handler as VendorPhpConsoleHandler; use PhpConsole\Handler as VendorPhpConsoleHandler;
@@ -235,7 +236,7 @@ class PHPConsoleHandler extends AbstractProcessingHandler
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new LineFormatter('%message%'); return new LineFormatter('%message%');
} }

View File

@@ -22,14 +22,15 @@ interface ProcessableHandlerInterface
* Adds a processor in the stack. * Adds a processor in the stack.
* *
* @param callable $callback * @param callable $callback
* @return self * @return HandlerInterface self
*/ */
public function pushProcessor(callable $callback); public function pushProcessor(callable $callback): HandlerInterface;
/** /**
* Removes the processor on top of the stack and returns it. * Removes the processor on top of the stack and returns it.
* *
* @return callable * @return callable
* @throws LogicException In case the processor stack is empty
*/ */
public function popProcessor(); public function popProcessor(): callable;
} }

View File

@@ -28,7 +28,7 @@ trait ProcessableHandlerTrait
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function pushProcessor(callable $callback) public function pushProcessor(callable $callback): HandlerInterface
{ {
if (!is_callable($callback)) { if (!is_callable($callback)) {
throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given'); throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
@@ -41,7 +41,7 @@ trait ProcessableHandlerTrait
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function popProcessor() public function popProcessor(): callable
{ {
if (!$this->processors) { if (!$this->processors) {
throw new \LogicException('You tried to pop from an empty processor stack.'); throw new \LogicException('You tried to pop from an empty processor stack.');

View File

@@ -194,7 +194,7 @@ class RavenHandler extends AbstractProcessingHandler
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new LineFormatter('[%channel%] %message%'); return new LineFormatter('[%channel%] %message%');
} }

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Formatter\LineFormatter; use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\FormatterInterface;
use Monolog\Logger; use Monolog\Logger;
/** /**
@@ -90,7 +91,7 @@ class RedisHandler extends AbstractProcessingHandler
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected function getDefaultFormatter() protected function getDefaultFormatter(): FormatterInterface
{ {
return new LineFormatter(); return new LineFormatter();
} }

View File

@@ -10,6 +10,7 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\NormalizerFormatter; use Monolog\Formatter\NormalizerFormatter;
use Monolog\Logger; use Monolog\Logger;
@@ -78,7 +79,7 @@ class ZendMonitorHandler extends AbstractProcessingHandler
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDefaultFormatter() public function getDefaultFormatter(): FormatterInterface
{ {
return new NormalizerFormatter(); return new NormalizerFormatter();
} }

View File

@@ -87,44 +87,4 @@ class HandlerWrapperTest extends TestCase
$this->assertEquals($result, $this->wrapper->handleBatch($records)); $this->assertEquals($result, $this->wrapper->handleBatch($records));
} }
public function testPushProcessor()
{
$processor = function () {};
$this->handler->expects($this->once())
->method('pushProcessor')
->with($processor);
$this->assertEquals($this->wrapper, $this->wrapper->pushProcessor($processor));
}
public function testPopProcessor()
{
$processor = function () {};
$this->handler->expects($this->once())
->method('popProcessor')
->willReturn($processor);
$this->assertEquals($processor, $this->wrapper->popProcessor());
}
public function testSetFormatter()
{
$formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
$this->handler->expects($this->once())
->method('setFormatter')
->with($formatter);
$this->assertEquals($this->wrapper, $this->wrapper->setFormatter($formatter));
}
public function testGetFormatter()
{
$formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
$this->handler->expects($this->once())
->method('getFormatter')
->willReturn($formatter);
$this->assertEquals($formatter, $this->wrapper->getFormatter());
}
} }