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

Convert level/levelName to enums (#1656)

This commit is contained in:
Jordi Boggiano
2022-04-19 21:49:03 +02:00
committed by GitHub
parent 248673e858
commit 2d006a8472
138 changed files with 1297 additions and 1216 deletions

View File

@@ -11,7 +11,7 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\Level;
use Monolog\Test\TestCase;
class ChromePHPFormatterTest extends TestCase
@@ -23,7 +23,7 @@ class ChromePHPFormatterTest extends TestCase
{
$formatter = new ChromePHPFormatter();
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['from' => 'logger'],
@@ -55,7 +55,7 @@ class ChromePHPFormatterTest extends TestCase
{
$formatter = new ChromePHPFormatter();
$record = $this->getRecord(
Logger::CRITICAL,
Level::Critical,
'log',
channel: 'meh',
context: ['from' => 'logger'],
@@ -87,7 +87,7 @@ class ChromePHPFormatterTest extends TestCase
{
$formatter = new ChromePHPFormatter();
$record = $this->getRecord(
Logger::DEBUG,
Level::Debug,
'log',
channel: 'meh',
datetime: new \DateTimeImmutable("@0"),
@@ -114,13 +114,13 @@ class ChromePHPFormatterTest extends TestCase
$formatter = new ChromePHPFormatter();
$records = [
$this->getRecord(
Logger::INFO,
Level::Info,
'log',
channel: 'meh',
datetime: new \DateTimeImmutable("@0"),
),
$this->getRecord(
Logger::WARNING,
Level::Warning,
'log2',
channel: 'foo',
datetime: new \DateTimeImmutable("@0"),

View File

@@ -11,7 +11,7 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\Level;
use Monolog\LogRecord;
use Monolog\Test\TestCase;
@@ -33,7 +33,7 @@ class ElasticaFormatterTest extends TestCase
{
// test log message
$msg = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['foo' => 7, 'bar', 'class' => new \stdClass],

View File

@@ -11,7 +11,7 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\Level;
use Monolog\Test\TestCase;
class ElasticsearchFormatterTest extends TestCase
@@ -25,7 +25,7 @@ class ElasticsearchFormatterTest extends TestCase
{
// Test log message
$msg = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['foo' => 7, 'bar', 'class' => new \stdClass],

View File

@@ -11,7 +11,7 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\Level;
use Monolog\Test\TestCase;
class FlowdockFormatterTest extends TestCase
@@ -44,8 +44,8 @@ class FlowdockFormatterTest extends TestCase
{
$formatter = new FlowdockFormatter('test_source', 'source@test.com');
$records = [
$this->getRecord(Logger::WARNING),
$this->getRecord(Logger::DEBUG),
$this->getRecord(Level::Warning),
$this->getRecord(Level::Debug),
];
$formatted = $formatter->formatBatch($records);

View File

@@ -11,7 +11,7 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\Level;
use Monolog\Test\TestCase;
class FluentdFormatterTest extends TestCase
@@ -35,7 +35,7 @@ class FluentdFormatterTest extends TestCase
*/
public function testFormat()
{
$record = $this->getRecord(Logger::WARNING, datetime: new \DateTimeImmutable("@0"));
$record = $this->getRecord(Level::Warning, datetime: new \DateTimeImmutable("@0"));
$formatter = new FluentdFormatter();
$this->assertEquals(
@@ -49,7 +49,7 @@ class FluentdFormatterTest extends TestCase
*/
public function testFormatWithTag()
{
$record = $this->getRecord(Logger::ERROR, datetime: new \DateTimeImmutable("@0"));
$record = $this->getRecord(Level::Error, datetime: new \DateTimeImmutable("@0"));
$formatter = new FluentdFormatter(true);
$this->assertEquals(

View File

@@ -11,7 +11,7 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\Level;
use Monolog\Test\TestCase;
class GelfMessageFormatterTest extends TestCase
@@ -30,7 +30,7 @@ class GelfMessageFormatterTest extends TestCase
{
$formatter = new GelfMessageFormatter();
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
datetime: new \DateTimeImmutable("@0"),
@@ -62,7 +62,7 @@ class GelfMessageFormatterTest extends TestCase
{
$formatter = new GelfMessageFormatter();
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['from' => 'logger'],
@@ -84,7 +84,7 @@ class GelfMessageFormatterTest extends TestCase
{
$formatter = new GelfMessageFormatter();
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['from' => 'logger'],
@@ -120,7 +120,7 @@ class GelfMessageFormatterTest extends TestCase
{
$formatter = new GelfMessageFormatter();
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['from' => 'logger', 'exception' => [
@@ -146,7 +146,7 @@ class GelfMessageFormatterTest extends TestCase
{
$formatter = new GelfMessageFormatter();
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['from' => 'logger'],
@@ -179,7 +179,7 @@ class GelfMessageFormatterTest extends TestCase
{
$formatter = new GelfMessageFormatter();
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['exception' => str_repeat(' ', 32767)],
@@ -205,7 +205,7 @@ class GelfMessageFormatterTest extends TestCase
{
$formatter = new GelfMessageFormatter('LONG_SYSTEM_NAME', null, 'ctxt_', PHP_INT_MAX);
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['exception' => str_repeat(' ', 32767 * 2)],
@@ -231,7 +231,7 @@ class GelfMessageFormatterTest extends TestCase
{
$formatter = new GelfMessageFormatter();
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
str_repeat('в', 32767),
channel: 'meh',
context: ['exception' => str_repeat('а', 32767)],

View File

@@ -11,7 +11,7 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\Level;
use Monolog\LogRecord;
use Monolog\Test\TestCase;
@@ -85,8 +85,8 @@ class JsonFormatterTest extends TestCase
{
$formatter = new JsonFormatter();
$records = [
$this->getRecord(Logger::WARNING),
$this->getRecord(Logger::DEBUG),
$this->getRecord(Level::Warning),
$this->getRecord(Level::Debug),
];
$this->assertEquals(json_encode($records), $formatter->formatBatch($records));
}
@@ -99,8 +99,8 @@ class JsonFormatterTest extends TestCase
{
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES);
$records = [
$this->getRecord(Logger::WARNING),
$this->getRecord(Logger::DEBUG),
$this->getRecord(Level::Warning),
$this->getRecord(Level::Debug),
];
$expected = array_map(fn (LogRecord $record) => json_encode($record->toArray(), JSON_FORCE_OBJECT), $records);
$this->assertEquals(implode("\n", $expected), $formatter->formatBatch($records));
@@ -213,7 +213,7 @@ class JsonFormatterTest extends TestCase
private function formatRecordWithExceptionInContext(JsonFormatter $formatter, \Throwable $exception)
{
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
Level::Critical,
'foobar',
channel: 'core',
context: ['exception' => $exception],
@@ -262,7 +262,7 @@ class JsonFormatterTest extends TestCase
$largeArray = range(1, 1000);
$res = $formatter->format($this->getRecord(
Logger::CRITICAL,
Level::Critical,
'bar',
channel: 'test',
context: array($largeArray),
@@ -278,7 +278,7 @@ class JsonFormatterTest extends TestCase
$largeArray = range(1, 2000);
$res = $formatter->format($this->getRecord(
Logger::CRITICAL,
Level::Critical,
'bar',
channel: 'test',
context: array($largeArray),
@@ -293,7 +293,7 @@ class JsonFormatterTest extends TestCase
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, true, true);
$record = $formatter->format($this->getRecord(
Logger::DEBUG,
Level::Debug,
'Testing',
channel: 'test',
datetime: new \DateTimeImmutable('2022-02-22 00:00:00'),

View File

@@ -12,7 +12,7 @@
namespace Monolog\Formatter;
use Monolog\Test\TestCase;
use Monolog\Logger;
use Monolog\Level;
/**
* @covers Monolog\Formatter\LineFormatter
@@ -23,7 +23,7 @@ class LineFormatterTest extends TestCase
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format($this->getRecord(
Logger::WARNING,
Level::Warning,
'foo',
channel: 'log',
));
@@ -34,7 +34,7 @@ class LineFormatterTest extends TestCase
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format($this->getRecord(
Logger::ERROR,
Level::Error,
'foo',
channel: 'meh',
context: [
@@ -51,7 +51,7 @@ class LineFormatterTest extends TestCase
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format($this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
extra: ['ip' => '127.0.0.1'],
@@ -63,7 +63,7 @@ class LineFormatterTest extends TestCase
{
$formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra.file% %extra%\n", 'Y-m-d');
$message = $formatter->format($this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
extra: ['ip' => '127.0.0.1', 'file' => 'test'],
@@ -75,7 +75,7 @@ class LineFormatterTest extends TestCase
{
$formatter = new LineFormatter(null, 'Y-m-d', false, true);
$message = $formatter->format($this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
));
@@ -86,7 +86,7 @@ class LineFormatterTest extends TestCase
{
$formatter = new LineFormatter('%context.foo% => %extra.foo%');
$message = $formatter->format($this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['foo' => 'bar'],
@@ -100,7 +100,7 @@ class LineFormatterTest extends TestCase
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format($this->getRecord(
Logger::ERROR,
Level::Error,
'foobar',
channel: 'meh',
context: [],
@@ -114,7 +114,7 @@ class LineFormatterTest extends TestCase
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
Level::Critical,
'foobar',
channel: 'core',
context: ['exception' => new \RuntimeException('Foo')],
@@ -130,7 +130,7 @@ class LineFormatterTest extends TestCase
$formatter = new LineFormatter(null, 'Y-m-d');
$formatter->includeStacktraces();
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
Level::Critical,
'foobar',
channel: 'core',
context: ['exception' => new \RuntimeException('Foo')],
@@ -146,7 +146,7 @@ class LineFormatterTest extends TestCase
$formatter = new LineFormatter(null, 'Y-m-d');
$previous = new \LogicException('Wut?');
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
Level::Critical,
'foobar',
channel: 'core',
context: ['exception' => new \RuntimeException('Foo', 0, $previous)],
@@ -165,7 +165,7 @@ class LineFormatterTest extends TestCase
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
Level::Critical,
'foobar',
channel: 'core',
context: ['exception' => new \SoapFault('foo', 'bar', 'hello', 'world')],
@@ -176,7 +176,7 @@ class LineFormatterTest extends TestCase
$this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (SoapFault(code: 0 faultcode: foo faultactor: hello detail: world): bar at '.substr($path, 1, -1).':'.(__LINE__ - 5).')"} []'."\n", $message);
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
Level::Critical,
'foobar',
channel: 'core',
context: ['exception' => new \SoapFault('foo', 'bar', 'hello', (object) ['bar' => (object) ['biz' => 'baz'], 'foo' => 'world'])],
@@ -192,12 +192,12 @@ class LineFormatterTest extends TestCase
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->formatBatch([
$this->getRecord(
Logger::CRITICAL,
Level::Critical,
'bar',
channel: 'test',
),
$this->getRecord(
Logger::WARNING,
Level::Warning,
'foo',
channel: 'log',
),

View File

@@ -11,7 +11,8 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\Level;
use Monolog\LevelName;
use Monolog\Test\TestCase;
class LogstashFormatterTest extends TestCase
@@ -23,7 +24,7 @@ class LogstashFormatterTest extends TestCase
{
$formatter = new LogstashFormatter('test', 'hostname');
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
datetime: new \DateTimeImmutable("@0"),
@@ -35,8 +36,8 @@ class LogstashFormatterTest extends TestCase
$this->assertEquals("1", $message['@version']);
$this->assertEquals('log', $message['message']);
$this->assertEquals('meh', $message['channel']);
$this->assertEquals('ERROR', $message['level']);
$this->assertEquals(Logger::ERROR, $message['monolog_level']);
$this->assertEquals(LevelName::Error->value, $message['level']);
$this->assertEquals(Level::Error->value, $message['monolog_level']);
$this->assertEquals('test', $message['type']);
$this->assertEquals('hostname', $message['host']);
@@ -54,7 +55,7 @@ class LogstashFormatterTest extends TestCase
{
$formatter = new LogstashFormatter('test');
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['from' => 'logger'],
@@ -75,7 +76,7 @@ class LogstashFormatterTest extends TestCase
{
$formatter = new LogstashFormatter('test');
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['from' => 'logger'],
@@ -105,7 +106,7 @@ class LogstashFormatterTest extends TestCase
{
$formatter = new LogstashFormatter('test');
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['from' => 'logger'],
@@ -132,7 +133,7 @@ class LogstashFormatterTest extends TestCase
{
$formatter = new LogstashFormatter('app', 'test');
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['from' => 'logger'],
@@ -150,7 +151,7 @@ class LogstashFormatterTest extends TestCase
{
$formatter = new LogstashFormatter('test', 'hostname');
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: '¯\_(ツ)_/¯',
datetime: new \DateTimeImmutable("@0"),

View File

@@ -14,7 +14,8 @@ namespace Monolog\Formatter;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\Regex;
use MongoDB\BSON\UTCDateTime;
use Monolog\Logger;
use Monolog\Level;
use Monolog\LevelName;
use Monolog\Test\TestCase;
/**
@@ -62,7 +63,7 @@ class MongoDBFormatterTest extends TestCase
{
$record = $this->getRecord(
message: 'some log message',
level: Logger::WARNING,
level: Level::Warning,
channel: 'test',
datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
);
@@ -73,8 +74,8 @@ class MongoDBFormatterTest extends TestCase
$this->assertCount(7, $formattedRecord);
$this->assertEquals('some log message', $formattedRecord['message']);
$this->assertEquals([], $formattedRecord['context']);
$this->assertEquals(Logger::WARNING, $formattedRecord['level']);
$this->assertEquals(Logger::getLevelName(Logger::WARNING), $formattedRecord['level_name']);
$this->assertEquals(Level::Warning->value, $formattedRecord['level']);
$this->assertEquals(LevelName::Warning->value, $formattedRecord['level_name']);
$this->assertEquals('test', $formattedRecord['channel']);
$this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $formattedRecord['datetime']);
$this->assertEquals('1453410690123', $formattedRecord['datetime']->__toString());
@@ -96,7 +97,7 @@ class MongoDBFormatterTest extends TestCase
'context_int' => 123456,
'except' => new \Exception('exception message', 987),
],
level: Logger::WARNING,
level: Level::Warning,
channel: 'test',
datetime: new \DateTimeImmutable('2016-01-21T21:11:30.213000+00:00'),
);
@@ -140,7 +141,7 @@ class MongoDBFormatterTest extends TestCase
],
],
],
level: Logger::WARNING,
level: Level::Warning,
channel: 'test',
datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
);
@@ -174,7 +175,7 @@ class MongoDBFormatterTest extends TestCase
],
],
],
level: Logger::WARNING,
level: Level::Warning,
channel: 'test',
datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
);
@@ -211,7 +212,7 @@ class MongoDBFormatterTest extends TestCase
context: [
'nest2' => $someObject,
],
level: Logger::WARNING,
level: Level::Warning,
channel: 'test',
datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
);
@@ -238,7 +239,7 @@ class MongoDBFormatterTest extends TestCase
context: [
'nest2' => new \Exception('exception message', 987),
],
level: Logger::WARNING,
level: Level::Warning,
channel: 'test',
datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
);
@@ -262,7 +263,7 @@ class MongoDBFormatterTest extends TestCase
'regex' => new Regex('pattern'),
],
],
level: Logger::WARNING,
level: Level::Warning,
channel: 'test',
datetime: new \DateTimeImmutable('2016-01-21T21:11:30.123456+00:00'),
);

View File

@@ -11,8 +11,9 @@
namespace Monolog\Formatter;
use Monolog\LevelName;
use Monolog\Test\TestCase;
use Monolog\Logger;
use Monolog\Level;
/**
* @covers Monolog\Formatter\NormalizerFormatter
@@ -23,7 +24,7 @@ class NormalizerFormatterTest extends TestCase
{
$formatter = new NormalizerFormatter('Y-m-d');
$formatted = $formatter->format($this->getRecord(
Logger::ERROR,
Level::Error,
'foo',
channel: 'meh',
extra: ['foo' => new TestFooNorm, 'bar' => new TestBarNorm, 'baz' => [], 'res' => fopen('php://memory', 'rb')],
@@ -37,8 +38,8 @@ class NormalizerFormatterTest extends TestCase
));
$this->assertEquals([
'level_name' => 'ERROR',
'level' => Logger::ERROR,
'level_name' => LevelName::Error->value,
'level' => Level::Error->value,
'channel' => 'meh',
'message' => 'foo',
'datetime' => date('Y-m-d'),
@@ -142,13 +143,13 @@ class NormalizerFormatterTest extends TestCase
{
$formatter = new NormalizerFormatter('Y-m-d');
$formatted = $formatter->formatBatch([
$this->getRecord(Logger::CRITICAL, 'bar', channel: 'test'),
$this->getRecord(Logger::WARNING, 'foo', channel: 'log'),
$this->getRecord(Level::Critical, 'bar', channel: 'test'),
$this->getRecord(Level::Warning, 'foo', channel: 'log'),
]);
$this->assertEquals([
[
'level_name' => 'CRITICAL',
'level' => Logger::CRITICAL,
'level_name' => LevelName::Critical->value,
'level' => Level::Critical->value,
'channel' => 'test',
'message' => 'bar',
'context' => [],
@@ -156,8 +157,8 @@ class NormalizerFormatterTest extends TestCase
'extra' => [],
],
[
'level_name' => 'WARNING',
'level' => Logger::WARNING,
'level_name' => LevelName::Warning->value,
'level' => Level::Warning->value,
'channel' => 'log',
'message' => 'foo',
'context' => [],
@@ -241,7 +242,7 @@ class NormalizerFormatterTest extends TestCase
$largeArray = range(1, 1000);
$res = $formatter->format($this->getRecord(
Logger::CRITICAL,
Level::Critical,
'bar',
channel: 'test',
context: [$largeArray],
@@ -257,7 +258,7 @@ class NormalizerFormatterTest extends TestCase
$largeArray = range(1, 2000);
$res = $formatter->format($this->getRecord(
Logger::CRITICAL,
Level::Critical,
'bar',
channel: 'test',
context: [$largeArray],
@@ -379,7 +380,7 @@ class NormalizerFormatterTest extends TestCase
private function formatRecordWithExceptionInContext(NormalizerFormatter $formatter, \Throwable $exception)
{
$message = $formatter->format($this->getRecord(
Logger::CRITICAL,
Level::Critical,
'foobar',
channel: 'core',
context: ['exception' => $exception],

View File

@@ -11,7 +11,7 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\Level;
use Monolog\Test\TestCase;
class WildfireFormatterTest extends TestCase
@@ -23,7 +23,7 @@ class WildfireFormatterTest extends TestCase
{
$wildfire = new WildfireFormatter();
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['from' => 'logger'],
@@ -46,7 +46,7 @@ class WildfireFormatterTest extends TestCase
{
$wildfire = new WildfireFormatter();
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
context: ['from' => 'logger'],
@@ -69,7 +69,7 @@ class WildfireFormatterTest extends TestCase
{
$wildfire = new WildfireFormatter();
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
);
@@ -91,7 +91,7 @@ class WildfireFormatterTest extends TestCase
$wildfire = new WildfireFormatter();
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'log',
channel: 'meh',
);
@@ -106,7 +106,7 @@ class WildfireFormatterTest extends TestCase
{
$wildfire = new WildfireFormatter();
$record = $this->getRecord(
Logger::ERROR,
Level::Error,
'table-message',
channel: 'table-channel',
context: [