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

Add Record/Level/LevelName type aliases and improve phpstan type coverage to level 6

This commit is contained in:
Jordi Boggiano
2021-05-28 10:29:33 +02:00
parent 5c9d9bf43e
commit 01d104aa78
88 changed files with 791 additions and 180 deletions

View File

@@ -21,8 +21,10 @@ use Monolog\Logger;
*/
class GitProcessor implements ProcessorInterface
{
/** @var int */
private $level;
private static $cache;
/** @var array{branch: string, commit: string}|array<never>|null */
private static $cache = null;
/**
* @param string|int $level The minimum logging level at which this Processor will be triggered
@@ -32,6 +34,9 @@ class GitProcessor implements ProcessorInterface
$this->level = Logger::toMonologLevel($level);
}
/**
* {@inheritDoc}
*/
public function __invoke(array $record): array
{
// return if the level is not high enough
@@ -44,6 +49,9 @@ class GitProcessor implements ProcessorInterface
return $record;
}
/**
* @return array{branch: string, commit: string}|array<never>
*/
private static function getGitInfo(): array
{
if (self::$cache) {

View File

@@ -16,6 +16,7 @@ namespace Monolog\Processor;
*/
class HostnameProcessor implements ProcessorInterface
{
/** @var string */
private static $host;
public function __construct()
@@ -23,6 +24,9 @@ class HostnameProcessor implements ProcessorInterface
self::$host = (string) gethostname();
}
/**
* {@inheritDoc}
*/
public function __invoke(array $record): array
{
$record['extra']['hostname'] = self::$host;

View File

@@ -26,19 +26,21 @@ use Monolog\Logger;
*/
class IntrospectionProcessor implements ProcessorInterface
{
/** @var int */
private $level;
/** @var string[] */
private $skipClassesPartials;
/** @var int */
private $skipStackFramesCount;
/** @var string[] */
private $skipFunctions = [
'call_user_func',
'call_user_func_array',
];
/**
* @param string|int $level The minimum logging level at which this Processor will be triggered
* @param string|int $level The minimum logging level at which this Processor will be triggered
* @param string[] $skipClassesPartials
*/
public function __construct($level = Logger::DEBUG, array $skipClassesPartials = [], int $skipStackFramesCount = 0)
{
@@ -47,6 +49,9 @@ class IntrospectionProcessor implements ProcessorInterface
$this->skipStackFramesCount = $skipStackFramesCount;
}
/**
* {@inheritDoc}
*/
public function __invoke(array $record): array
{
// return if the level is not high enough
@@ -97,7 +102,10 @@ class IntrospectionProcessor implements ProcessorInterface
return $record;
}
private function isTraceClassOrSkippedFunction(array $trace, int $index)
/**
* @param array[] $trace
*/
private function isTraceClassOrSkippedFunction(array $trace, int $index): bool
{
if (!isset($trace[$index])) {
return false;

View File

@@ -19,6 +19,9 @@ namespace Monolog\Processor;
*/
class MemoryPeakUsageProcessor extends MemoryProcessor
{
/**
* {@inheritDoc}
*/
public function __invoke(array $record): array
{
$usage = memory_get_peak_usage($this->realUsage);

View File

@@ -19,6 +19,9 @@ namespace Monolog\Processor;
*/
class MemoryUsageProcessor extends MemoryProcessor
{
/**
* {@inheritDoc}
*/
public function __invoke(array $record): array
{
$usage = memory_get_usage($this->realUsage);

View File

@@ -20,8 +20,10 @@ use Monolog\Logger;
*/
class MercurialProcessor implements ProcessorInterface
{
/** @var int */
private $level;
private static $cache;
/** @var array{branch: string, revision: string}|array<never>|null */
private static $cache = null;
/**
* @param string|int $level The minimum logging level at which this Processor will be triggered
@@ -31,6 +33,9 @@ class MercurialProcessor implements ProcessorInterface
$this->level = Logger::toMonologLevel($level);
}
/**
* {@inheritDoc}
*/
public function __invoke(array $record): array
{
// return if the level is not high enough
@@ -43,6 +48,9 @@ class MercurialProcessor implements ProcessorInterface
return $record;
}
/**
* @return array{branch: string, revision: string}|array<never>
*/
private static function getMercurialInfo(): array
{
if (self::$cache) {

View File

@@ -18,6 +18,9 @@ namespace Monolog\Processor;
*/
class ProcessIdProcessor implements ProcessorInterface
{
/**
* {@inheritDoc}
*/
public function __invoke(array $record): array
{
$record['extra']['process_id'] = getmypid();

View File

@@ -15,11 +15,16 @@ namespace Monolog\Processor;
* An optional interface to allow labelling Monolog processors.
*
* @author Nicolas Grekas <p@tchwork.com>
*
* @phpstan-import-type Record from \Monolog\Logger
*/
interface ProcessorInterface
{
/**
* @return array The processed record
*
* @phpstan-param Record $record
* @phpstan-return Record
*/
public function __invoke(array $record);
}

View File

@@ -41,8 +41,7 @@ class PsrLogMessageProcessor implements ProcessorInterface
}
/**
* @param array $record
* @return array
* {@inheritDoc}
*/
public function __invoke(array $record): array
{

View File

@@ -18,13 +18,20 @@ namespace Monolog\Processor;
*/
class TagProcessor implements ProcessorInterface
{
/** @var string[] */
private $tags;
/**
* @param string[] $tags
*/
public function __construct(array $tags = [])
{
$this->setTags($tags);
}
/**
* @param string[] $tags
*/
public function addTags(array $tags = []): self
{
$this->tags = array_merge($this->tags, $tags);
@@ -32,6 +39,9 @@ class TagProcessor implements ProcessorInterface
return $this;
}
/**
* @param string[] $tags
*/
public function setTags(array $tags = []): self
{
$this->tags = $tags;
@@ -39,6 +49,9 @@ class TagProcessor implements ProcessorInterface
return $this;
}
/**
* {@inheritDoc}
*/
public function __invoke(array $record): array
{
$record['extra']['tags'] = $this->tags;

View File

@@ -20,6 +20,7 @@ use Monolog\ResettableInterface;
*/
class UidProcessor implements ProcessorInterface, ResettableInterface
{
/** @var string */
private $uid;
public function __construct(int $length = 7)
@@ -31,6 +32,9 @@ class UidProcessor implements ProcessorInterface, ResettableInterface
$this->uid = $this->generateUid($length);
}
/**
* {@inheritDoc}
*/
public function __invoke(array $record): array
{
$record['extra']['uid'] = $this->uid;

View File

@@ -19,7 +19,7 @@ namespace Monolog\Processor;
class WebProcessor implements ProcessorInterface
{
/**
* @var array|\ArrayAccess
* @var array<string, mixed>|\ArrayAccess<string, mixed>
*/
protected $serverData;
@@ -28,7 +28,7 @@ class WebProcessor implements ProcessorInterface
*
* Array is structured as [key in record.extra => key in $serverData]
*
* @var array
* @var array<string, string>
*/
protected $extraFields = [
'url' => 'REQUEST_URI',
@@ -39,8 +39,8 @@ class WebProcessor implements ProcessorInterface
];
/**
* @param array|\ArrayAccess|null $serverData Array or object w/ ArrayAccess that provides access to the $_SERVER data
* @param array|null $extraFields Field names and the related key inside $serverData to be added. If not provided it defaults to: url, ip, http_method, server, referrer
* @param array<string, mixed>|\ArrayAccess<string, mixed>|null $serverData Array or object w/ ArrayAccess that provides access to the $_SERVER data
* @param array<string, string>|null $extraFields Field names and the related key inside $serverData to be added. If not provided it defaults to: url, ip, http_method, server, referrer
*/
public function __construct($serverData = null, array $extraFields = null)
{
@@ -69,6 +69,9 @@ class WebProcessor implements ProcessorInterface
}
}
/**
* {@inheritDoc}
*/
public function __invoke(array $record): array
{
// skip processing if for some reason request data
@@ -89,6 +92,10 @@ class WebProcessor implements ProcessorInterface
return $this;
}
/**
* @param mixed[] $extra
* @return mixed[]
*/
private function appendExtraFields(array $extra): array
{
foreach ($this->extraFields as $extraName => $serverName) {