1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-07-31 18:30:15 +02:00

Add a lot more scalar types and go strict in Logger

This commit is contained in:
Jordi Boggiano
2015-12-18 17:47:20 +00:00
parent c9aa0368bf
commit 6c424f851c
15 changed files with 67 additions and 75 deletions

View File

@@ -36,7 +36,7 @@ abstract class AbstractHandler extends Handler
/**
* {@inheritdoc}
*/
public function isHandling(array $record)
public function isHandling(array $record): bool
{
return $record['level'] >= $this->level;
}
@@ -47,7 +47,7 @@ abstract class AbstractHandler extends Handler
* @param int|string $level Level or level name
* @return self
*/
public function setLevel($level)
public function setLevel($level): self
{
$this->level = Logger::toMonologLevel($level);
@@ -59,7 +59,7 @@ abstract class AbstractHandler extends Handler
*
* @return int
*/
public function getLevel()
public function getLevel(): int
{
return $this->level;
}
@@ -71,7 +71,7 @@ abstract class AbstractHandler extends Handler
* false means that bubbling is not permitted.
* @return self
*/
public function setBubble($bubble)
public function setBubble(bool $bubble): self
{
$this->bubble = $bubble;
@@ -84,7 +84,7 @@ abstract class AbstractHandler extends Handler
* @return Boolean true means that this handler allows bubbling.
* false means that bubbling is not permitted.
*/
public function getBubble()
public function getBubble(): bool
{
return $this->bubble;
}

View File

@@ -27,7 +27,7 @@ abstract class AbstractProcessingHandler extends AbstractHandler implements Proc
/**
* {@inheritdoc}
*/
public function handle(array $record)
public function handle(array $record): bool
{
if (!$this->isHandling($record)) {
return false;

View File

@@ -50,7 +50,7 @@ class BufferHandler extends AbstractHandler implements ProcessableHandlerInterfa
/**
* {@inheritdoc}
*/
public function handle(array $record)
public function handle(array $record): bool
{
if ($record['level'] < $this->level) {
return false;

View File

@@ -66,7 +66,7 @@ class FilterHandler extends Handler implements ProcessableHandlerInterface
/**
* @return array
*/
public function getAcceptedLevels()
public function getAcceptedLevels(): array
{
return array_flip($this->acceptedLevels);
}
@@ -92,7 +92,7 @@ class FilterHandler extends Handler implements ProcessableHandlerInterface
/**
* {@inheritdoc}
*/
public function isHandling(array $record)
public function isHandling(array $record): bool
{
return isset($this->acceptedLevels[$record['level']]);
}
@@ -100,7 +100,7 @@ class FilterHandler extends Handler implements ProcessableHandlerInterface
/**
* {@inheritdoc}
*/
public function handle(array $record)
public function handle(array $record): bool
{
if (!$this->isHandling($record)) {
return false;

View File

@@ -76,7 +76,7 @@ class FingersCrossedHandler extends Handler implements ProcessableHandlerInterfa
/**
* {@inheritdoc}
*/
public function isHandling(array $record)
public function isHandling(array $record): bool
{
return true;
}
@@ -84,7 +84,7 @@ class FingersCrossedHandler extends Handler implements ProcessableHandlerInterfa
/**
* {@inheritdoc}
*/
public function handle(array $record)
public function handle(array $record): bool
{
if ($this->processors) {
$record = $this->processRecord($record);

View File

@@ -41,7 +41,7 @@ class GroupHandler extends Handler implements ProcessableHandlerInterface
/**
* {@inheritdoc}
*/
public function isHandling(array $record)
public function isHandling(array $record): bool
{
foreach ($this->handlers as $handler) {
if ($handler->isHandling($record)) {
@@ -55,7 +55,7 @@ class GroupHandler extends Handler implements ProcessableHandlerInterface
/**
* {@inheritdoc}
*/
public function handle(array $record)
public function handle(array $record): bool
{
if ($this->processors) {
$record = $this->processRecord($record);

View File

@@ -31,7 +31,7 @@ interface HandlerInterface
*
* @return Boolean
*/
public function isHandling(array $record);
public function isHandling(array $record): bool;
/**
* Handles a record.
@@ -47,7 +47,7 @@ interface HandlerInterface
* @return Boolean true means that this handler handled the record, and that bubbling is not permitted.
* false means the record was either not processed or that this handler allows bubbling.
*/
public function handle(array $record);
public function handle(array $record): bool;
/**
* Handles a set of records at once.

View File

@@ -36,7 +36,7 @@ class NullHandler extends Handler
/**
* {@inheritdoc}
*/
public function isHandling(array $record)
public function isHandling(array $record): bool
{
return $record['level'] >= $this->level;
}
@@ -44,7 +44,7 @@ class NullHandler extends Handler
/**
* {@inheritdoc}
*/
public function handle(array $record)
public function handle(array $record): bool
{
if ($record['level'] < $this->level) {
return false;

View File

@@ -157,7 +157,7 @@ class PHPConsoleHandler extends AbstractProcessingHandler
return $this->options;
}
public function handle(array $record)
public function handle(array $record): bool
{
if ($this->options['enabled'] && $this->connector->isActiveClient()) {
return parent::handle($record);

View File

@@ -43,7 +43,7 @@ class PsrHandler extends AbstractHandler
/**
* {@inheritDoc}
*/
public function handle(array $record)
public function handle(array $record): bool
{
if (!$this->isHandling($record)) {
return false;

View File

@@ -54,12 +54,12 @@ class SamplingHandler extends AbstractHandler implements ProcessableHandlerInter
}
}
public function isHandling(array $record)
public function isHandling(array $record): bool
{
return $this->handler->isHandling($record);
}
public function handle(array $record)
public function handle(array $record): bool
{
if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) {
// The same logic as in FingersCrossedHandler

View File

@@ -22,7 +22,7 @@ class WhatFailureGroupHandler extends GroupHandler
/**
* {@inheritdoc}
*/
public function handle(array $record)
public function handle(array $record): bool
{
if ($this->processors) {
foreach ($this->processors as $processor) {

View File

@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/*
* This file is part of the Monolog package.
@@ -11,6 +12,7 @@
namespace Monolog;
use DateTimeZone;
use Monolog\Handler\HandlerInterface;
use Monolog\Handler\StreamHandler;
use Psr\Log\LoggerInterface;
@@ -91,7 +93,7 @@ class Logger implements LoggerInterface
*
* @var array $levels Logging levels
*/
protected static $levels = array(
protected static $levels = [
self::DEBUG => 'DEBUG',
self::INFO => 'INFO',
self::NOTICE => 'NOTICE',
@@ -100,7 +102,7 @@ class Logger implements LoggerInterface
self::CRITICAL => 'CRITICAL',
self::ALERT => 'ALERT',
self::EMERGENCY => 'EMERGENCY',
);
];
/**
* @var string
@@ -129,7 +131,7 @@ class Logger implements LoggerInterface
protected $microsecondTimestamps = true;
/**
* @var \DateTimeZone
* @var DateTimeZone
*/
protected $timezone;
@@ -139,18 +141,18 @@ class Logger implements LoggerInterface
* @param callable[] $processors Optional array of processors
* @param DateTimeZone $timezone Optional timezone, if not provided date_default_timezone_get() will be used
*/
public function __construct($name, array $handlers = array(), array $processors = array(), $timezone = null)
public function __construct(string $name, array $handlers = array(), array $processors = array(), DateTimeZone $timezone = null)
{
$this->name = $name;
$this->handlers = $handlers;
$this->processors = $processors;
$this->timezone = new \DateTimeZone($timezone ?: date_default_timezone_get() ?: 'UTC');
$this->timezone = new DateTimeZone($timezone ?: date_default_timezone_get() ?: 'UTC');
}
/**
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
@@ -161,7 +163,7 @@ class Logger implements LoggerInterface
* @param HandlerInterface $handler
* @return $this
*/
public function pushHandler(HandlerInterface $handler)
public function pushHandler(HandlerInterface $handler): self
{
array_unshift($this->handlers, $handler);
@@ -173,7 +175,7 @@ class Logger implements LoggerInterface
*
* @return HandlerInterface
*/
public function popHandler()
public function popHandler(): HandlerInterface
{
if (!$this->handlers) {
throw new \LogicException('You tried to pop from an empty handler stack.');
@@ -190,7 +192,7 @@ class Logger implements LoggerInterface
* @param HandlerInterface[] $handlers
* @return $this
*/
public function setHandlers(array $handlers)
public function setHandlers(array $handlers): self
{
$this->handlers = array();
foreach (array_reverse($handlers) as $handler) {
@@ -203,7 +205,7 @@ class Logger implements LoggerInterface
/**
* @return HandlerInterface[]
*/
public function getHandlers()
public function getHandlers(): array
{
return $this->handlers;
}
@@ -214,11 +216,8 @@ class Logger implements LoggerInterface
* @param callable $callback
* @return $this
*/
public function pushProcessor($callback)
public function pushProcessor(callable $callback): self
{
if (!is_callable($callback)) {
throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
}
array_unshift($this->processors, $callback);
return $this;
@@ -229,7 +228,7 @@ class Logger implements LoggerInterface
*
* @return callable
*/
public function popProcessor()
public function popProcessor(): callable
{
if (!$this->processors) {
throw new \LogicException('You tried to pop from an empty processor stack.');
@@ -241,7 +240,7 @@ class Logger implements LoggerInterface
/**
* @return callable[]
*/
public function getProcessors()
public function getProcessors(): array
{
return $this->processors;
}
@@ -259,9 +258,9 @@ class Logger implements LoggerInterface
*
* @param bool $micro True to use microtime() to create timestamps
*/
public function useMicrosecondTimestamps($micro)
public function useMicrosecondTimestamps(bool $micro)
{
$this->microsecondTimestamps = (bool) $micro;
$this->microsecondTimestamps = $micro;
}
/**
@@ -272,7 +271,7 @@ class Logger implements LoggerInterface
* @param array $context The log context
* @return Boolean Whether the record has been processed
*/
public function addRecord($level, $message, array $context = array())
public function addRecord(int $level, string $message, array $context = array()): bool
{
$levelName = static::getLevelName($level);
@@ -295,12 +294,12 @@ class Logger implements LoggerInterface
if ($this->microsecondTimestamps) {
$ts = \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), $this->timezone);
} else {
$ts = new \DateTime(null, $this->timezone);
$ts = new \DateTime('', $this->timezone);
}
$ts->setTimezone($this->timezone);
$record = array(
'message' => (string) $message,
'message' => $message,
'context' => $context,
'level' => $level,
'level_name' => $levelName,
@@ -329,7 +328,7 @@ class Logger implements LoggerInterface
*
* @return array Assoc array with human-readable level names => level codes.
*/
public static function getLevels()
public static function getLevels(): array
{
return array_flip(static::$levels);
}
@@ -340,7 +339,7 @@ class Logger implements LoggerInterface
* @param int $level
* @return string
*/
public static function getLevelName($level)
public static function getLevelName(int $level): string
{
if (!isset(static::$levels[$level])) {
throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(static::$levels)));
@@ -355,12 +354,16 @@ class Logger implements LoggerInterface
* @param string|int Level number (monolog) or name (PSR-3)
* @return int
*/
public static function toMonologLevel($level)
public static function toMonologLevel($level): int
{
if (is_string($level) && defined(__CLASS__.'::'.strtoupper($level))) {
if (is_string($level)) {
if (defined(__CLASS__.'::'.strtoupper($level))) {
return constant(__CLASS__.'::'.strtoupper($level));
}
throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(static::$levels)));
}
return $level;
}
@@ -370,7 +373,7 @@ class Logger implements LoggerInterface
* @param int $level
* @return Boolean
*/
public function isHandling($level)
public function isHandling(int $level): bool
{
$record = array(
'level' => $level,
@@ -399,7 +402,7 @@ class Logger implements LoggerInterface
{
$level = static::toMonologLevel($level);
return $this->addRecord($level, $message, $context);
return $this->addRecord($level, (string) $message, $context);
}
/**
@@ -413,7 +416,7 @@ class Logger implements LoggerInterface
*/
public function debug($message, array $context = array())
{
return $this->addRecord(static::DEBUG, $message, $context);
return $this->addRecord(static::DEBUG, (string) $message, $context);
}
/**
@@ -427,7 +430,7 @@ class Logger implements LoggerInterface
*/
public function info($message, array $context = array())
{
return $this->addRecord(static::INFO, $message, $context);
return $this->addRecord(static::INFO, (string) $message, $context);
}
/**
@@ -441,7 +444,7 @@ class Logger implements LoggerInterface
*/
public function notice($message, array $context = array())
{
return $this->addRecord(static::NOTICE, $message, $context);
return $this->addRecord(static::NOTICE, (string) $message, $context);
}
/**
@@ -455,7 +458,7 @@ class Logger implements LoggerInterface
*/
public function warning($message, array $context = array())
{
return $this->addRecord(static::WARNING, $message, $context);
return $this->addRecord(static::WARNING, (string) $message, $context);
}
/**
@@ -469,7 +472,7 @@ class Logger implements LoggerInterface
*/
public function error($message, array $context = array())
{
return $this->addRecord(static::ERROR, $message, $context);
return $this->addRecord(static::ERROR, (string) $message, $context);
}
/**
@@ -483,7 +486,7 @@ class Logger implements LoggerInterface
*/
public function critical($message, array $context = array())
{
return $this->addRecord(static::CRITICAL, $message, $context);
return $this->addRecord(static::CRITICAL, (string) $message, $context);
}
/**
@@ -497,7 +500,7 @@ class Logger implements LoggerInterface
*/
public function alert($message, array $context = array())
{
return $this->addRecord(static::ALERT, $message, $context);
return $this->addRecord(static::ALERT, (string) $message, $context);
}
/**
@@ -511,15 +514,15 @@ class Logger implements LoggerInterface
*/
public function emergency($message, array $context = array())
{
return $this->addRecord(static::EMERGENCY, $message, $context);
return $this->addRecord(static::EMERGENCY, (string) $message, $context);
}
/**
* Set the timezone to be used for the timestamp of log records.
*
* @param \DateTimeZone $tz Timezone object
* @param DateTimeZone $tz Timezone object
*/
public function setTimezone(\DateTimeZone $tz)
public function setTimezone(DateTimeZone $tz)
{
$this->timezone = $tz;
}
@@ -527,9 +530,9 @@ class Logger implements LoggerInterface
/**
* Set the timezone to be used for the timestamp of log records.
*
* @return \DateTimeZone
* @return DateTimeZone
*/
public function getTimezone()
public function getTimezone(): DateTimeZone
{
return $this->timezone;
}

View File

@@ -112,7 +112,7 @@ class ExceptionTestHandler extends TestHandler
/**
* {@inheritdoc}
*/
public function handle(array $record)
public function handle(array $record): bool
{
parent::handle($record);

View File

@@ -182,17 +182,6 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
$logger->popProcessor();
}
/**
* @covers Monolog\Logger::pushProcessor
* @expectedException InvalidArgumentException
*/
public function testPushProcessorWithNonCallable()
{
$logger = new Logger(__METHOD__);
$logger->pushProcessor(new \stdClass());
}
/**
* @covers Monolog\Logger::addRecord
*/