diff --git a/composer.json b/composer.json index 4238bd0..4e680df 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "require": { "php": "^8.2", "igorw/get-in": "^1.0.3", - "monolog/monolog": "^2.9.1" + "monolog/monolog": "^3.4.0" }, "require-dev": { "wyrihaximus/test-utilities": "^5.6.0" diff --git a/composer.lock b/composer.lock index cfe73ee..5dcc76d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "bf608aee4b5262660d2efaf31c806d27", + "content-hash": "0bf58978fa03a25aa55478485158860b", "packages": [ { "name": "igorw/get-in", @@ -57,42 +57,41 @@ }, { "name": "monolog/monolog", - "version": "2.9.1", + "version": "3.4.0", "source": { "type": "git", "url": "https://github.com/Seldaek/monolog.git", - "reference": "f259e2b15fb95494c83f52d3caad003bbf5ffaa1" + "reference": "e2392369686d420ca32df3803de28b5d6f76867d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Seldaek/monolog/zipball/f259e2b15fb95494c83f52d3caad003bbf5ffaa1", - "reference": "f259e2b15fb95494c83f52d3caad003bbf5ffaa1", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/e2392369686d420ca32df3803de28b5d6f76867d", + "reference": "e2392369686d420ca32df3803de28b5d6f76867d", "shasum": "" }, "require": { - "php": ">=7.2", - "psr/log": "^1.0.1 || ^2.0 || ^3.0" + "php": ">=8.1", + "psr/log": "^2.0 || ^3.0" }, "provide": { - "psr/log-implementation": "1.0.0 || 2.0.0 || 3.0.0" + "psr/log-implementation": "3.0.0" }, "require-dev": { - "aws/aws-sdk-php": "^2.4.9 || ^3.0", + "aws/aws-sdk-php": "^3.0", "doctrine/couchdb": "~1.0@dev", "elasticsearch/elasticsearch": "^7 || ^8", "ext-json": "*", - "graylog2/gelf-php": "^1.4.2 || ^2@dev", - "guzzlehttp/guzzle": "^7.4", + "graylog2/gelf-php": "^1.4.2 || ^2.0", + "guzzlehttp/guzzle": "^7.4.5", "guzzlehttp/psr7": "^2.2", "mongodb/mongodb": "^1.8", "php-amqplib/php-amqplib": "~2.4 || ^3", - "phpspec/prophecy": "^1.15", - "phpstan/phpstan": "^0.12.91", - "phpunit/phpunit": "^8.5.14", - "predis/predis": "^1.1 || ^2.0", - "rollbar/rollbar": "^1.3 || ^2 || ^3", + "phpstan/phpstan": "^1.9", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-strict-rules": "^1.4", + "phpunit/phpunit": "^10.1", + "predis/predis": "^1.1 || ^2", "ruflin/elastica": "^7", - "swiftmailer/swiftmailer": "^5.3|^6.0", "symfony/mailer": "^5.4 || ^6", "symfony/mime": "^5.4 || ^6" }, @@ -115,7 +114,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "2.x-dev" + "dev-main": "3.x-dev" } }, "autoload": { @@ -143,7 +142,7 @@ ], "support": { "issues": "https://github.com/Seldaek/monolog/issues", - "source": "https://github.com/Seldaek/monolog/tree/2.9.1" + "source": "https://github.com/Seldaek/monolog/tree/3.4.0" }, "funding": [ { @@ -155,7 +154,7 @@ "type": "tidelift" } ], - "time": "2023-02-06T13:44:46+00:00" + "time": "2023-06-21T08:46:11+00:00" }, { "name": "psr/log", diff --git a/src/CopyProcessor.php b/src/CopyProcessor.php index 598277f..3742ac7 100644 --- a/src/CopyProcessor.php +++ b/src/CopyProcessor.php @@ -4,19 +4,20 @@ namespace WyriHaximus\Monolog\Processors; -use Monolog\Logger; +use Monolog\LogRecord; +use Monolog\Processor\ProcessorInterface; +use function array_merge_recursive; use function explode; use function igorw\assoc_in; use function igorw\get_in; -/** @phpstan-import-type Record from Logger */ -final class CopyProcessor +final class CopyProcessor implements ProcessorInterface { - /** @var string[] */ + /** @var array */ private array $from; - /** @var string[] */ + /** @var array */ private array $to; public function __construct(string $from, string $to) @@ -25,16 +26,11 @@ public function __construct(string $from, string $to) $this->to = explode('.', $to); } - /** - * @phpstan-param Record $record - * - * @phpstan-return Record - */ - public function __invoke(array $record): array + public function __invoke(LogRecord $record): LogRecord { - $value = get_in($record, $this->from); + $value = get_in($record->toArray(), $this->from); if ($value !== null) { - $record = assoc_in($record, $this->to, $value); + $record = $record->with(...array_merge_recursive(assoc_in([], $this->to, $value), ['context' => $record->context, 'extra' => $record->extra])); } return $record; diff --git a/src/ExceptionClassProcessor.php b/src/ExceptionClassProcessor.php index fe252a5..0d3ecd9 100644 --- a/src/ExceptionClassProcessor.php +++ b/src/ExceptionClassProcessor.php @@ -4,24 +4,19 @@ namespace WyriHaximus\Monolog\Processors; -use Monolog\Logger; +use Monolog\LogRecord; +use Monolog\Processor\ProcessorInterface; use Throwable; use function array_key_exists; use function get_class; -/** @phpstan-import-type Record from Logger */ -final class ExceptionClassProcessor +final class ExceptionClassProcessor implements ProcessorInterface { - /** - * @phpstan-param Record $record - * - * @phpstan-return Record - */ - public function __invoke(array $record): array + public function __invoke(LogRecord $record): LogRecord { - if (array_key_exists('exception', $record['context']) && $record['context']['exception'] instanceof Throwable) { - $record['extra']['exception_class'] = get_class($record['context']['exception']); + if (array_key_exists('exception', $record->context) && $record->context['exception'] instanceof Throwable) { + $record->extra['exception_class'] = get_class($record->context['exception']); } return $record; diff --git a/src/KeyValueProcessor.php b/src/KeyValueProcessor.php index 5351884..46005fa 100644 --- a/src/KeyValueProcessor.php +++ b/src/KeyValueProcessor.php @@ -4,23 +4,18 @@ namespace WyriHaximus\Monolog\Processors; -use Monolog\Logger; +use Monolog\LogRecord; +use Monolog\Processor\ProcessorInterface; -/** @phpstan-import-type Record from Logger */ -final class KeyValueProcessor +final class KeyValueProcessor implements ProcessorInterface { public function __construct(private string $key, private mixed $value) { } - /** - * @phpstan-param Record $record - * - * @phpstan-return Record - */ - public function __invoke(array $record): array + public function __invoke(LogRecord $record): LogRecord { - $record['extra'][$this->key] = $this->value; + $record->extra[$this->key] = $this->value; return $record; } diff --git a/src/RuntimeProcessor.php b/src/RuntimeProcessor.php index 2cb0a8c..3dfbb2f 100644 --- a/src/RuntimeProcessor.php +++ b/src/RuntimeProcessor.php @@ -4,12 +4,12 @@ namespace WyriHaximus\Monolog\Processors; -use Monolog\Logger; +use Monolog\LogRecord; +use Monolog\Processor\ProcessorInterface; use function microtime; -/** @phpstan-import-type Record from Logger */ -final class RuntimeProcessor +final class RuntimeProcessor implements ProcessorInterface { private float $start; @@ -18,14 +18,9 @@ public function __construct() $this->start = microtime(true); } - /** - * @phpstan-param Record $record - * - * @phpstan-return Record - */ - public function __invoke(array $record): array + public function __invoke(LogRecord $record): LogRecord { - $record['extra']['runtime'] = microtime(true) - $this->start; + $record->extra['runtime'] = microtime(true) - $this->start; return $record; } diff --git a/src/ToContextProcessor.php b/src/ToContextProcessor.php index a40661c..f2dd7dd 100644 --- a/src/ToContextProcessor.php +++ b/src/ToContextProcessor.php @@ -4,15 +4,15 @@ namespace WyriHaximus\Monolog\Processors; -use Monolog\Logger; +use Monolog\LogRecord; +use Monolog\Processor\ProcessorInterface; use function array_key_exists; -/** @phpstan-import-type Record from Logger */ -final class ToContextProcessor +final class ToContextProcessor implements ProcessorInterface { /** - * @param string[] $keys + * @param array $keys * * @phpstan-ignore-next-line */ @@ -20,19 +20,14 @@ public function __construct(private array $keys = ['channel', 'extra', 'datetime { } - /** - * @phpstan-param Record $record - * - * @phpstan-return Record - */ - public function __invoke(array $record): array + public function __invoke(LogRecord $record): LogRecord { foreach ($this->keys as $key) { - if (! array_key_exists($key, $record)) { + if (! array_key_exists($key, $record->toArray())) { continue; } - $record['context'][$key] = $record[$key]; + $record = $record->with(context: [$key => $record->toArray()[$key]] + $record->toArray()['context']); } return $record; diff --git a/src/TraceProcessor.php b/src/TraceProcessor.php index 6783e69..97c9397 100644 --- a/src/TraceProcessor.php +++ b/src/TraceProcessor.php @@ -4,7 +4,8 @@ namespace WyriHaximus\Monolog\Processors; -use Monolog\Logger; +use Monolog\LogRecord; +use Monolog\Processor\ProcessorInterface; use Throwable; use function array_key_exists; @@ -12,34 +13,28 @@ use const DEBUG_BACKTRACE_IGNORE_ARGS; -/** @phpstan-import-type Record from Logger */ -final class TraceProcessor +final class TraceProcessor implements ProcessorInterface { /** @phpstan-ignore-next-line */ public function __construct(private bool $always = false) { } - /** - * @phpstan-param Record $record - * - * @phpstan-return Record - */ - public function __invoke(array $record): array + public function __invoke(LogRecord $record): LogRecord { - if (array_key_exists('exception', $record['context']) && $record['context']['exception'] instanceof Throwable) { - $record['extra']['trace'] = $record['context']['exception']->getTrace(); - foreach ($record['extra']['trace'] as $index => $line) { + if (array_key_exists('exception', $record->context) && $record->context['exception'] instanceof Throwable) { + $record->extra['trace'] = $record->context['exception']->getTrace(); + foreach ($record->extra['trace'] as $index => $line) { if (! array_key_exists('args', $line)) { continue; } - unset($record['extra']['trace'][$index]['args']); + unset($record->extra['trace'][$index]['args']); } } - if ($this->always && ! array_key_exists('trace', $record['extra'])) { - $record['extra']['trace'] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + if ($this->always && ! array_key_exists('trace', $record->extra)) { + $record->extra['trace'] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); } return $record; diff --git a/tests/CopyProcessorTest.php b/tests/CopyProcessorTest.php index 0d998a0..a4e309f 100644 --- a/tests/CopyProcessorTest.php +++ b/tests/CopyProcessorTest.php @@ -4,6 +4,7 @@ namespace WyriHaximus\Tests\Monolog\Processors; +use Monolog\LogRecord; use PHPUnit\Framework\TestCase; use WyriHaximus\Monolog\Processors\CopyProcessor; @@ -13,14 +14,14 @@ final class CopyProcessorTest extends TestCase public function testCopy(): void { $processor = new CopyProcessor('context.abc', 'context.def'); - $fromRecord = [ + $fromRecord = new LogRecord(...[ 'context' => ['abc' => 'value'], - ] + Records::basic(); + ] + Records::basic()); $toRecord = $processor($fromRecord); - self::assertArrayHasKey('def', $toRecord['context']); - self::assertEquals('value', $toRecord['context']['abc']); - self::assertEquals('value', $toRecord['context']['def']); + self::assertArrayHasKey('def', $toRecord->context); + self::assertEquals('value', $toRecord->context['abc']); + self::assertEquals('value', $toRecord->context['def']); } } diff --git a/tests/ExceptionClassProcessorTest.php b/tests/ExceptionClassProcessorTest.php index 06f2c57..437fe04 100644 --- a/tests/ExceptionClassProcessorTest.php +++ b/tests/ExceptionClassProcessorTest.php @@ -5,6 +5,7 @@ namespace WyriHaximus\Tests\Monolog\Processors; use InvalidArgumentException; +use Monolog\LogRecord; use PHPUnit\Framework\TestCase; use WyriHaximus\Monolog\Processors\ExceptionClassProcessor; @@ -13,7 +14,7 @@ final class ExceptionClassProcessorTest extends TestCase { public function testNoException(): void { - $input = Records::basic(); + $input = new LogRecord(...Records::basic()); $processor = new ExceptionClassProcessor(); $record = $processor($input); @@ -24,11 +25,11 @@ public function testException(): void { $processor = new ExceptionClassProcessor(); $exception = new InvalidArgumentException('fail!'); - $record = $processor([ + $record = $processor(new LogRecord(...[ 'context' => ['exception' => $exception], - ] + Records::basic()); + ] + Records::basic())); - self::assertArrayHasKey('exception_class', $record['extra']); - self::assertSame(InvalidArgumentException::class, $record['extra']['exception_class']); + self::assertArrayHasKey('exception_class', $record->extra); + self::assertSame(InvalidArgumentException::class, $record->extra['exception_class']); } } diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 35f149c..4af33af 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -5,7 +5,9 @@ namespace WyriHaximus\Tests\Monolog\Processors; use Exception; +use Monolog\Level; use Monolog\Logger; +use Monolog\LogRecord; use Monolog\Utils; use Psr\Log\LogLevel; use WyriHaximus\Monolog\Processors\CopyProcessor; @@ -19,10 +21,9 @@ use function array_key_exists; use function sprintf; -/** @phpstan-import-type Record from Logger */ final class FunctionalTest extends TestCase { - /** @phpstan-var array */ + /** @phpstan-var array */ private array $logs = []; public function testBasic(): void @@ -32,29 +33,31 @@ public function testBasic(): void $monolog->info('message'); self::assertCount(1, $this->logs); - self::assertArrayHasKey('datetime', $this->logs[0]['context']); - self::assertArrayHasKey('runtime', $this->logs[0]['extra']); - self::assertSame($this->logs[0]['datetime'], $this->logs[0]['context']['datetime']); - unset($this->logs[0]['datetime'], $this->logs[0]['context']['datetime']); + self::assertArrayHasKey('datetime', $this->logs[0]->context); + self::assertArrayHasKey('runtime', $this->logs[0]->extra); + self::assertSame($this->logs[0]->datetime, $this->logs[0]->context['datetime']); +// unset($this->logs[0]['datetime'], $this->logs[0]->context['datetime']); - self::assertIsFloat($this->logs[0]['extra']['runtime']); - self::assertIsFloat($this->logs[0]['context']['extra']['runtime']); - self::assertSame($this->logs[0]['extra']['runtime'], $this->logs[0]['context']['extra']['runtime']); - unset($this->logs[0]['extra']['runtime'], $this->logs[0]['context']['extra']['runtime']); + self::assertIsFloat($this->logs[0]->extra['runtime']); + self::assertIsFloat($this->logs[0]->context['extra']['runtime']); + self::assertSame($this->logs[0]->extra['runtime'], $this->logs[0]->context['extra']['runtime']); +// unset($this->logs[0]->extra['runtime'], $this->logs[0]->context->extra['runtime']); + self::assertSame('message', $this->logs[0]->message); self::assertSame([ - [ - 'message' => 'message', - 'context' => [ - 'channel' => 'logger', - 'extra' => ['version' => '1.2.3'], - ], - 'level' => 200, - 'level_name' => 'INFO', - 'channel' => 'logger', - 'extra' => ['version' => '1.2.3'], + 'datetime' => $this->logs[0]->datetime, + 'extra' => [ + 'runtime' => $this->logs[0]->extra['runtime'], + 'version' => '1.2.3', ], - ], $this->logs); + 'channel' => 'logger', + ], $this->logs[0]->context); + self::assertSame(Level::Info, $this->logs[0]->level); + self::assertSame('logger', $this->logs[0]->channel); + self::assertSame([ + 'runtime' => $this->logs[0]->extra['runtime'], + 'version' => '1.2.3', + ], $this->logs[0]->extra); } public function testException(): void @@ -79,40 +82,38 @@ public function testException(): void ); self::assertCount(1, $this->logs); - self::assertArrayHasKey('datetime', $this->logs[0]['context']); - self::assertArrayHasKey('runtime', $this->logs[0]['extra']); - self::assertSame($this->logs[0]['datetime'], $this->logs[0]['context']['datetime']); - unset($this->logs[0]['datetime'], $this->logs[0]['context']['datetime']); + self::assertArrayHasKey('datetime', $this->logs[0]->context); + self::assertArrayHasKey('runtime', $this->logs[0]->extra); + self::assertSame($this->logs[0]['datetime'], $this->logs[0]->context['datetime']); +// unset($this->logs[0]['datetime'], $this->logs[0]->context['datetime']); - self::assertIsFloat($this->logs[0]['extra']['runtime']); - self::assertIsFloat($this->logs[0]['context']['extra']['runtime']); - self::assertSame($this->logs[0]['extra']['runtime'], $this->logs[0]['context']['extra']['runtime']); - unset($this->logs[0]['extra']['runtime'], $this->logs[0]['context']['extra']['runtime']); + self::assertIsFloat($this->logs[0]->extra['runtime']); + self::assertIsFloat($this->logs[0]->context['extra']['runtime']); + self::assertSame($this->logs[0]->extra['runtime'], $this->logs[0]->context['extra']['runtime']); +// unset($this->logs[0]->extra['runtime'], $this->logs[0]->context->extra['runtime']); + self::assertSame($message, $this->logs[0]->message); self::assertSame([ - [ - 'message' => $message, - 'context' => [ - 'exception' => $e, - 'channel' => 'logger', - 'extra' => [ - 'trace' => $trace, - 'version' => '1.2.3', - 'exception_class' => 'Exception', - 'class_exception' => 'Exception', - ], - ], - 'level' => 400, - 'level_name' => 'ERROR', - 'channel' => 'logger', - 'extra' => [ - 'trace' => $trace, - 'version' => '1.2.3', - 'exception_class' => 'Exception', - 'class_exception' => 'Exception', - ], + 'datetime' => $this->logs[0]->datetime, + 'extra' => [ + 'class_exception' => 'Exception', + 'trace' => $trace, + 'runtime' => $this->logs[0]->extra['runtime'], + 'version' => '1.2.3', + 'exception_class' => 'Exception', ], - ], $this->logs); + 'channel' => 'logger', + 'exception' => $e, + ], $this->logs[0]->context); + self::assertSame(Level::Error, $this->logs[0]->level); + self::assertSame('logger', $this->logs[0]->channel); + self::assertSame([ + 'class_exception' => 'Exception', + 'trace' => $trace, + 'runtime' => $this->logs[0]->extra['runtime'], + 'version' => '1.2.3', + 'exception_class' => 'Exception', + ], $this->logs[0]->extra); } /** @return iterable */ diff --git a/tests/FunctionalTestLogger.php b/tests/FunctionalTestLogger.php index f00e7a3..066b325 100644 --- a/tests/FunctionalTestLogger.php +++ b/tests/FunctionalTestLogger.php @@ -5,9 +5,8 @@ namespace WyriHaximus\Tests\Monolog\Processors; use Monolog\Handler\AbstractHandler; -use Monolog\Logger; +use Monolog\LogRecord; -/** @phpstan-import-type Record from Logger */ final class FunctionalTestLogger extends AbstractHandler { /** @var callable */ @@ -20,8 +19,7 @@ public function __construct(callable $handler) parent::__construct(); } - /** @phpstan-param Record $record */ - public function handle(array $record): bool + public function handle(LogRecord $record): bool { ($this->handler)($record); diff --git a/tests/KeyValueProcessorTest.php b/tests/KeyValueProcessorTest.php index d2e61d8..dc85e1e 100644 --- a/tests/KeyValueProcessorTest.php +++ b/tests/KeyValueProcessorTest.php @@ -4,6 +4,7 @@ namespace WyriHaximus\Tests\Monolog\Processors; +use Monolog\LogRecord; use PHPUnit\Framework\TestCase; use WyriHaximus\Monolog\Processors\KeyValueProcessor; @@ -15,8 +16,8 @@ public function testKeyValue(): void $key = 'key'; $value = 'value'; $processor = new KeyValueProcessor($key, $value); - $record = $processor(Records::basic()); - self::assertArrayHasKey($key, $record['extra']); - self::assertSame($value, $record['extra'][$key]); + $record = $processor(new LogRecord(...Records::basic())); + self::assertArrayHasKey($key, $record->extra); + self::assertSame($value, $record->extra[$key]); } } diff --git a/tests/Records.php b/tests/Records.php index 3a93631..c342293 100644 --- a/tests/Records.php +++ b/tests/Records.php @@ -4,20 +4,18 @@ namespace WyriHaximus\Tests\Monolog\Processors; -use Monolog\Logger; +use Monolog\Level; use Safe\DateTimeImmutable; -/** @phpstan-import-type Record from Logger */ final class Records { - /** @phpstan-return Record */ + /** @return array{message: string, context: array, level: Level::Info, channel: string, extra: array, datetime: \DateTimeImmutable} */ public static function basic(): array { return [ 'message' => 'message', 'context' => [], - 'level' => 200, - 'level_name' => 'INFO', + 'level' => Level::Info, 'channel' => 'logger', 'extra' => [], 'datetime' => new DateTimeImmutable(), diff --git a/tests/RuntimeProcessorTest.php b/tests/RuntimeProcessorTest.php index e68e6b6..da2828c 100644 --- a/tests/RuntimeProcessorTest.php +++ b/tests/RuntimeProcessorTest.php @@ -4,6 +4,7 @@ namespace WyriHaximus\Tests\Monolog\Processors; +use Monolog\LogRecord; use PHPUnit\Framework\TestCase; use WyriHaximus\Monolog\Processors\RuntimeProcessor; @@ -15,14 +16,14 @@ final class RuntimeProcessorTest extends TestCase public function testKeyValue(): void { $processor = new RuntimeProcessor(); - $baselineRecord = $processor(Records::basic()); - self::assertArrayHasKey('runtime', $baselineRecord['extra']); - self::assertIsFloat($baselineRecord['extra']['runtime']); + $baselineRecord = $processor(new LogRecord(...Records::basic())); + self::assertArrayHasKey('runtime', $baselineRecord->extra); + self::assertIsFloat($baselineRecord->extra['runtime']); sleep(1); - $record = $processor(Records::basic()); - self::assertArrayHasKey('runtime', $record['extra']); - self::assertIsFloat($record['extra']['runtime']); - self::assertGreaterThan($baselineRecord['extra']['runtime'], $record['extra']['runtime']); - self::assertGreaterThan(1, $record['extra']['runtime']); + $record = $processor(new LogRecord(...Records::basic())); + self::assertArrayHasKey('runtime', $record->extra); + self::assertIsFloat($record->extra['runtime']); + self::assertGreaterThan($baselineRecord->extra['runtime'], $record->extra['runtime']); + self::assertGreaterThan(1, $record->extra['runtime']); } } diff --git a/tests/ToContextProcessorTest.php b/tests/ToContextProcessorTest.php index 9cc1ab5..904fc34 100644 --- a/tests/ToContextProcessorTest.php +++ b/tests/ToContextProcessorTest.php @@ -4,6 +4,7 @@ namespace WyriHaximus\Tests\Monolog\Processors; +use Monolog\LogRecord; use PHPUnit\Framework\TestCase; use Safe\DateTimeImmutable; use WyriHaximus\Monolog\Processors\ToContextProcessor; @@ -16,15 +17,16 @@ public function testKeyValue(): void $now = new DateTimeImmutable(); $extra = ['foo' => 'bar']; $processor = new ToContextProcessor(['datetime', 'extra']); - $record = $processor([ + $record = $processor(new LogRecord(...[ 'datetime' => $now, 'extra' => $extra, - 'excluded' => [], - ] + Records::basic()); + 'context' => ['extra' => ['foo' => 'baz']], + ] + Records::basic())); - self::assertArrayHasKey('datetime', $record['context']); - self::assertSame($now, $record['context']['datetime']); - self::assertArrayHasKey('extra', $record['context']); - self::assertSame($extra, $record['context']['extra']); + self::assertArrayHasKey('datetime', $record->context); + self::assertSame($now, $record->context['datetime']); + self::assertArrayHasKey('extra', $record->context); + self::assertSame($extra, $record->context['extra']); + self::assertSame('bar', $record->context['extra']['foo']); /** @phpstan-ignore-line */ } } diff --git a/tests/TraceProcessorTest.php b/tests/TraceProcessorTest.php index 0f0f2a8..8bc3931 100644 --- a/tests/TraceProcessorTest.php +++ b/tests/TraceProcessorTest.php @@ -5,6 +5,7 @@ namespace WyriHaximus\Tests\Monolog\Processors; use Exception; +use Monolog\LogRecord; use PHPUnit\Framework\TestCase; use WyriHaximus\Monolog\Processors\TraceProcessor; @@ -16,38 +17,38 @@ final class TraceProcessorTest extends TestCase public function testNoTrace(): void { $processor = new TraceProcessor(); - $record = $processor(Records::basic()); + $record = $processor(new LogRecord(...Records::basic())); - self::assertArrayNotHasKey('trace', $record['extra']); + self::assertArrayNotHasKey('trace', $record->extra); } public function testNonExceptionTrace(): void { $processor = new TraceProcessor(true); $line = __LINE__ + 1; - $record = $processor(Records::basic()); + $record = $processor(new LogRecord(...Records::basic())); - self::assertArrayHasKey('trace', $record['extra']); - self::assertArrayHasKey(0, $record['extra']['trace']); - self::assertSame(__FILE__, $record['extra']['trace'][0]['file']); - self::assertSame($line, $record['extra']['trace'][0]['line']); - self::assertThat(count($record['extra']['trace']), self::greaterThan(1)); + self::assertArrayHasKey('trace', $record->extra); + self::assertArrayHasKey(0, $record->extra['trace']); + self::assertSame(__FILE__, $record->extra['trace'][0]['file']); + self::assertSame($line, $record->extra['trace'][0]['line']); + self::assertThat(count($record->extra['trace']), self::greaterThan(1)); } public function testExceptionTrace(): void { $processor = new TraceProcessor(); $exception = new Exception('fail!'); - $record = $processor([ + $record = $processor(new LogRecord(...[ 'context' => ['exception' => $exception], - ] + Records::basic()); - - self::assertArrayHasKey('trace', $record['extra']); - self::assertArrayHasKey(0, $record['extra']['trace']); - self::assertArrayHasKey('file', $record['extra']['trace'][0]); - self::assertArrayHasKey('line', $record['extra']['trace'][0]); - self::assertSame(__FUNCTION__, $record['extra']['trace'][0]['function']); - self::assertSame(self::class, $record['extra']['trace'][0]['class']); - self::assertThat(count($record['extra']['trace']), self::greaterThan(1)); + ] + Records::basic())); + + self::assertArrayHasKey('trace', $record->extra); + self::assertArrayHasKey(0, $record->extra['trace']); + self::assertArrayHasKey('file', $record->extra['trace'][0]); + self::assertArrayHasKey('line', $record->extra['trace'][0]); + self::assertSame(__FUNCTION__, $record->extra['trace'][0]['function']); + self::assertSame(self::class, $record->extra['trace'][0]['class']); + self::assertThat(count($record->extra['trace']), self::greaterThan(1)); } }