1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-11 07:34:12 +02:00

Add LogRecord class, drop PHP <8.1

This commit is contained in:
Jordi Boggiano
2022-02-20 14:39:51 +01:00
parent f2f66cd480
commit 22c8b19358
111 changed files with 501 additions and 361 deletions

View File

@@ -12,6 +12,7 @@
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\LogRecord;
class ElasticaFormatterTest extends \PHPUnit\Framework\TestCase
{
@@ -30,18 +31,18 @@ class ElasticaFormatterTest extends \PHPUnit\Framework\TestCase
public function testFormat()
{
// test log message
$msg = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['foo' => 7, 'bar', 'class' => new \stdClass],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];
$msg = new LogRecord(
level: Logger::ERROR,
levelName: 'ERROR',
channel: 'meh',
context: ['foo' => 7, 'bar', 'class' => new \stdClass],
datetime: new \DateTimeImmutable("@0"),
message: 'log',
);
// expected values
$expected = $msg;
$expected = (array) $msg;
unset($expected['formatted']);
$expected['datetime'] = '1970-01-01T00:00:00.000000+00:00';
$expected['context'] = [
'class' => ['stdClass' => []],

View File

@@ -152,7 +152,7 @@ class LineFormatterTest extends \PHPUnit\Framework\TestCase
$path = str_replace('\\/', '/', json_encode(__FILE__));
$this->assertRegexp('{^\['.date('Y-m-d').'] core\.CRITICAL: foobar \{"exception":"\[object] \(RuntimeException\(code: 0\): Foo at '.preg_quote(substr($path, 1, -1)).':'.(__LINE__ - 8).'\)\n\[stacktrace]\n#0}', $message);
$this->assertMatchesRegularExpression('{^\['.date('Y-m-d').'] core\.CRITICAL: foobar \{"exception":"\[object] \(RuntimeException\(code: 0\): Foo at '.preg_quote(substr($path, 1, -1)).':'.(__LINE__ - 8).'\)\n\[stacktrace]\n#0}', $message);
}
public function testDefFormatWithPreviousException()
@@ -242,7 +242,7 @@ class LineFormatterTest extends \PHPUnit\Framework\TestCase
]
);
$this->assertRegExp('/foo bar/', $message);
$this->assertMatchesRegularExpression('/foo bar/', $message);
}
public function testFormatShouldNotStripInlineLineBreaksWhenFlagIsSet()
@@ -256,7 +256,7 @@ class LineFormatterTest extends \PHPUnit\Framework\TestCase
]
);
$this->assertRegExp('/foo\nbar/', $message);
$this->assertMatchesRegularExpression('/foo\nbar/', $message);
}
}

View File

