Skip to content

Commit

Permalink
Remove LevelName enum in favor of a Level::getName method, fixes #1667 (
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed May 10, 2022
1 parent d381140 commit 1dacc79
Show file tree
Hide file tree
Showing 65 changed files with 219 additions and 279 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 3.0.0-RC2

BC Breaks from RC1

- The `Monolog\LevelName` enum does not exist anymore, use `Monolog\Level->getName()` instead.

### 3.0.0-RC1 (2022-05-08)

This is mostly a cleanup release offering stronger type guarantees for integrators with the
Expand Down
2 changes: 1 addition & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Overall / notable changes:
The interfaces do not require a `LogRecord` return type even where it would be applicable, but if you only
support Monolog 3 in integration code I would recommend you use `LogRecord` return types wherever fitting
to ensure forward compatibility as it may be added in Monolog 4.
- Log levels are now enums [`Monolog\Level`](src/Monolog/Level.php) and [`Monolog\LevelName`](src/Monolog/LevelName.php)
- Log levels are now stored as an enum [`Monolog\Level`](src/Monolog/Level.php)
- All properties have had types added, which may require you to do so as well if you extended
a Monolog class and declared the same property.

Expand Down
3 changes: 1 addition & 2 deletions doc/04-extending.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ abstract class provided by Monolog to keep things DRY.
<?php

use Monolog\Level;
use Monolog\LevelName;
use Monolog\Logger;
use Monolog\Handler\AbstractProcessingHandler;

Expand All @@ -32,7 +31,7 @@ class PDOHandler extends AbstractProcessingHandler
private PDO $pdo;
private PDOStatement $statement;

