From 653b8c464fc4bf22ab25fc16f4189b67c239c9ac Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 29 Mar 2024 14:43:45 +0000 Subject: [PATCH] Adds `Context` support --- .gitignore | 1 + src/Handler.php | 10 +++++++++- src/PailServiceProvider.php | 1 + tests/Features/CommandTest.php | 12 ++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 661060c..bffde09 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ composer.lock /phpunit.xml .phpunit.result.cache +.phpunit.cache diff --git a/src/Handler.php b/src/Handler.php index cf412ba..deb69ba 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -3,10 +3,13 @@ namespace Laravel\Pail; use Illuminate\Console\Events\CommandStarting; +use Illuminate\Contracts\Container\Container; use Illuminate\Foundation\Auth\User; use Illuminate\Log\Events\MessageLogged; +use Illuminate\Log\Context\Repository as ContextRepository; use Illuminate\Queue\Events\JobExceptionOccurred; use Illuminate\Queue\Events\JobProcessing; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; use Throwable; @@ -26,6 +29,7 @@ class Handler * Creates a new instance of the handler. */ public function __construct( + protected Container $container, protected Files $files, protected bool $runningInConsole, ) { @@ -108,6 +112,10 @@ protected function context(MessageLogged $messageLogged): array ])->values() : null; - return collect($messageLogged->context)->merge($context)->toArray(); + return collect($messageLogged->context) + ->merge($context) + ->when($this->container->bound(ContextRepository::class), function (Collection $context) { + return $context->merge($this->container->make(ContextRepository::class)->all()); // @phpstan-ignore-line + })->toArray(); } } diff --git a/src/PailServiceProvider.php b/src/PailServiceProvider.php index 262465f..ff40b1c 100644 --- a/src/PailServiceProvider.php +++ b/src/PailServiceProvider.php @@ -24,6 +24,7 @@ public function register(): void ); $this->app->singleton(Handler::class, fn (Application $app) => new Handler( + $app, $app->make(Files::class), // @phpstan-ignore-line $app->runningInConsole(), )); diff --git a/tests/Features/CommandTest.php b/tests/Features/CommandTest.php index b61327c..55d6642 100644 --- a/tests/Features/CommandTest.php +++ b/tests/Features/CommandTest.php @@ -1,5 +1,7 @@ debug("my debug message")')->toPail(<<<'EOF' ┌ 03:04:05 DEBUG ────────────────────────────────┐ @@ -193,3 +195,13 @@ EOF, verbose: true); }); + +test('using context facade', function () { + expect('Context::add("user_id", 1); Context::push("breadcrumbs", "first_value"); Log::error("log message", ["exception" => "an exception occured"])')->toPail(<<<'EOF' + ┌ 03:04:05 ERROR ────────────────────────────────┐ + │ log message │ + └ artisan • user_id: 1 • breadcrumbs: array ( 0 => 'first_value', ) ┘ + + EOF, + ); +})->skip(! class_exists(Context::class), 'Context facade is not available in this version of Laravel');