@@ -15,6 +15,7 @@ use Monolog\Formatter\ElasticaFormatter;
use Monolog\Formatter\NormalizerFormatter;
use Monolog\Test\TestCase;
use Monolog\Logger;
use Monolog\LogRecord;
use Elastica\Client;
use Elastica\Request;
use Elastica\Response;
@@ -57,15 +58,15 @@ class ElasticaHandlerTest extends TestCase
public function testHandle()
{
// log message
$msg = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['foo' => 7, 'bar', 'class' => new \stdClass],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];
$msg = new LogRecord(
level: Logger::ERROR,
levelName: 'ERROR',
channel: 'meh',
context: ['foo' => 7, 'bar', 'class' => new \stdClass],
datetime: new \DateTimeImmutable("@0"),
extra: [],
message: 'log',
);
// format expected result
$formatter = new ElasticaFormatter($this->options['index'], $this->options['type']);
@@ -165,17 +166,17 @@ class ElasticaHandlerTest extends TestCase
*/
public function testHandleIntegration()
{
$msg = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['foo' => 7, 'bar', 'class' => new \stdClass],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];
$msg = new LogRecord(
level: Logger::ERROR,
levelName: 'ERROR',
channel: 'meh',
context: ['foo' => 7, 'bar', 'class' => new \stdClass],
datetime: new \DateTimeImmutable("@0"),
extra: [],
message: 'log',
);
$expected = $msg;
$expected = (array) $msg;
$expected['datetime'] = $msg['datetime']->format(\DateTime::ISO8601);
$expected['context'] = [
'class' => '[object] (stdClass: {})',
@@ -219,17 +220,17 @@ class ElasticaHandlerTest extends TestCase
*/
public function testHandleIntegrationNewESVersion()
{
$msg = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['foo' => 7, 'bar', 'class' => new \stdClass],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => [],
'message' => 'log',
];
$msg = new LogRecord(
level: Logger::ERROR,
levelName: 'ERROR',
channel: 'meh',
context: ['foo' => 7, 'bar', 'class' => new \stdClass],
datetime: new \DateTimeImmutable("@0"),
extra: [],
message: 'log',
);
$expected = $msg;
$expected = (array) $msg;
$expected['datetime'] = $msg['datetime']->format(\DateTime::ISO8601);
$expected['context'] = [
'class' => '[object] (stdClass: {})',

View File

@@ -12,13 +12,14 @@
namespace Monolog\Handler;
use Exception;
use Monolog\LogRecord;
class ExceptionTestHandler extends TestHandler
{
/**
* {@inheritDoc}
*/
public function handle(array $record): bool
public function handle(LogRecord $record): bool
{
throw new Exception("ExceptionTestHandler::handle");

View File

@@ -45,7 +45,7 @@ class FlowdockHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/POST \/v1\/messages\/team_inbox\/.* HTTP\/1.1\\r\\nHost: api.flowdock.com\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
$this->assertMatchesRegularExpression('/POST \/v1\/messages\/team_inbox\/.* HTTP\/1.1\\r\\nHost: api.flowdock.com\\r\\nContent-Type: application\/json\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
return $content;
}
@@ -55,8 +55,8 @@ class FlowdockHandlerTest extends TestCase
*/
public function testWriteContent($content)
{
$this->assertRegexp('/"source":"test_source"/', $content);
$this->assertRegexp('/"from_address":"source@test\.com"/', $content);
$this->assertMatchesRegularExpression('/"source":"test_source"/', $content);
$this->assertMatchesRegularExpression('/"from_address":"source@test\.com"/', $content);
}
private function createHandler($token = 'myToken')

View File

@@ -38,7 +38,7 @@ class InsightOpsHandlerTest extends TestCase
fseek($this->resource, 0);
$content = fread($this->resource, 1024);
$this->assertRegexp('/testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] test.CRITICAL: Critical write test/', $content);
$this->assertMatchesRegularExpression('/testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] test.CRITICAL: Critical write test/', $content);
}
public function testWriteBatchContent()
@@ -49,7 +49,7 @@ class InsightOpsHandlerTest extends TestCase
fseek($this->resource, 0);
$content = fread($this->resource, 1024);
$this->assertRegexp('/(testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] .* \[\] \[\]\n){3}/', $content);
$this->assertMatchesRegularExpression('/(testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] .* \[\] \[\]\n){3}/', $content);
}
private function createHandler()

View File

@@ -37,7 +37,7 @@ class LogEntriesHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] test.CRITICAL: Critical write test/', $content);
$this->assertMatchesRegularExpression('/testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] test.CRITICAL: Critical write test/', $content);
}
public function testWriteBatchContent()
@@ -53,7 +53,7 @@ class LogEntriesHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/(testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] .* \[\] \[\]\n){3}/', $content);
$this->assertMatchesRegularExpression('/(testToken \[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}\+00:00\] .* \[\] \[\]\n){3}/', $content);
}
private function createHandler()

View File

