From 170a1f409b50c479ca9e36ece07bd7130d0ea634 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 10 Jan 2023 16:09:39 +0000 Subject: [PATCH] [2.x] Adds Laravel 10 support (#146) * Adds Laravel 10 support * Adds support for monolog `^3.2` * Improves tests around monolog * Don't use PHPUnit 10 * Update composer.json Co-authored-by: Joe Dixon * Tests Octane on Laravel 10 Co-authored-by: Joe Dixon --- .github/workflows/tests.yml | 20 ++- composer.json | 12 +- phpunit.xml.dist | 13 +- src/Logging/JsonFormatter.php | 23 +++- tests/Unit/Logging/JsonFormatterTest.php | 147 +++++++++++++++++++++++ 5 files changed, 189 insertions(+), 26 deletions(-) create mode 100644 tests/Unit/Logging/JsonFormatterTest.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index fa94023..e1577f6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -8,26 +8,38 @@ on: jobs: tests: + runs-on: ubuntu-22.04 - runs-on: ubuntu-latest strategy: fail-fast: true matrix: - php: [7.2, 7.3, 7.4, 8.0, 8.1] - laravel: [^6.0, ^7.0, ^8.0, ^9.0] + php: [7.2, 7.3, 7.4, 8.0, 8.1, 8.2] + laravel: [^6.0, ^7.0, ^8.0, ^9.0, ^10.0] exclude: - php: 7.2 laravel: ^8.0 - php: 7.2 laravel: ^9.0 + - php: 7.2 + laravel: ^10.0 - php: 7.3 laravel: ^9.0 + - php: 7.3 + laravel: ^10.0 - php: 7.4 laravel: ^9.0 + - php: 7.4 + laravel: ^10.0 + - php: 8.0 + laravel: ^10.0 - php: 8.1 laravel: ^6.0 - php: 8.1 laravel: ^7.0 + - php: 8.2 + laravel: ^6.0 + - php: 8.2 + laravel: ^7.0 name: P${{ matrix.php }} - L${{ matrix.laravel }} @@ -56,7 +68,7 @@ jobs: run: | composer require laravel/octane --dev if: | - matrix.php >= 8.0 && (matrix.laravel == '^8.0' || matrix.laravel == '^9.0') + matrix.php >= 8.0 && (matrix.laravel == '^8.0' || matrix.laravel == '^9.0' || matrix.laravel == '^10.0') - name: Execute unit tests run: vendor/bin/phpunit diff --git a/composer.json b/composer.json index 17e8149..e37a7cc 100644 --- a/composer.json +++ b/composer.json @@ -18,11 +18,11 @@ "aws/aws-sdk-php": "^3.80", "guzzlehttp/guzzle": "^6.3|^7.0", "hollodotme/fast-cgi-client": "^3.0", - "illuminate/container": "^6.0|^7.0|^8.0|^9.0", - "illuminate/http": "^6.0|^7.0|^8.0|^9.0", - "illuminate/queue": "^6.0|^7.0|^8.0|^9.0", - "illuminate/support": "^6.0|^7.0|^8.0|^9.0", - "monolog/monolog": "^1.12|^2.0", + "illuminate/container": "^6.0|^7.0|^8.0|^9.0|^10.0", + "illuminate/http": "^6.0|^7.0|^8.0|^9.0|^10.0", + "illuminate/queue": "^6.0|^7.0|^8.0|^9.0|^10.0", + "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0", + "monolog/monolog": "^1.12|^2.0|^3.2", "nyholm/psr7": "^1.0", "riverline/multipart-parser": "^2.0.9", "symfony/process": "^4.3|^5.0|^6.0", @@ -30,7 +30,7 @@ }, "require-dev": { "mockery/mockery": "^1.2", - "orchestra/testbench": "^4.0|^5.0|^6.0|^7.0", + "orchestra/testbench": "^4.0|^5.0|^6.0|^7.0|^8.0", "phpunit/phpunit": "^8.0|^9.0" }, "autoload": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 9009d22..d45efe2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -7,6 +7,7 @@ convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" + convertDeprecationsToExceptions="true" processIsolation="false" stopOnFailure="false" > @@ -18,18 +19,6 @@ ./tests/Feature - - - src/ - - - - - - - - - diff --git a/src/Logging/JsonFormatter.php b/src/Logging/JsonFormatter.php index 5ccab68..e63ee50 100644 --- a/src/Logging/JsonFormatter.php +++ b/src/Logging/JsonFormatter.php @@ -3,17 +3,32 @@ namespace Laravel\Vapor\Logging; use Monolog\Formatter\JsonFormatter as BaseJsonFormatter; +use Monolog\LogRecord; class JsonFormatter extends BaseJsonFormatter { /** * {@inheritdoc} */ - public function format(array $record): string + public function format($record): string { - $record['context'] = array_merge( - $record['context'] ?? [], ['aws_request_id' => ($_ENV['AWS_REQUEST_ID'] ?? null)] - ); + $context = ['aws_request_id' => ($_ENV['AWS_REQUEST_ID'] ?? null)]; + + if ($record instanceof LogRecord) { + $record = new LogRecord( + $record->datetime, + $record->channel, + $record->level, + $record->message, + array_merge($record->context, $context), + $record->extra, + $record->formatted + ); + } else { + $record['context'] = array_merge( + $record['context'] ?? [], $context + ); + } return parent::format($record); } diff --git a/tests/Unit/Logging/JsonFormatterTest.php b/tests/Unit/Logging/JsonFormatterTest.php new file mode 100644 index 0000000..0bcaae9 --- /dev/null +++ b/tests/Unit/Logging/JsonFormatterTest.php @@ -0,0 +1,147 @@ + 'context-value'], + ['extra-test' => 'extra-value'] + ); + } else { + $record = [ + 'datetime' => new \DateTimeImmutable(), + 'channel' => 'channel-test', + 'level' => 200, + 'level_name' => 'INFO', + 'message' => 'message-test', + 'context' => ['context-test' => 'context-value'], + 'extra' => ['extra-test' => 'extra-value'], + ]; + } + + $_ENV['AWS_REQUEST_ID'] = '123456789'; + + $record = $formatter->format($record); + $this->assertJson($record); + + $record = json_decode($record, true); + + if (class_exists(LogRecord::class)) { + $this->assertSame([ + 'message' => 'message-test', + 'context' => [ + 'context-test' => 'context-value', + 'aws_request_id' => '123456789', + ], + 'level' => 200, + 'level_name' => 'INFO', + 'channel' => 'channel-test', + 'datetime' => $record['datetime'], + 'extra' => [ + 'extra-test' => 'extra-value', + ], + ], $record); + } else { + $this->assertSame([ + 'datetime' => $record['datetime'], + 'channel' => 'channel-test', + 'level' => 200, + 'level_name' => 'INFO', + 'message' => 'message-test', + 'context' => [ + 'context-test' => 'context-value', + 'aws_request_id' => '123456789', + ], + 'extra' => [ + 'extra-test' => 'extra-value', + ], + ], $record); + } + } + + public function test_includes_aws_request_id() + { + $formatter = new JsonFormatter(); + + $_ENV['AWS_REQUEST_ID'] = '123456789'; + + if (class_exists(LogRecord::class)) { + $record = new LogRecord( + new \DateTimeImmutable(), + 'channel-test', + Level::Info, + 'message-test', + ['foo' => 'bar'] + ); + } else { + $record = [ + 'datetime' => new \DateTimeImmutable(), + 'channel' => 'channel-test', + 'level' => 200, + 'level_name' => 'INFO', + 'message' => 'message-test', + 'context' => ['foo' => 'bar'], + 'extra' => [], + ]; + } + + $record = $formatter->format($record); + $this->assertJson($record); + + $record = json_decode($record, true); + $this->assertSame(['foo' => 'bar', 'aws_request_id' => '123456789'], $record['context']); + } + + public function test_aws_request_id_may_be_null() + { + $formatter = new JsonFormatter(); + + if (class_exists(LogRecord::class)) { + $record = new LogRecord( + new \DateTimeImmutable(), + 'channel-test', + Level::Info, + 'message-test' + ); + } else { + $record = [ + 'datetime' => new \DateTimeImmutable(), + 'channel' => 'channel-test', + 'level' => 200, + 'level_name' => 'INFO', + 'message' => 'message-test', + 'context' => [], + 'extra' => [], + ]; + } + + $record = $formatter->format($record); + $this->assertJson($record); + + $record = json_decode($record, true); + $this->assertSame(['aws_request_id' => null], $record['context']); + } +}