public function __construct(PDO $pdo, int|string|Level|LevelName $level = Level::Debug, bool $bubble = true)
public function __construct(PDO $pdo, int|string|Level $level = Level::Debug, bool $bubble = true)
{
$this->pdo = $pdo;
parent::__construct($level, $bubble);
Expand Down
3 changes: 1 addition & 2 deletions doc/message-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ property | type | description
-----------|---------------------------|-------------------------------------------------------------------------------
message | string | The log message. When the `PsrLogMessageProcessor` is used this string may contain placeholders that will be replaced by variables from the context, e.g., "User {username} logged in" with `['username' => 'John']` as context will be written as "User John logged in".
level | Monolog\Level case | Severity of the log message. See log levels described in [01-usage.md](01-usage.md#log-levels).
levelName | Monolog\LevelName case | String representation of log level.
context | array | Arbitrary data passed with the construction of the message. For example the username of the current user or their IP address.
channel | string | The channel this message was logged to. This is the name that was passed when the logger was created with `new Logger($channel)`.
datetime | Monolog\DateTimeImmutable | Date and time when the message was logged. Class extends `\DateTimeImmutable`.
Expand All @@ -22,4 +21,4 @@ and can be filled by processors. The reason processors write to `extra` and not
All properties except `extra` are read-only.

> Note: For BC reasons with Monolog 1 and 2 which used arrays, `LogRecord` implements `ArrayAccess` so you can access the above properties
> using `$record['message']` for example, with the notable exception of `levelName` which must be referred to as `level_name` for BC.
> using `$record['message']` for example, with the notable exception of `level->getName()` which must be referred to as `level_name` for BC.
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ parameters:

-
message: "#^Variable property access on \\$this\\(Monolog\\\\LogRecord\\)\\.$#"
count: 5
count: 4
path: src/Monolog/LogRecord.php

-
message: "#^Parameter \\#1 \\$level \\('alert'\\|'critical'\\|'debug'\\|'emergency'\\|'error'\\|'info'\\|'notice'\\|'warning'\\|Monolog\\\\Level\\|Monolog\\\\LevelName\\) of method Monolog\\\\Logger\\:\\:log\\(\\) should be contravariant with parameter \\$level \\(mixed\\) of method Psr\\\\Log\\\\LoggerInterface\\:\\:log\\(\\)$#"
message: "#^Parameter \\#1 \\$level \\('alert'\\|'critical'\\|'debug'\\|'emergency'\\|'error'\\|'info'\\|'notice'\\|'warning'\\|Monolog\\\\Level\\) of method Monolog\\\\Logger\\:\\:log\\(\\) should be contravariant with parameter \\$level \\(mixed\\) of method Psr\\\\Log\\\\LoggerInterface\\:\\:log\\(\\)$#"
count: 1
path: src/Monolog/Logger.php

Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/Formatter/FlowdockFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function format(LogRecord $record): array
{
$tags = [
'#logs',
'#' . strtolower($record->levelName->value),
'#' . $record->level->toPsrLogLevel(),
'#' . $record->channel,
];

Expand All @@ -50,7 +50,7 @@ public function format(LogRecord $record): array
$subject = sprintf(
'in %s: %s - %s',
$this->source,
$record->levelName->value,
$record->level->getName(),
$this->getShortMessage($record->message)
);

Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/Formatter/FluentdFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function format(LogRecord $record): string
{
$tag = $record->channel;
if ($this->levelTag) {
$tag .= '.' . strtolower($record->levelName->value);
$tag .= '.' . $record->level->toPsrLogLevel();
}

$message = [
Expand All @@ -71,7 +71,7 @@ public function format(LogRecord $record): string

if (!$this->levelTag) {
$message['level'] = $record->level->value;
$message['level_name'] = $record->levelName->value;
$message['level_name'] = $record->level->getName();
}

return Utils::jsonEncode([$tag, $record->datetime->getTimestamp(), $message]);
Expand Down
2 changes: 1 addition & 1 deletion src/Monolog/Formatter/HtmlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function addTitle(string $title, Level $level): string
*/
public function format(LogRecord $record): string
{
$output = $this->addTitle($record->levelName->value, $record->level);
$output = $this->addTitle($record->level->getName(), $record->level);
$output .= '<table cellspacing="1" width="100%" class="monolog-output">';

$output .= $this->addRow('Message', $record->message);
Expand Down
13 changes: 6 additions & 7 deletions src/Monolog/Handler/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Monolog\Handler;

use Monolog\Level;
use Monolog\LevelName;
use Monolog\Logger;
use Monolog\ResettableInterface;
use Psr\Log\LogLevel;
Expand All @@ -29,12 +28,12 @@ abstract class AbstractHandler extends Handler implements ResettableInterface
protected bool $bubble = true;

/**
* @param int|string|Level|LevelName|LogLevel::* $level The minimum logging level at which this handler will be triggered
* @param int|string|Level|LogLevel::* $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
*
* @phpstan-param value-of<Level::VALUES>|value-of<LevelName::VALUES>|Level|LevelName|LogLevel::* $level
* @phpstan-param value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::* $level
*/
public function __construct(int|string|Level|LevelName $level = Level::Debug, bool $bubble = true)
public function __construct(int|string|Level $level = Level::Debug, bool $bubble = true)
{
$this->setLevel($level);
$this->bubble = $bubble;
Expand All @@ -51,11 +50,11 @@ public function isHandling(LogRecord $record): bool
/**
* Sets minimum logging level at which this handler will be triggered.
*
* @param Level|LevelName|LogLevel::* $level Level or level name
* @param Level|LogLevel::* $level Level or level name
*
* @phpstan-param value-of<Level::VALUES>|value-of<LevelName::VALUES>|Level|LevelName|LogLevel::* $level
* @phpstan-param value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::* $level
*/
public function setLevel(int|string|Level|LevelName $level): self
public function setLevel(int|string|Level $level): self
{
$this->level = Logger::toMonologLevel($level);

Expand Down
3 changes: 1 addition & 2 deletions src/Monolog/Handler/AbstractSyslogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Monolog\Level;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\LevelName;

/**
* Common syslog functionality
Expand Down Expand Up @@ -61,7 +60,7 @@ protected function toSyslogPriority(Level $level): int
/**
* @param string|int $facility Either one of the names of the keys in $this->facilities, or a LOG_* facility constant
*/
public function __construct(string|int $facility = \LOG_USER, int|string|Level|LevelName $level = Level::Debug, bool $bubble = true)
public function __construct(string|int $facility = \LOG_USER, int|string|Level $level = Level::Debug, bool $bubble = true)
{
parent::__construct($level, $bubble);

Expand Down
5 changes: 2 additions & 3 deletions src/Monolog/Handler/AmqpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Monolog\Level;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\JsonFormatter;
use Monolog\LevelName;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Channel\AMQPChannel;
use AMQPExchange;
Expand All @@ -30,7 +29,7 @@ class AmqpHandler extends AbstractProcessingHandler
* @param AMQPExchange|AMQPChannel $exchange AMQPExchange (php AMQP ext) or PHP AMQP lib channel, ready for use
* @param string|null $exchangeName Optional exchange name, for AMQPChannel (PhpAmqpLib) only
*/
public function __construct(AMQPExchange|AMQPChannel $exchange, ?string $exchangeName = null, int|string|Level|LevelName $level = Level::Debug, bool $bubble = true)
public function __construct(AMQPExchange|AMQPChannel $exchange, ?string $exchangeName = null, int|string|Level $level = Level::Debug, bool $bubble = true)
{
if ($exchange instanceof AMQPChannel) {
$this->exchangeName = (string) $exchangeName;
Expand Down Expand Up @@ -103,7 +102,7 @@ public function handleBatch(array $records): void
*/
protected function getRoutingKey(LogRecord $record): string
{
$routingKey = sprintf('%s.%s', $record->levelName->value, $record->channel);
$routingKey = sprintf('%s.%s', $record->level->name, $record->channel);

return strtolower($routingKey);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Monolog/Handler/BufferHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Monolog\Handler;

use Monolog\Level;
use Monolog\LevelName;
use Monolog\ResettableInterface;
use Monolog\Formatter\FormatterInterface;
use Monolog\LogRecord;
Expand Down Expand Up @@ -47,7 +46,7 @@ class BufferHandler extends AbstractHandler implements ProcessableHandlerInterfa
* @param int $bufferLimit How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
* @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, int $bufferLimit = 0, int|string|Level|LevelName $level = Level::Debug, bool $bubble = true, bool $flushOnOverflow = false)
public function __construct(HandlerInterface $handler, int $bufferLimit = 0, int|string|Level $level = Level::Debug, bool $bubble = true, bool $flushOnOverflow = false)
{
parent::__construct($level, $bubble);
$this->handler = $handler;
Expand Down
3 changes: 1 addition & 2 deletions src/Monolog/Handler/ChromePHPHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Monolog\Formatter\ChromePHPFormatter;
use Monolog\Formatter\FormatterInterface;
use Monolog\Level;
use Monolog\LevelName;
use Monolog\Utils;
use Monolog\LogRecord;
use Monolog\DateTimeImmutable;
Expand Down Expand Up @@ -63,7 +62,7 @@ class ChromePHPHandler extends AbstractProcessingHandler

protected static bool $sendHeaders = true;

public function __construct(int|string|Level|LevelName $level = Level::Debug, bool $bubble = true)
public function __construct(int|string|Level $level = Level::Debug, bool $bubble = true)
{
parent::__construct($level, $bubble);
if (!function_exists('json_encode')) {
Expand Down
3 changes: 1 addition & 2 deletions src/Monolog/Handler/CouchDBHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\JsonFormatter;
use Monolog\Level;
use Monolog\LevelName;
use Monolog\LogRecord;

/**
Expand Down Expand Up @@ -49,7 +48,7 @@ class CouchDBHandler extends AbstractProcessingHandler
*
* @phpstan-param InputOptions $options
*/
public function __construct(array $options = [], int|string|Level|LevelName $level = Level::Debug, bool $bubble = true)
public function __construct(array $options = [], int|string|Level $level = Level::Debug, bool $bubble = true)
{
$this->options = array_merge([
'host' => 'localhost',
Expand Down
3 changes: 1 addition & 2 deletions src/Monolog/Handler/CubeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Monolog\Handler;

use Monolog\Level;
use Monolog\LevelName;
use Monolog\Utils;
use Monolog\LogRecord;

Expand All @@ -39,7 +38,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(string $url, int|string|Level|LevelName $level = Level::Debug, bool $bubble = true)
public function __construct(string $url, int|string|Level $level = Level::Debug, bool $bubble = true)
{
$urlInfo = parse_url($url);

Expand Down
11 changes: 5 additions & 6 deletions src/Monolog/Handler/DeduplicationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Monolog\Handler;

use Monolog\Level;
use Monolog\LevelName;
use Monolog\Logger;
use Psr\Log\LogLevel;
use Monolog\LogRecord;
Expand Down Expand Up @@ -50,13 +49,13 @@ class DeduplicationHandler extends BufferHandler
/**
* @param HandlerInterface $handler Handler.
* @param string $deduplicationStore The file/path where the deduplication log should be kept
* @param int|string|Level|LevelName|LogLevel::* $deduplicationLevel The minimum logging level for log records to be looked at for deduplication purposes
* @param int|string|Level|LogLevel::* $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
*
* @phpstan-param value-of<Level::VALUES>|value-of<LevelName::VALUES>|Level|LevelName|LogLevel::* $deduplicationLevel
* @phpstan-param value-of<Level::VALUES>|value-of<Level::NAMES>|Level|LogLevel::* $deduplicationLevel
*/
public function __construct(HandlerInterface $handler, ?string $deduplicationStore = null, int|string|Level|LevelName $deduplicationLevel = Level::Error, int $time = 60, bool $bubble = true)
public function __construct(HandlerInterface $handler, ?string $deduplicationStore = null, int|string|Level $deduplicationLevel = Level::Error, int $time = 60, bool $bubble = true)
{
parent::__construct($handler, 0, Level::Debug, $bubble, false);

Expand Down Expand Up @@ -112,7 +111,7 @@ private function isDuplicate(LogRecord $record): bool
for ($i = count($store) - 1; $i >= 0; $i--) {
list($timestamp, $level, $message) = explode(':', $store[$i], 3);

if ($level === $record->levelName->value && $message === $expectedMessage && $timestamp > $timestampValidity) {
if ($level === $record->level->getName() && $message === $expectedMessage && $timestamp > $timestampValidity) {
return true;
}

Expand Down Expand Up @@ -162,6 +161,6 @@ private function collectLogs(): void

private function appendRecord(LogRecord $record): void
{
file_put_contents($this->deduplicationStore, $record->datetime->getTimestamp() . ':' . $record->levelName->value . ':' . preg_replace('{[\r\n].*}', '', $record->message) . "\n", FILE_APPEND);
file_put_contents($this->deduplicationStore, $record->datetime->getTimestamp() . ':' . $record->level->getName() . ':' . preg_replace('{[\r\n].*}', '', $record->message) . "\n", FILE_APPEND);
}
}
3 changes: 1 addition & 2 deletions src/Monolog/Handler/DoctrineCouchDBHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Monolog\Formatter\NormalizerFormatter;
use Monolog\Formatter\FormatterInterface;
use Doctrine\CouchDB\CouchDBClient;
use Monolog\LevelName;
use Monolog\LogRecord;

/**
Expand All @@ -27,7 +26,7 @@ class DoctrineCouchDBHandler extends AbstractProcessingHandler
{
private CouchDBClient $client;

public function __construct(CouchDBClient $client, int|string|Level|LevelName $level = Level::Debug, bool $bubble = true)
public function __construct(CouchDBClient $client, int|string|Level $level = Level::Debug, bool $bubble = true)
{
$this->client = $client;
parent::__construct($level, $bubble);
Expand Down
3 changes: 1 addition & 2 deletions src/Monolog/Handler/DynamoDbHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Aws\DynamoDb\Marshaler;
use Monolog\Formatter\ScalarFormatter;
use Monolog\Level;
use Monolog\LevelName;
use Monolog\LogRecord;

/**
Expand All @@ -36,7 +35,7 @@ class DynamoDbHandler extends AbstractProcessingHandler

protected Marshaler $marshaler;

public function __construct(DynamoDbClient $client, string $table, int|string|Level|LevelName $level = Level::Debug, bool $bubble = true)
public function __construct(DynamoDbClient $client, string $table, int|string|Level $level = Level::Debug, bool $bubble = true)
{
$this->marshaler = new Marshaler;

Expand Down
3 changes: 1 addition & 2 deletions src/Monolog/Handler/ElasticaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Monolog\Level;
use Elastica\Client;
use Elastica\Exception\ExceptionInterface;
use Monolog\LevelName;
use Monolog\LogRecord;

/**
Expand Down Expand Up @@ -62,7 +61,7 @@ class ElasticaHandler extends AbstractProcessingHandler
*
* @phpstan-param InputOptions $options
*/
public function __construct(Client $client, array $options = [], int|string|Level|LevelName $level = Level::Debug, bool $bubble = true)
public function __construct(Client $client, array $options = [], int|string|Level $level = Level::Debug, bool $bubble = true)
{
parent::__construct($level, $bubble);
$this->client = $client;
Expand Down
3 changes: 1 addition & 2 deletions src/Monolog/Handler/ElasticsearchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Monolog\Handler;

use Monolog\LevelName;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Throwable;
use RuntimeException;
Expand Down Expand Up @@ -77,7 +76,7 @@ class ElasticsearchHandler extends AbstractProcessingHandler
*
* @phpstan-param InputOptions $options
*/
public function __construct(Client|Client8 $client, array $options = [], int|string|Level|LevelName $level = Level::Debug, bool $bubble = true)
public function __construct(Client|Client8 $client, array $options = [], int|string|Level $level = Level::Debug, bool $bubble = true)
{
parent::__construct($level, $bubble);
$this->client = $client;
Expand Down
3 changes: 1 addition & 2 deletions src/Monolog/Handler/ErrorLogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\FormatterInterface;
use Monolog\Level;
use Monolog\LevelName;
use Monolog\Utils;
use Monolog\LogRecord;

Expand All @@ -35,7 +34,7 @@ class ErrorLogHandler extends AbstractProcessingHandler
* @param int $messageType Says where the error should go.
* @param bool $expandNewlines If set to true, newlines in the message will be expanded to be take multiple log entries
*/
public function __construct(int $messageType = self::OPERATING_SYSTEM, int|string|Level|LevelName $level = Level::Debug, bool $bubble = true, bool $expandNewlines = false)
public function __construct(int $messageType = self::OPERATING_SYSTEM, int|string|Level $level = Level::Debug, bool $bubble = true, bool $expandNewlines = false)
{
parent::__construct($level, $bubble);

Expand Down

0 comments on commit 1dacc79

Please sign in to comment.