Skip to content

Commit

Permalink
Adds Context support (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro authored Mar 29, 2024
1 parent 65cfe0a commit 02e1e52
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
composer.lock
/phpunit.xml
.phpunit.result.cache
.phpunit.cache
10 changes: 9 additions & 1 deletion src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
) {
Expand Down Expand Up @@ -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();
}
}
1 change: 1 addition & 0 deletions src/PailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
));
Expand Down
12 changes: 12 additions & 0 deletions tests/Features/CommandTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Illuminate\Support\Facades\Context;

test('debug messages', function () {
expect('app("log")->debug("my debug message")')->toPail(<<<'EOF'
┌ 03:04:05 DEBUG ────────────────────────────────┐
Expand Down Expand Up @@ -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');

0 comments on commit 02e1e52

Please sign in to comment.