Skip to content

Commit b946aa6

Browse files
committed
Add validation for optional log configurations
- Added constants OPTIONAL_LOG_KEYS and OPTIONAL_LOGS to define minimum required keys and the list of optional logs. - Implemented validateOptionalLogs method to validate optional logs (query, performance, error) in the configuration. - Ensured that optional logs have at least 'enabled' and 'channel' keys. - Added checks to ensure 'handlers' and 'processors' are arrays if present in optional logs. - Integrated validateOptionalLogs into the main validate method.
1 parent 99c06a4 commit b946aa6

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

src/Service/LoggerServiceProvider.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,27 @@ private function registerEmergencyLogger(): void
5656

5757
private function registerOptionalLoggers(): void
5858
{
59-
$this->registerLoggerIfEnabled('query', 'createQueryLogger');
60-
$this->registerLoggerIfEnabled('performance', 'createPerformanceLogger');
61-
$this->registerLoggerIfEnabled('error', 'createErrorLogger', true);
59+
$this->registerLogger('query', 'createQueryLogger');
60+
$this->registerLogger('performance', 'createPerformanceLogger');
61+
$this->registerLogger('error', 'createErrorLogger');
6262
$this->registerAsyncLoggerIfEnabled();
6363
}
6464

65-
private function registerLoggerIfEnabled(
65+
private function registerLogger(
6666
string $configKey,
6767
string $factoryMethod,
68-
bool $defaultEnabled = false
6968
): void {
70-
if ($this->config->get("$configKey.enabled", $defaultEnabled)) {
71-
$loggerConfig = $this->config->get($configKey, []);
72-
$logger = $this->loggerFactory->$factoryMethod($loggerConfig);
73-
$this->loggerRegistry->addLogger(explode('_', $configKey)[0], $logger);
74-
}
69+
$loggerConfig = $this->config->get($configKey, []);
70+
$logger = $this->loggerFactory->$factoryMethod($loggerConfig);
71+
$this->loggerRegistry->addLogger($configKey, $logger);
7572
}
7673

7774
private function registerAsyncLoggerIfEnabled(): void
7875
{
79-
if ($this->config->get('async.enabled', true)) {
80-
$asyncLogger = $this->loggerFactory->createAsyncLogger(
81-
$this->loggerRegistry->getLogger('default'),
82-
(int) $this->config->get('async.batch_size', 10)
83-
);
84-
$this->loggerRegistry->addLogger('async', $asyncLogger);
85-
}
76+
$asyncLogger = $this->loggerFactory->createAsyncLogger(
77+
$this->loggerRegistry->getLogger('default'),
78+
(int) $this->config->get('async.batch_size', 10)
79+
);
80+
$this->loggerRegistry->addLogger('async', $asyncLogger);
8681
}
8782
}

src/Validation/ConfigurationValidator.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,25 @@ class ConfigurationValidator
3232
'class',
3333
];
3434

35+
private const OPTIONAL_LOG_KEYS = [
36+
'enabled',
37+
'channel',
38+
];
39+
40+
private const OPTIONAL_LOGS = [
41+
'query',
42+
'performance',
43+
'error',
44+
];
45+
3546
public function validate(array $config): void
3647
{
3748
$this->validateRequiredKeys($config, self::REQUIRED_KEYS);
3849
$this->validateChannels($config['channels']);
3950
$this->validateHandlers($config['handlers']);
4051
$this->validateProcessors($config['processors']);
4152
$this->validateFormatters($config['formatters']);
53+
$this->validateOptionalLogs($config);
4254
}
4355

4456
private function validateRequiredKeys(array $config, array $requiredKeys, string $context = ''): void
@@ -93,4 +105,21 @@ private function validateFormatters(array $formatters): void
93105
}
94106
}
95107
}
108+
109+
private function validateOptionalLogs(array $config): void
110+
{
111+
foreach (self::OPTIONAL_LOGS as $log) {
112+
if (isset($config[$log])) {
113+
$this->validateRequiredKeys($config[$log], self::OPTIONAL_LOG_KEYS, "optional log '{$log}'");
114+
115+
if (isset($config[$log]['handlers']) && !is_array($config[$log]['handlers'])) {
116+
throw new InvalidConfigurationException("Handlers for optional log '{$log}' must be an array");
117+
}
118+
119+
if (isset($config[$log]['processors']) && !is_array($config[$log]['processors'])) {
120+
throw new InvalidConfigurationException("Processors for optional log '{$log}' must be an array");
121+
}
122+
}
123+
}
124+
}
96125
}

tests/application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
$serviceProvider->register();
2828

29-
$defaultLogger = $loggerRegistry->getLogger('console');
29+
$defaultLogger = $loggerRegistry->getLogger('file');
3030
$defaultLogger->debug('This is a debug message', ['context' => 'debug']);
3131
$defaultLogger->info('This is an info message');
3232
$defaultLogger->notice('This is a notice message', ['context' => 'notice']);

0 commit comments

Comments
 (0)