Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.]x Adds Context support #34

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');