mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-04 12:17:35 +02:00
More type hints on some handler classes
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
* Added ability to customize error handling at the Logger level using Logger::setExceptionHandler
|
||||
* Added InsightOpsHandler to migrate users of the LogEntriesHandler
|
||||
* Added protection to NormalizerHandler against circular and very deep structures, it now stops normalizing at a depth of 9
|
||||
* Added protection to NormalizerFormatter against circular and very deep structures, it now stops normalizing at a depth of 9
|
||||
* Added capture of stack traces to ErrorHandler when logging PHP errors
|
||||
* Added forwarding of context info to FluentdFormatter
|
||||
* Added SocketHandler::setChunkSize to override the default chunk size in case you must send large log lines to rsyslog for example
|
||||
|
@@ -47,9 +47,9 @@ class HtmlFormatter extends NormalizerFormatter
|
||||
/**
|
||||
* Creates an HTML table row
|
||||
*
|
||||
* @param string $th Row header content
|
||||
* @param string $td Row standard cell content
|
||||
* @param bool $escapeTd false if td content must not be html escaped
|
||||
* @param string $th Row header content
|
||||
* @param string $td Row standard cell content
|
||||
* @param bool $escapeTd false if td content must not be html escaped
|
||||
*/
|
||||
protected function addRow(string $th, string $td = ' ', bool $escapeTd = true): string
|
||||
{
|
||||
|
@@ -54,9 +54,9 @@ abstract class AbstractSyslogHandler extends AbstractProcessingHandler
|
||||
];
|
||||
|
||||
/**
|
||||
* @param mixed $facility
|
||||
* @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 mixed $facility
|
||||
* @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($facility = LOG_USER, $level = Logger::DEBUG, bool $bubble = true)
|
||||
{
|
||||
|
@@ -33,10 +33,10 @@ class AmqpHandler extends AbstractProcessingHandler
|
||||
/**
|
||||
* @param AMQPExchange|AMQPChannel $exchange AMQPExchange (php AMQP ext) or PHP AMQP lib channel, ready for use
|
||||
* @param string $exchangeName Optional exchange name, for AMQPChannel (PhpAmqpLib) only
|
||||
* @param int $level
|
||||
* @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($exchange, $exchangeName = null, $level = Logger::DEBUG, bool $bubble = true)
|
||||
public function __construct($exchange, ?string $exchangeName = null, $level = Logger::DEBUG, bool $bubble = true)
|
||||
{
|
||||
if ($exchange instanceof AMQPChannel) {
|
||||
$this->exchangeName = $exchangeName;
|
||||
@@ -108,25 +108,18 @@ class AmqpHandler extends AbstractProcessingHandler
|
||||
|
||||
/**
|
||||
* Gets the routing key for the AMQP exchange
|
||||
*
|
||||
* @param array $record
|
||||
* @return string
|
||||
*/
|
||||
protected function getRoutingKey(array $record)
|
||||
protected function getRoutingKey(array $record): string
|
||||
{
|
||||
$routingKey = sprintf('%s.%s', $record['level_name'], $record['channel']);
|
||||
|
||||
return strtolower($routingKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data
|
||||
* @return AMQPMessage
|
||||
*/
|
||||
private function createAmqpMessage($data)
|
||||
private function createAmqpMessage(string $data): AMQPMessage
|
||||
{
|
||||
return new AMQPMessage(
|
||||
(string) $data,
|
||||
$data,
|
||||
[
|
||||
'delivery_mode' => 2,
|
||||
'content_type' => 'application/json',
|
||||
|
@@ -20,9 +20,6 @@ interface ActivationStrategyInterface
|
||||
{
|
||||
/**
|
||||
* Returns whether the given record activates the handler.
|
||||
*
|
||||
* @param array $record
|
||||
* @return bool
|
||||
*/
|
||||
public function isHandlerActivated(array $record);
|
||||
public function isHandlerActivated(array $record): bool;
|
||||
}
|
||||
|
@@ -39,8 +39,8 @@ class ChannelLevelActivationStrategy implements ActivationStrategyInterface
|
||||
private $channelToActionLevel;
|
||||
|
||||
/**
|
||||
* @param int $defaultActionLevel The default action level to be used if the record's category doesn't match any
|
||||
* @param array $channelToActionLevel An array that maps channel names to action levels.
|
||||
* @param int|string $defaultActionLevel The default action level to be used if the record's category doesn't match any
|
||||
* @param array $channelToActionLevel An array that maps channel names to action levels.
|
||||
*/
|
||||
public function __construct($defaultActionLevel, $channelToActionLevel = [])
|
||||
{
|
||||
@@ -48,7 +48,7 @@ class ChannelLevelActivationStrategy implements ActivationStrategyInterface
|
||||
$this->channelToActionLevel = array_map('Monolog\Logger::toMonologLevel', $channelToActionLevel);
|
||||
}
|
||||
|
||||
public function isHandlerActivated(array $record)
|
||||
public function isHandlerActivated(array $record): bool
|
||||
{
|
||||
if (isset($this->channelToActionLevel[$record['channel']])) {
|
||||
return $record['level'] >= $this->channelToActionLevel[$record['channel']];
|
||||
|
@@ -22,12 +22,15 @@ class ErrorLevelActivationStrategy implements ActivationStrategyInterface
|
||||
{
|
||||
private $actionLevel;
|
||||
|
||||
/**
|
||||
* @param int|string $actionLevel Level or name or value
|
||||
*/
|
||||
public function __construct($actionLevel)
|
||||
{
|
||||
$this->actionLevel = Logger::toMonologLevel($actionLevel);
|
||||
}
|
||||
|
||||
public function isHandlerActivated(array $record)
|
||||
public function isHandlerActivated(array $record): bool
|
||||
{
|
||||
return $record['level'] >= $this->actionLevel;
|
||||
}
|
||||
|
@@ -52,6 +52,8 @@ trait FormattableHandlerTrait
|
||||
/**
|
||||
* Gets the default formatter.
|
||||
*
|
||||
* Overwrite this if the LineFormatter is not a good default for your handler.
|
||||
*
|
||||
* @return FormatterInterface
|
||||
*/
|
||||
protected function getDefaultFormatter(): FormatterInterface
|
||||
|
@@ -13,7 +13,7 @@ namespace Monolog\Handler;
|
||||
|
||||
use Monolog\Logger;
|
||||
|
||||
/**
|
||||
/**
|
||||
* Inspired on LogEntriesHandler.
|
||||
*
|
||||
* @author Robert Kaufmann III <rok3@rok3.me>
|
||||
|
@@ -48,11 +48,8 @@ trait ProcessableHandlerTrait
|
||||
|
||||
/**
|
||||
* Processes a record.
|
||||
*
|
||||
* @param array $record
|
||||
* @return array
|
||||
*/
|
||||
protected function processRecord(array $record)
|
||||
protected function processRecord(array $record): array
|
||||
{
|
||||
foreach ($this->processors as $processor) {
|
||||
$record = $processor($record);
|
||||
|
@@ -35,19 +35,19 @@ class SlackRecord
|
||||
|
||||
/**
|
||||
* Slack channel (encoded ID or name)
|
||||
* @var string|null
|
||||
* @var ?string
|
||||
*/
|
||||
private $channel;
|
||||
|
||||
/**
|
||||
* Name of a bot
|
||||
* @var string|null
|
||||
* @var ?string
|
||||
*/
|
||||
private $username;
|
||||
|
||||
/**
|
||||
* User icon e.g. 'ghost', 'http://example.com/user.png'
|
||||
* @var string
|
||||
* @var ?string
|
||||
*/
|
||||
private $userIcon;
|
||||
|
||||
@@ -85,7 +85,7 @@ class SlackRecord
|
||||
*/
|
||||
private $normalizerFormatter;
|
||||
|
||||
public function __construct($channel = null, $username = null, $useAttachment = true, $userIcon = null, $useShortAttachment = false, $includeContextAndExtra = false, array $excludeFields = array(), FormatterInterface $formatter = null)
|
||||
public function __construct(?string $channel = null, ?string $username = null, bool $useAttachment = true, ?string $userIcon = null, bool $useShortAttachment = false, bool $includeContextAndExtra = false, array $excludeFields = array(), FormatterInterface $formatter = null)
|
||||
{
|
||||
$this->channel = $channel;
|
||||
$this->username = $username;
|
||||
@@ -101,7 +101,7 @@ class SlackRecord
|
||||
}
|
||||
}
|
||||
|
||||
public function getSlackData(array $record)
|
||||
public function getSlackData(array $record): array
|
||||
{
|
||||
$dataArray = array();
|
||||
$record = $this->excludeFields($record);
|
||||
@@ -177,11 +177,8 @@ class SlackRecord
|
||||
/**
|
||||
* Returned a Slack message attachment color associated with
|
||||
* provided level.
|
||||
*
|
||||
* @param int $level
|
||||
* @return string
|
||||
*/
|
||||
public function getAttachmentColor($level)
|
||||
public function getAttachmentColor(int $level): string
|
||||
{
|
||||
switch (true) {
|
||||
case $level >= Logger::ERROR:
|
||||
@@ -197,12 +194,8 @@ class SlackRecord
|
||||
|
||||
/**
|
||||
* Stringifies an array of key/value pairs to be used in attachment fields
|
||||
*
|
||||
* @param array $fields
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function stringify($fields)
|
||||
public function stringify(array $fields): string
|
||||
{
|
||||
$normalized = $this->normalizerFormatter->format($fields);
|
||||
$prettyPrintFlag = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 128;
|
||||
@@ -215,25 +208,19 @@ class SlackRecord
|
||||
: json_encode($normalized, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the formatter
|
||||
*
|
||||
* @param FormatterInterface $formatter
|
||||
*/
|
||||
public function setFormatter(FormatterInterface $formatter)
|
||||
public function setFormatter(FormatterInterface $formatter): self
|
||||
{
|
||||
$this->formatter = $formatter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates attachment field
|
||||
*
|
||||
* @param string $title
|
||||
* @param string|array $value
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function generateAttachmentField($title, $value)
|
||||
private function generateAttachmentField(string $title, $value): array
|
||||
{
|
||||
$value = is_array($value)
|
||||
? sprintf('```%s```', $this->stringify($value))
|
||||
@@ -248,12 +235,8 @@ class SlackRecord
|
||||
|
||||
/**
|
||||
* Generates a collection of attachment fields from array
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function generateAttachmentFields(array $data)
|
||||
private function generateAttachmentFields(array $data): array
|
||||
{
|
||||
$fields = array();
|
||||
foreach ($this->normalizerFormatter->format($data) as $key => $value) {
|
||||
@@ -265,12 +248,8 @@ class SlackRecord
|
||||
|
||||
/**
|
||||
* Get a copy of record with fields excluded according to $this->excludeFields
|
||||
*
|
||||
* @param array $record
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function excludeFields(array $record)
|
||||
private function excludeFields(array $record): array
|
||||
{
|
||||
foreach ($this->excludeFields as $field) {
|
||||
$keys = explode('.', $field);
|
||||
|
@@ -15,13 +15,14 @@ class UdpSocket
|
||||
{
|
||||
const DATAGRAM_MAX_LENGTH = 65023;
|
||||
|
||||
/** @var string */
|
||||
protected $ip;
|
||||
/** @var int */
|
||||
protected $port;
|
||||
|
||||
/** @var resource|null */
|
||||
protected $socket;
|
||||
|
||||
public function __construct($ip, $port = 514)
|
||||
public function __construct(string $ip, int $port = 514)
|
||||
{
|
||||
$this->ip = $ip;
|
||||
$this->port = $port;
|
||||
@@ -41,7 +42,7 @@ class UdpSocket
|
||||
}
|
||||
}
|
||||
|
||||
protected function send($chunk)
|
||||
protected function send(string $chunk): void
|
||||
{
|
||||
if (!is_resource($this->socket)) {
|
||||
throw new \RuntimeException('The UdpSocket to '.$this->ip.':'.$this->port.' has been closed and can not be written to anymore');
|
||||
@@ -49,7 +50,7 @@ class UdpSocket
|
||||
socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port);
|
||||
}
|
||||
|
||||
protected function assembleMessage($line, $header)
|
||||
protected function assembleMessage(string $line, string $header): string
|
||||
{
|
||||
$chunkSize = self::DATAGRAM_MAX_LENGTH - strlen($header);
|
||||
|
||||
|
@@ -360,7 +360,7 @@ class Logger implements LoggerInterface
|
||||
/**
|
||||
* Converts PSR-3 levels to Monolog ones if necessary
|
||||
*
|
||||
* @param string|int $level Level number (monolog) or name (PSR-3)
|
||||
* @param string|int $level Level number (monolog) or name (PSR-3)
|
||||
* @throws \Psr\Log\InvalidArgumentException If level is not defined
|
||||
*/
|
||||
public static function toMonologLevel($level): int
|
||||
|
Reference in New Issue
Block a user