@@ -37,7 +37,7 @@ class LogmaticHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/testToken {"message":"Critical write test","context":{},"level":500,"level_name":"CRITICAL","channel":"test","datetime":"(.*)","extra":{},"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
$this->assertMatchesRegularExpression('/testToken {"message":"Critical write test","context":{},"level":500,"level_name":"CRITICAL","channel":"test","datetime":"(.*)","extra":{},"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
}
public function testWriteBatchContent()
@@ -53,7 +53,7 @@ class LogmaticHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/testToken {"message":"test","context":{},"level":300,"level_name":"WARNING","channel":"test","datetime":"(.*)","extra":{},"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
$this->assertMatchesRegularExpression('/testToken {"message":"test","context":{},"level":300,"level_name":"WARNING","channel":"test","datetime":"(.*)","extra":{},"hostname":"testHostname","appname":"testAppname","@marker":\["sourcecode","php"\]}/', $content);
}
private function createHandler()

View File

@@ -33,7 +33,7 @@ class PushoverHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/POST \/1\/messages.json HTTP\/1.1\\r\\nHost: api.pushover.net\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
$this->assertMatchesRegularExpression('/POST \/1\/messages.json HTTP\/1.1\\r\\nHost: api.pushover.net\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
return $content;
}
@@ -43,7 +43,7 @@ class PushoverHandlerTest extends TestCase
*/
public function testWriteContent($content)
{
$this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}$/', $content);
$this->assertMatchesRegularExpression('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}$/', $content);
}
public function testWriteWithComplexTitle()
@@ -53,7 +53,7 @@ class PushoverHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/title=Backup\+finished\+-\+SQL1/', $content);
$this->assertMatchesRegularExpression('/title=Backup\+finished\+-\+SQL1/', $content);
}
public function testWriteWithComplexMessage()
@@ -64,7 +64,7 @@ class PushoverHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
$this->assertMatchesRegularExpression('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
}
public function testWriteWithTooLongMessage()
@@ -78,7 +78,7 @@ class PushoverHandlerTest extends TestCase
$expectedMessage = substr($message, 0, 505);
$this->assertRegexp('/message=' . $expectedMessage . '&title/', $content);
$this->assertMatchesRegularExpression('/message=' . $expectedMessage . '&title/', $content);
}
public function testWriteWithHighPriority()
@@ -88,7 +88,7 @@ class PushoverHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}&priority=1$/', $content);
$this->assertMatchesRegularExpression('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}&priority=1$/', $content);
}
public function testWriteWithEmergencyPriority()
@@ -98,7 +98,7 @@ class PushoverHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200$/', $content);
$this->assertMatchesRegularExpression('/token=myToken&user=myUser&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200$/', $content);
}
public function testWriteToMultipleUsers()
@@ -108,8 +108,8 @@ class PushoverHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/token=myToken&user=userA&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200POST/', $content);
$this->assertRegexp('/token=myToken&user=userB&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200$/', $content);
$this->assertMatchesRegularExpression('/token=myToken&user=userA&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200POST/', $content);
$this->assertMatchesRegularExpression('/token=myToken&user=userB&message=test1&title=Monolog&timestamp=\d{10}&priority=2&retry=30&expire=25200$/', $content);
}
private function createHandler($token = 'myToken', $user = 'myUser', $title = 'Monolog')

View File

