diff --git a/src/Bootstrappers/LogTenancyBootstrapper.php b/src/Bootstrappers/LogTenancyBootstrapper.php index 6785a80a..0355dd7e 100644 --- a/src/Bootstrappers/LogTenancyBootstrapper.php +++ b/src/Bootstrappers/LogTenancyBootstrapper.php @@ -37,7 +37,7 @@ class LogTenancyBootstrapper implements TenancyBootstrapper * * Examples: * - Array mapping (the default approach): ['slack' => ['url' => 'webhookUrl']] maps $tenant->webhookUrl to slack.url (if $tenant->webhookUrl is not null, otherwise, the override is ignored) - * - Closure: ['slack' => fn ($config, $tenant) => $config->set('logging.channels.slack.url', $tenant->slackUrl)] + * - Closure: ['slack' => fn (Tenant $tenant, array $channel) => array_merge($channel, ['url' => $tenant->slackUrl])] */ public static array $channelOverrides = []; @@ -100,7 +100,7 @@ class LogTenancyBootstrapper implements TenancyBootstrapper $this->overrideChannelConfig($channel, static::$channelOverrides[$channel], $tenant); } 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" + // 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')); } } @@ -122,7 +122,7 @@ class LogTenancyBootstrapper implements TenancyBootstrapper } elseif ($override instanceof Closure) { $channelConfigKey = "logging.channels.{$channel}"; - $this->config->set($channelConfigKey, $override($this->config->get($channelConfigKey), $tenant)); + $this->config->set($channelConfigKey, $override($tenant, $this->config->get($channelConfigKey))); } } diff --git a/tests/Bootstrappers/LogTenancyBootstrapperTest.php b/tests/Bootstrappers/LogTenancyBootstrapperTest.php index 86b112f1..eb43e605 100644 --- a/tests/Bootstrappers/LogTenancyBootstrapperTest.php +++ b/tests/Bootstrappers/LogTenancyBootstrapperTest.php @@ -14,7 +14,7 @@ use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; beforeEach(function () { config([ 'tenancy.bootstrappers' => [ - // FilesystemTenancyBootstrapper needed for storage path channels (added in tests that check the storage path channel logic) + // FilesystemTenancyBootstrapper needed for LogTenancyBootstrapper to work with storage path channels BY DEFAULT (note that this can be completely overridden) LogTenancyBootstrapper::class, ], ]); @@ -33,7 +33,7 @@ afterEach(function () { }); test('storage path channels get tenant-specific paths by default', function () { - // Note that for LogTenancyBootstrapper to change the paths correctly, + // Note that for LogTenancyBootstrapper to change the paths correctly by default, // the bootstrapper MUST run after FilesystemTenancyBootstrapper. config([ 'tenancy.bootstrappers' => [ @@ -113,7 +113,7 @@ test('channel overrides work correctly with both arrays and closures', function // Test both array mapping and closure-based overrides LogTenancyBootstrapper::$channelOverrides = [ 'slack' => ['url' => 'webhookUrl'], // slack.url will be mapped to $tenant->webhookUrl - 'single' => function (array $channel, Tenant $tenant) { + 'single' => function (Tenant $tenant, array $channel) { return array_merge($channel, ['path' => storage_path("logs/override-{$tenant->id}.log")]); }, ]; @@ -155,7 +155,7 @@ test('channel overrides take precedence over the default storage path channel up $tenant = Tenant::create(['id' => 'tenant1']); LogTenancyBootstrapper::$channelOverrides = [ - 'single' => function (array $channel, Tenant $tenant) { + 'single' => function (Tenant $tenant, array $channel) { return array_merge($channel, ['path' => storage_path("logs/override-{$tenant->id}.log")]); }, ]; @@ -261,7 +261,7 @@ test('logs are written to tenant-specific files and do not leak between contexts $tenant = Tenant::create(['id' => 'override-tenant']); LogTenancyBootstrapper::$channelOverrides = [ - 'single' => function (array $channel, Tenant $tenant) { + 'single' => function (Tenant $tenant, array $channel) { // The tenant log path will be set to storage/tenantoverride-tenant/logs/custom-override-tenant.log return array_merge($channel, ['path' => storage_path("logs/custom-{$tenant->id}.log")]); },