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

Update return types

This commit is contained in:
Jordi Boggiano
2018-11-19 14:40:44 +01:00
parent 6bb347c040
commit 1c5b0b8ff4
13 changed files with 63 additions and 82 deletions

View File

@@ -97,7 +97,7 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
/**
* Wrapper for register_shutdown_function to allow overriding
*/
protected function registerShutdownFunction()
protected function registerShutdownFunction(): void
{
if (PHP_SAPI !== 'cli') {
register_shutdown_function(['Monolog\Handler\BrowserConsoleHandler', 'send']);

View File

@@ -36,11 +36,11 @@ class BufferHandler extends AbstractHandler implements ProcessableHandlerInterfa
/**
* @param HandlerInterface $handler Handler.
* @param int $bufferLimit How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
* @param int $level The minimum logging level at which this handler will be triggered
* @param string|int $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
* @param bool $flushOnOverflow If true, the buffer is flushed when the max size has been reached, by default oldest entries are discarded
*/
public function __construct(HandlerInterface $handler, $bufferLimit = 0, $level = Logger::DEBUG, bool $bubble = true, $flushOnOverflow = false)
public function __construct(HandlerInterface $handler, int $bufferLimit = 0, $level = Logger::DEBUG, bool $bubble = true, bool $flushOnOverflow = false)
{
parent::__construct($level, $bubble);
$this->handler = $handler;
@@ -82,7 +82,7 @@ class BufferHandler extends AbstractHandler implements ProcessableHandlerInterfa
return false === $this->bubble;
}
public function flush()
public function flush(): void
{
if ($this->bufferSize === 0) {
return;
@@ -112,7 +112,7 @@ class BufferHandler extends AbstractHandler implements ProcessableHandlerInterfa
/**
* Clears the buffer without flushing any messages down to the wrapped handler.
*/
public function clear()
public function clear(): void
{
$this->bufferSize = 0;
$this->buffer = [];

View File

@@ -61,8 +61,8 @@ class ChromePHPHandler extends AbstractProcessingHandler
protected static $sendHeaders = true;
/**
* @param int $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
* @param string|int $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct($level = Logger::DEBUG, bool $bubble = true)
{
@@ -110,7 +110,6 @@ class ChromePHPHandler extends AbstractProcessingHandler
*
* @see sendHeader()
* @see send()
* @param array $record
*/
protected function write(array $record): void
{
@@ -128,7 +127,7 @@ class ChromePHPHandler extends AbstractProcessingHandler
*
* @see sendHeader()
*/
protected function send()
protected function send(): void
{
if (self::$overflowed || !self::$sendHeaders) {
return;
@@ -171,11 +170,8 @@ class ChromePHPHandler extends AbstractProcessingHandler
/**
* Send header string to the client
*
* @param string $header
* @param string $content
*/
protected function sendHeader($header, $content)
protected function sendHeader(string $header, string $content): void
{
if (!headers_sent() && self::$sendHeaders) {
header(sprintf('%s: %s', $header, $content));

View File

@@ -35,7 +35,7 @@ class CubeHandler extends AbstractProcessingHandler
* A valid url must consist of three parts : protocol://host:port
* Only valid protocols used by Cube are http and udp
*/
public function __construct($url, $level = Logger::DEBUG, bool $bubble = true)
public function __construct(string $url, $level = Logger::DEBUG, bool $bubble = true)
{
$urlInfo = parse_url($url);
@@ -63,7 +63,7 @@ class CubeHandler extends AbstractProcessingHandler
* @throws \LogicException when unable to connect to the socket
* @throws MissingExtensionException when there is no socket extension
*/
protected function connectUdp()
protected function connectUdp(): void
{
if (!extension_loaded('sockets')) {
throw new MissingExtensionException('The sockets extension is required to use udp URLs with the CubeHandler');
@@ -83,7 +83,7 @@ class CubeHandler extends AbstractProcessingHandler
* Establish a connection to a http server
* @throws \LogicException when no curl extension
*/
protected function connectHttp()
protected function connectHttp(): void
{
if (!extension_loaded('curl')) {
throw new \LogicException('The curl extension is needed to use http URLs with the CubeHandler');
@@ -126,7 +126,7 @@ class CubeHandler extends AbstractProcessingHandler
}
}
private function writeUdp($data)
private function writeUdp(string $data): void
{
if (!$this->udpConnection) {
$this->connectUdp();
@@ -135,7 +135,7 @@ class CubeHandler extends AbstractProcessingHandler
socket_send($this->udpConnection, $data, strlen($data), 0);
}
private function writeHttp($data)
private function writeHttp(string $data): void
{
if (!$this->httpConnection) {
$this->connectHttp();

View File

@@ -58,11 +58,11 @@ class DeduplicationHandler extends BufferHandler
/**
* @param HandlerInterface $handler Handler.
* @param string $deduplicationStore The file/path where the deduplication log should be kept
* @param int $deduplicationLevel The minimum logging level for log records to be looked at for deduplication purposes
* @param string|int $deduplicationLevel The minimum logging level for log records to be looked at for deduplication purposes
* @param int $time The period (in seconds) during which duplicate entries should be suppressed after a given log is sent through
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct(HandlerInterface $handler, $deduplicationStore = null, $deduplicationLevel = Logger::ERROR, $time = 60, bool $bubble = true)
public function __construct(HandlerInterface $handler, ?string $deduplicationStore = null, $deduplicationLevel = Logger::ERROR, int $time = 60, bool $bubble = true)
{
parent::__construct($handler, 0, Logger::DEBUG, $bubble, false);
@@ -71,7 +71,7 @@ class DeduplicationHandler extends BufferHandler
$this->time = $time;
}
public function flush()
public function flush(): void
{
if ($this->bufferSize === 0) {
return;
@@ -100,7 +100,7 @@ class DeduplicationHandler extends BufferHandler
}
}
private function isDuplicate(array $record)
private function isDuplicate(array $record): bool
{
if (!file_exists($this->deduplicationStore)) {
return false;
@@ -130,10 +130,10 @@ class DeduplicationHandler extends BufferHandler
return false;
}
private function collectLogs()
private function collectLogs(): void
{
if (!file_exists($this->deduplicationStore)) {
return false;
return;
}
$handle = fopen($this->deduplicationStore, 'rw+');
@@ -161,7 +161,7 @@ class DeduplicationHandler extends BufferHandler
$this->gc = false;
}
private function appendRecord(array $record)
private function appendRecord(array $record): void
{
file_put_contents($this->deduplicationStore, $record['datetime']->getTimestamp() . ':' . $record['level_name'] . ':' . preg_replace('{[\r\n].*}', '', $record['message']) . "\n", FILE_APPEND);
}

View File

@@ -49,12 +49,9 @@ class DynamoDbHandler extends AbstractProcessingHandler
protected $marshaler;
/**
* @param DynamoDbClient $client
* @param string $table
* @param int $level
* @param bool $bubble
* @param int|string $level
*/
public function __construct(DynamoDbClient $client, $table, $level = Logger::DEBUG, bool $bubble = true)
public function __construct(DynamoDbClient $client, string $table, $level = Logger::DEBUG, bool $bubble = true)
{
if (defined('Aws\Sdk::VERSION') && version_compare(Sdk::VERSION, '3.0', '>=')) {
$this->version = 3;
@@ -87,11 +84,7 @@ class DynamoDbHandler extends AbstractProcessingHandler
]);
}
/**
* @param array $record
* @return array
*/
protected function filterEmptyFields(array $record)
protected function filterEmptyFields(array $record): array
{
return array_filter($record, function ($value) {
return !empty($value) || false === $value || 0 === $value;

View File

@@ -46,10 +46,10 @@ class ElasticSearchHandler extends AbstractProcessingHandler
protected $options = [];
/**
* @param Client $client Elastica Client object
* @param array $options Handler configuration
* @param int $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
* @param Client $client Elastica Client object
* @param array $options Handler configuration
* @param int|string $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct(Client $client, array $options = [], $level = Logger::DEBUG, bool $bubble = true)
{
@@ -85,11 +85,7 @@ class ElasticSearchHandler extends AbstractProcessingHandler
throw new \InvalidArgumentException('ElasticSearchHandler is only compatible with ElasticaFormatter');
}
/**
* Getter options
* @return array
*/
public function getOptions()
public function getOptions(): array
{
return $this->options;
}
@@ -113,10 +109,9 @@ class ElasticSearchHandler extends AbstractProcessingHandler
/**
* Use Elasticsearch bulk API to send list of documents
* @param array $documents
* @throws \RuntimeException
*/
protected function bulkSend(array $documents)
protected function bulkSend(array $documents): void
{
try {
$this->client->addDocuments($documents);

View File

@@ -29,12 +29,12 @@ class ErrorLogHandler extends AbstractProcessingHandler
protected $expandNewlines;
/**
* @param int $messageType Says where the error should go.
* @param int $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
* @param bool $expandNewlines If set to true, newlines in the message will be expanded to be take multiple log entries
* @param int $messageType Says where the error should go.
* @param int|string $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
* @param bool $expandNewlines If set to true, newlines in the message will be expanded to be take multiple log entries
*/
public function __construct($messageType = self::OPERATING_SYSTEM, $level = Logger::DEBUG, bool $bubble = true, $expandNewlines = false)
public function __construct(int $messageType = self::OPERATING_SYSTEM, $level = Logger::DEBUG, bool $bubble = true, bool $expandNewlines = false)
{
parent::__construct($level, $bubble);
@@ -51,7 +51,7 @@ class ErrorLogHandler extends AbstractProcessingHandler
/**
* @return array With all available types
*/
public static function getAvailableTypes()
public static function getAvailableTypes(): array
{
return [
self::OPERATING_SYSTEM,

View File

@@ -50,7 +50,7 @@ class FilterHandler extends Handler implements ProcessableHandlerInterface, Rese
/**
* @param callable|HandlerInterface $handler Handler or factory callable($record, $this).
* @param int|array $minLevelOrList A list of levels to accept or a minimum level if maxLevel is provided
* @param int $maxLevel Maximum level to accept, only used if $minLevelOrList is not an array
* @param int|string $maxLevel Maximum level to accept, only used if $minLevelOrList is not an array
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct($handler, $minLevelOrList = Logger::DEBUG, $maxLevel = Logger::EMERGENCY, bool $bubble = true)
@@ -64,9 +64,6 @@ class FilterHandler extends Handler implements ProcessableHandlerInterface, Rese
}
}
/**
* @return array
*/
public function getAcceptedLevels(): array
{
return array_flip($this->acceptedLevels);
@@ -76,7 +73,7 @@ class FilterHandler extends Handler implements ProcessableHandlerInterface, Rese
* @param int|string|array $minLevelOrList A list of levels to accept or a minimum level or level name if maxLevel is provided
* @param int|string $maxLevel Maximum level or level name to accept, only used if $minLevelOrList is not an array
*/
public function setAcceptedLevels($minLevelOrList = Logger::DEBUG, $maxLevel = Logger::EMERGENCY)
public function setAcceptedLevels($minLevelOrList = Logger::DEBUG, $maxLevel = Logger::EMERGENCY): self
{
if (is_array($minLevelOrList)) {
$acceptedLevels = array_map('Monolog\Logger::toMonologLevel', $minLevelOrList);
@@ -88,6 +85,8 @@ class FilterHandler extends Handler implements ProcessableHandlerInterface, Rese
}));
}
$this->acceptedLevels = array_flip($acceptedLevels);
return $this;
}
/**

View File

@@ -23,6 +23,10 @@ use Monolog\ResettableInterface;
* Only requests which actually trigger an error (or whatever your actionLevel is) will be
* in the logs, but they will contain all records, not only those above the level threshold.
*
* You can then have a passthruLevel as well which means that at the end of the request,
* even if it did not get activated, it will still send through log records of e.g. at least a
* warning level.
*
* You can find the various activation strategies in the
* Monolog\Handler\FingersCrossed\ namespace.
*
@@ -41,14 +45,14 @@ class FingersCrossedHandler extends Handler implements ProcessableHandlerInterfa
protected $passthruLevel;
/**
* @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler).
* @param int|ActivationStrategyInterface $activationStrategy Strategy which determines when this handler takes action
* @param int $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
* @param bool $stopBuffering Whether the handler should stop buffering after being triggered (default true)
* @param int $passthruLevel Minimum level to always flush to handler on close, even if strategy not triggered
* @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler).
* @param int|string|ActivationStrategyInterface $activationStrategy Strategy which determines when this handler takes action, or a level name/value at which the handler is activated
* @param int $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
* @param bool $stopBuffering Whether the handler should stop buffering after being triggered (default true)
* @param int|string $passthruLevel Minimum level to always flush to handler on close, even if strategy not triggered
*/
public function __construct($handler, $activationStrategy = null, $bufferSize = 0, bool $bubble = true, $stopBuffering = true, $passthruLevel = null)
public function __construct($handler, $activationStrategy = null, int $bufferSize = 0, bool $bubble = true, bool $stopBuffering = true, $passthruLevel = null)
{
if (null === $activationStrategy) {
$activationStrategy = new ErrorLevelActivationStrategy(Logger::WARNING);
@@ -85,7 +89,7 @@ class FingersCrossedHandler extends Handler implements ProcessableHandlerInterfa
/**
* Manually activate this logger regardless of the activation strategy
*/
public function activate()
public function activate(): void
{
if ($this->stopBuffering) {
$this->buffering = false;
@@ -152,7 +156,7 @@ class FingersCrossedHandler extends Handler implements ProcessableHandlerInterfa
*
* It also resets the handler to its initial buffering state.
*/
public function clear()
public function clear(): void
{
$this->buffer = [];
$this->reset();
@@ -161,7 +165,7 @@ class FingersCrossedHandler extends Handler implements ProcessableHandlerInterfa
/**
* Resets the state of the handler. Stops forwarding records to the wrapped handler.
*/
private function flushBuffer()
private function flushBuffer(): void
{
if (null !== $this->passthruLevel) {
$level = $this->passthruLevel;

View File

@@ -98,9 +98,8 @@ class FirePHPHandler extends AbstractProcessingHandler
*
* @see createHeader()
* @see sendHeader()
* @return array
*/
protected function getInitHeaders()
protected function getInitHeaders(): array
{
// Initial payload consists of required headers for Wildfire
return array_merge(
@@ -112,11 +111,8 @@ class FirePHPHandler extends AbstractProcessingHandler
/**
* Send header string to the client
*
* @param string $header
* @param string $content
*/
protected function sendHeader($header, $content)
protected function sendHeader(string $header, string $content): void
{
if (!headers_sent() && self::$sendHeaders) {
header(sprintf('%s: %s', $header, $content));
@@ -158,10 +154,8 @@ class FirePHPHandler extends AbstractProcessingHandler
/**
* Verifies if the headers are accepted by the current user agent
*
* @return bool
*/
protected function headersAccepted()
protected function headersAccepted(): bool
{
if (!empty($_SERVER['HTTP_USER_AGENT']) && preg_match('{\bFirePHP/\d+\.\d+\b}', $_SERVER['HTTP_USER_AGENT'])) {
return true;

View File

@@ -136,7 +136,7 @@ class TestChromePHPHandler extends ChromePHPHandler
{
protected $headers = [];
public static function resetStatic()
public static function resetStatic(): void
{
self::$initialized = false;
self::$overflowed = false;
@@ -144,12 +144,12 @@ class TestChromePHPHandler extends ChromePHPHandler
self::$json['rows'] = [];
}
protected function sendHeader($header, $content)
protected function sendHeader(string $header, string $content): void
{
$this->headers[$header] = $content;
}
public function getHeaders()
public function getHeaders(): array
{
return $this->headers;
}

View File

@@ -77,19 +77,19 @@ class TestFirePHPHandler extends FirePHPHandler
{
protected $headers = [];
public static function resetStatic()
public static function resetStatic(): void
{
self::$initialized = false;
self::$sendHeaders = true;
self::$messageIndex = 1;
}
protected function sendHeader($header, $content)
protected function sendHeader(string $header, string $content): void
{
$this->headers[$header] = $content;
}
public function getHeaders()
public function getHeaders(): array
{
return $this->headers;
}