Skip to content

Commit

Permalink
Refreshes log manager container
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Jun 20, 2024
1 parent 5bbe2c5 commit b407525
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Concerns/ProvidesDefaultConfigurationOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static function prepareApplicationForNextOperation(): array
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToDatabaseSessionHandler::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToFilesystemManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToHttpKernel::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToLogManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToMailManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToNotificationChannelManager::class,
\Laravel\Octane\Listeners\GiveNewApplicationInstanceToPipelineHub::class,
Expand Down
22 changes: 22 additions & 0 deletions src/Listeners/GiveNewApplicationInstanceToLogManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Laravel\Octane\Listeners;

class GiveNewApplicationInstanceToLogManager
{
/**
* Handle the event.
*
* @param mixed $event
*/
public function handle($event): void
{
if (! $event->sandbox->resolved('log')) {
return;
}

if (method_exists($log = $event->sandbox->make('log'), 'setApplication')) {
$log->setApplication($event->sandbox);
}
}
}
1 change: 1 addition & 0 deletions src/OctaneServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ protected function bindListeners()
$this->app->singleton(Listeners\GiveNewApplicationInstanceToAuthorizationGate::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToBroadcastManager::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToHttpKernel::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToLogManager::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToMailManager::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToNotificationChannelManager::class);
$this->app->singleton(Listeners\GiveNewApplicationInstanceToPipelineHub::class);
Expand Down
37 changes: 37 additions & 0 deletions tests/Listeners/GiveNewApplicationInstanceToLogManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Laravel\Octane\Tests\Listeners;

use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Facades\Log;
use Laravel\Octane\Tests\TestCase;

class GiveNewApplicationInstanceToLogManagerTest extends TestCase
{
public function test_context_is_appened_to_logs()
{
if (! class_exists(Context::class)) {
$this->markTestSkipped('Context is not available in this version of Laravel.');
}
[$app, $worker, $client] = $this->createOctaneContext([
Request::create('/', 'GET'),
]);
$path = $app['config']->get('logging.channels.single.path');
@unlink($path);

$this->assertFileDoesNotExist($path);

$app['router']->middleware('web')->get('/', function () {
Context::add('foo', 'bar');
Log::info('Hello world');
});

$worker->run();

$this->assertStringContainsString('{"foo":"bar"}', file_get_contents($path),);
}
}

0 comments on commit b407525

Please sign in to comment.