1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-05-06 15:44:03 +00:00

Preserve filename from central log path in tenant context

This commit is contained in:
lukinovec 2026-04-14 10:46:15 +02:00
parent 9ea3813d28
commit 23ae15a8f1
2 changed files with 44 additions and 1 deletions

View file

@ -133,7 +133,9 @@ class LogTenancyBootstrapper implements TenancyBootstrapper
} elseif (in_array($channel, static::$storagePathChannels)) {
// Set storage path channels to use tenant-specific directory (default behavior)
// The tenant log will be located at e.g. "storage/tenant{$tenantKey}/logs/laravel.log" (assuming FilesystemTenancyBootstrapper is used before this bootstrapper)
$this->config->set("logging.channels.{$channel}.path", storage_path('logs/laravel.log'));
$path = $this->config->get("logging.channels.{$channel}.path");
$this->config->set("logging.channels.{$channel}.path", storage_path('logs/' . ($path ? basename($path) : 'laravel.log')));
}
}
}

View file

@ -397,3 +397,44 @@ test('slack channel uses correct webhook urls', function () {
// Central context, central webhook should be used again
$assertWebhook('central-webhook', 'central');
});
test('tenant logs inherit the filename from the central log path config', function () {
config([
'tenancy.bootstrappers' => [
FilesystemTenancyBootstrapper::class,
LogTenancyBootstrapper::class,
],
'logging.channels.stack' => [
'driver' => 'stack',
'channels' => ['single', 'daily'],
],
'logging.channels.single.path' => storage_path('logs/custom-name.log'),
'logging.channels.daily.path' => storage_path('logs/custom-name.log'),
]);
$tenant = Tenant::create();
$today = now()->format('Y-m-d');
// Central log is located at storage/logs/custom-name.log
Log::channel('stack')->info('central');
expect(file_get_contents(storage_path('logs/custom-name.log')))->toContain('central');
expect(file_get_contents(storage_path("logs/custom-name-{$today}.log")))->toContain('central');
tenancy()->initialize($tenant);
// Tenant log is located at storage/tenantX/logs/custom-name.log
Log::channel('stack')->info($tenant->id);
// The filename from the central config is preserved in tenant context
expect(config('logging.channels.single.path'))->toEndWith('custom-name.log');
expect(config('logging.channels.daily.path'))->toEndWith('custom-name.log');
expect(file_get_contents(storage_path('logs/custom-name.log')))
->toContain($tenant->id)
->not()->toContain('central');
expect(file_get_contents(storage_path("logs/custom-name-{$today}.log")))
->toContain($tenant->id)
->not()->toContain('central');
});