@@ -46,7 +46,7 @@ class SlackHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('{POST /api/chat.postMessage HTTP/1.1\\r\\nHost: slack.com\\r\\nContent-Type: application/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n}', $content);
$this->assertMatchesRegularExpression('{POST /api/chat.postMessage HTTP/1.1\\r\\nHost: slack.com\\r\\nContent-Type: application/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n}', $content);
}
public function testWriteContent()
@@ -56,10 +56,10 @@ class SlackHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegExp('/username=Monolog/', $content);
$this->assertRegExp('/channel=channel1/', $content);
$this->assertRegExp('/token=myToken/', $content);
$this->assertRegExp('/attachments/', $content);
$this->assertMatchesRegularExpression('/username=Monolog/', $content);
$this->assertMatchesRegularExpression('/channel=channel1/', $content);
$this->assertMatchesRegularExpression('/token=myToken/', $content);
$this->assertMatchesRegularExpression('/attachments/', $content);
}
public function testWriteContentUsesFormatterIfProvided()
@@ -75,8 +75,8 @@ class SlackHandlerTest extends TestCase
fseek($this->res, 0);
$content2 = fread($this->res, 1024);
$this->assertRegexp('/text=test1/', $content);
$this->assertRegexp('/text=foo--test2/', $content2);
$this->assertMatchesRegularExpression('/text=test1/', $content);
$this->assertMatchesRegularExpression('/text=foo--test2/', $content2);
}
public function testWriteContentWithEmoji()
@@ -86,7 +86,7 @@ class SlackHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/icon_emoji=%3Aalien%3A/', $content);
$this->assertMatchesRegularExpression('/icon_emoji=%3Aalien%3A/', $content);
}
/**
@@ -99,7 +99,7 @@ class SlackHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/%22color%22%3A%22'.$expectedColor.'/', $content);
$this->assertMatchesRegularExpression('/%22color%22%3A%22'.$expectedColor.'/', $content);
}
public function testWriteContentWithPlainTextMessage()
@@ -109,7 +109,7 @@ class SlackHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/text=test1/', $content);
$this->assertMatchesRegularExpression('/text=test1/', $content);
}
public function provideLevelColors()

View File

@@ -24,7 +24,7 @@ class MemoryPeakUsageProcessorTest extends TestCase
$processor = new MemoryPeakUsageProcessor();
$record = $processor($this->getRecord());
$this->assertArrayHasKey('memory_peak_usage', $record['extra']);
$this->assertRegExp('#[0-9.]+ (M|K)?B$#', $record['extra']['memory_peak_usage']);
$this->assertMatchesRegularExpression('#[0-9.]+ (M|K)?B$#', $record['extra']['memory_peak_usage']);
}
/**

View File

@@ -24,7 +24,7 @@ class MemoryUsageProcessorTest extends TestCase
$processor = new MemoryUsageProcessor();
$record = $processor($this->getRecord());
$this->assertArrayHasKey('memory_usage', $record['extra']);
$this->assertRegExp('#[0-9.]+ (M|K)?B$#', $record['extra']['memory_usage']);
$this->assertMatchesRegularExpression('#[0-9.]+ (M|K)?B$#', $record['extra']['memory_usage']);
}
/**

View File

@@ -11,7 +11,9 @@
namespace Monolog\Processor;
class PsrLogMessageProcessorTest extends \PHPUnit\Framework\TestCase
use Monolog\Test\TestCase;
class PsrLogMessageProcessorTest extends TestCase
{
/**
* @dataProvider getPairs
@@ -20,10 +22,7 @@ class PsrLogMessageProcessorTest extends \PHPUnit\Framework\TestCase
{
$proc = new PsrLogMessageProcessor;
$message = $proc([
'message' => '{foo}',
'context' => ['foo' => $val],
]);
$message = $proc($this->getRecord(message: '{foo}', context: ['foo' => $val]));
$this->assertEquals($expected, $message['message']);
$this->assertSame(['foo' => $val], $message['context']);
}
@@ -32,10 +31,7 @@ class PsrLogMessageProcessorTest extends \PHPUnit\Framework\TestCase
{
$proc = new PsrLogMessageProcessor($dateFormat = null, $removeUsedContextFields = true);
$message = $proc([
'message' => '{foo}',
'context' => ['foo' => 'bar', 'lorem' => 'ipsum'],
]);
$message = $proc($this->getRecord(message: '{foo}', context: ['foo' => 'bar', 'lorem' => 'ipsum']));
$this->assertSame('bar', $message['message']);
$this->assertSame(['lorem' => 'ipsum'], $message['context']);
}
@@ -47,10 +43,7 @@ class PsrLogMessageProcessorTest extends \PHPUnit\Framework\TestCase
$proc = new PsrLogMessageProcessor($format);
$message = $proc([
'message' => '{foo}',
'context' => ['foo' => $date],
]);
$message = $proc($this->getRecord(message: '{foo}', context: ['foo' => $date]));
$this->assertEquals($date->format($format), $message['message']);
$this->assertSame(['foo' => $date], $message['context']);
}