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

Merge branch '2.x'

This commit is contained in:
Jordi Boggiano
2024-04-15 11:22:23 +02:00
3 changed files with 22 additions and 2 deletions

View File

@@ -188,6 +188,10 @@ class JsonFormatter extends NormalizerFormatter
return $data->__toString(); return $data->__toString();
} }
if (\get_class($data) === '__PHP_Incomplete_Class') {
return new \ArrayObject($data);
}
return $data; return $data;
} }

View File

@@ -122,7 +122,9 @@ class StreamHandler extends AbstractProcessingHandler
} }
$this->createDir($url); $this->createDir($url);
$this->errorMessage = null; $this->errorMessage = null;
set_error_handler([$this, 'customErrorHandler']); set_error_handler(function (...$args) {
return $this->customErrorHandler(...$args);
});
try { try {
$stream = fopen($url, 'a'); $stream = fopen($url, 'a');
if ($this->filePermission !== null) { if ($this->filePermission !== null) {
@@ -193,7 +195,9 @@ class StreamHandler extends AbstractProcessingHandler
$dir = $this->getDirFromStream($url); $dir = $this->getDirFromStream($url);
if (null !== $dir && !is_dir($dir)) { if (null !== $dir && !is_dir($dir)) {
$this->errorMessage = null; $this->errorMessage = null;
set_error_handler([$this, 'customErrorHandler']); set_error_handler(function (...$args) {
return $this->customErrorHandler(...$args);
});
$status = mkdir($dir, 0777, true); $status = mkdir($dir, 0777, true);
restore_error_handler(); restore_error_handler();
if (false === $status && !is_dir($dir) && strpos((string) $this->errorMessage, 'File exists') === false) { if (false === $status && !is_dir($dir) && strpos((string) $this->errorMessage, 'File exists') === false) {

View File

@@ -274,6 +274,18 @@ class JsonFormatterTest extends TestCase
$this->assertEquals('Over 1000 items (2000 total), aborting normalization', $res['context'][0]['...']); $this->assertEquals('Over 1000 items (2000 total), aborting normalization', $res['context'][0]['...']);
} }
public function testCanNormalizeIncompleteObject(): void
{
$serialized = "O:17:\"Monolog\TestClass\":1:{s:23:\"\x00Monolog\TestClass\x00name\";s:4:\"test\";}";
$object = unserialize($serialized);
$formatter = new JsonFormatter();
$record = $this->getRecord(context: ['object' => $object], datetime: new \DateTimeImmutable('2022-02-22 00:00:00'));
$result = $formatter->format($record);
self::assertSame('{"message":"test","context":{"object":{"__PHP_Incomplete_Class_Name":"Monolog\\\\TestClass"}},"level":300,"level_name":"WARNING","channel":"test","datetime":"2022-02-22T00:00:00+00:00","extra":{}}'."\n", $result);
}
public function testEmptyContextAndExtraFieldsCanBeIgnored() public function testEmptyContextAndExtraFieldsCanBeIgnored()
{ {
$formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, true, true); $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, true, true);