Skip to content

Commit dcb466b

Browse files
committed
feat(logging): Enhance LoggerProcessorFactory to merge optional and channel-specific processors
- Updated LoggerProcessorFactory to ensure processors from both channel configurations and optional configurations are correctly merged. - Adjusted the createProcessors method to handle the merging of processor configurations. - Ensured that the processors are instantiated with the correct configuration parameters. - Added utility methods to handle configuration merging and validation.
1 parent 8c6629e commit dcb466b

File tree

4 files changed

+63
-34
lines changed

4 files changed

+63
-34
lines changed

src/Processor/LoggerProcessorFactory.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class LoggerProcessorFactory implements LoggerConfigurableFactory
1414
use ReflectionFactoryTrait;
1515

1616
private array $processorMap = [];
17-
private array $channelConfigs = [];
17+
private LoggerConfiguration $config;
1818

1919
public function initializeFromConfiguration(LoggerConfiguration $config): void
2020
{
@@ -27,23 +27,54 @@ public function initializeFromConfiguration(LoggerConfiguration $config): void
2727
'web_processor' => WebProcessor::class,
2828
]);
2929

30-
$this->channelConfigs = $config->get('channels', []);
30+
$this->config = $config;
3131
}
3232

3333
public function createProcessors(string $channelName): array
3434
{
35-
$channelConfig = $this->channelConfigs[$channelName] ?? [];
36-
$processorsConfig = $channelConfig['processors'] ?? [];
35+
$processorsConfig = $this->getProcessorsConfig($channelName);
3736
$processors = [];
3837
foreach ($processorsConfig as $key => $value) {
3938
[$processorName, $processorOptions] = $this->extractMergedConfig($key, $value);
40-
4139
$processors[] = $this->createProcessor($processorName, $processorOptions);
4240
}
4341

4442
return $processors;
4543
}
4644

45+
private function getProcessorsConfig(string $channelName): array
46+
{
47+
$channelProcessorConfig = $this->getChannelProcessorsConfig($channelName);
48+
$optionalProcessorConfig = $this->getOptionalProcessorsConfig($channelName);
49+
50+
return array_merge($channelProcessorConfig ?? [], $optionalProcessorConfig ?? []);
51+
}
52+
53+
private function getChannelProcessorsConfig(string $channelName): ?array
54+
{
55+
$channelConfigs = $this->config->get('channels', []);
56+
57+
return $channelConfigs[$channelName]['processors'] ?? null;
58+
}
59+
60+
private function getOptionalProcessorsConfig(string $channelName): ?array
61+
{
62+
$optionalProcessorConfigs = $this->config->get($channelName, []);
63+
64+
if (!self::isOptionalProcessorEnabled($optionalProcessorConfigs)) {
65+
return [];
66+
}
67+
68+
return $optionalProcessorConfigs['processors'] ?? $this->getChannelProcessorsConfig(
69+
$optionalProcessorConfigs['channel'] ?? 'file'
70+
);
71+
}
72+
73+
private static function isOptionalProcessorEnabled(array $optionalProcessorConfigs): bool
74+
{
75+
return isset($optionalProcessorConfigs['enabled']) && $optionalProcessorConfigs['enabled'];
76+
}
77+
4778
private function createProcessor(string $processorName, array $processorOptions): ProcessorAware
4879
{
4980
$processorClass = $this->getClassFromMap($this->processorMap, $processorName);

src/Processor/Metric/CpuUsageProcessor.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public function process(ImmutableValue $record): ImmutableValue
2222
$record->level,
2323
$record->message,
2424
$context,
25-
$record->datetime,
26-
$record->extra
25+
$record->datetime
2726
);
2827
}
2928
}

src/Processor/Metric/MemoryUsageProcessor.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public function process(ImmutableValue $record): ImmutableValue
2525
$record->level,
2626
$record->message,
2727
$context,
28-
$record->datetime,
29-
$record->extra
28+
$record->datetime
3029
);
3130
}
3231

tests/application.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,34 @@
2626

2727
$serviceProvider->register();
2828

29-
$defaultLogger = $loggerRegistry->getLogger('file');
30-
$defaultLogger->debug('This is a debug message');
31-
$defaultLogger->info('This is an info message');
32-
$defaultLogger->notice('This is a notice message', ['context' => 'notice']);
33-
$defaultLogger->warning('This is a warning message', ['context' => 'warning']);
34-
$defaultLogger->error('This is an error message', ['context' => 'error']);
35-
$defaultLogger->critical('This is a critical message', ['context' => 'critical']);
36-
$defaultLogger->alert('This is an alert message', ['context' => 'alert']);
37-
$defaultLogger->emergency('This is an emergency message', ['context' => 'emergency']);
38-
39-
$asyncLogger = $loggerRegistry->getLogger('async');
40-
if ($asyncLogger) {
41-
for ($i = 0; $i < 3; ++$i) {
42-
$asyncLogger->info("Async log message {$i}", ['context' => "batch {$i}"]);
43-
}
44-
}
45-
46-
$queryLogger = $loggerRegistry->getLogger('query');
47-
$queryLogger->info('Executing a query', ['time' => 90, 'query' => 'SELECT * FROM users', 'bindings' => []]);
48-
49-
$queryLogger = $loggerRegistry->getLogger('query');
50-
$queryLogger->info('Executing a query', ['query' => 'SELECT * FROM users', 'bindings' => []]);
29+
// $defaultLogger = $loggerRegistry->getLogger('console');
30+
// $defaultLogger->debug('This is a debug message');
31+
// $defaultLogger->info('This is an info message');
32+
// $defaultLogger->notice('This is a notice message', ['context' => 'notice']);
33+
// $defaultLogger->warning('This is a warning message', ['context' => 'warning']);
34+
// $defaultLogger->error('This is an error message', ['context' => 'error']);
35+
// $defaultLogger->critical('This is a critical message', ['context' => 'critical']);
36+
// $defaultLogger->alert('This is an alert message', ['context' => 'alert']);
37+
// $defaultLogger->emergency('This is an emergency message', ['context' => 'emergency']);
38+
39+
// $asyncLogger = $loggerRegistry->getLogger('async');
40+
// if ($asyncLogger) {
41+
// for ($i = 0; $i < 3; ++$i) {
42+
// $asyncLogger->info("Async log message {$i}", ['context' => "batch {$i}"]);
43+
// }
44+
// }
45+
46+
// $queryLogger = $loggerRegistry->getLogger('query');
47+
// $queryLogger->info('Executing a query', ['time' => 90, 'query' => 'SELECT * FROM users', 'bindings' => []]);
48+
49+
// $queryLogger = $loggerRegistry->getLogger('query');
50+
// $queryLogger->info('Executing a query', ['query' => 'SELECT * FROM users', 'bindings' => []]);
5151

5252
$performanceLogger = $loggerRegistry->getLogger('performance');
53-
$performanceLogger->debug('Performance logging', ['execution_time' => 100, 'additional_context' => 'example']);
53+
$performanceLogger->debug('Performance logging', ['execution_time' => 1000, 'additional_context' => 'example']);
5454

55-
$performanceLogger = $loggerRegistry->getLogger('performance');
56-
$performanceLogger->debug('Performance logging', ['additional_context' => 'example']);
55+
// $performanceLogger = $loggerRegistry->getLogger('performance');
56+
// $performanceLogger->debug('Performance logging');
5757

5858
// // // Testa o error logger
5959
// // if (LoggerRegistry::getLogger('error')) {

0 commit comments

Comments
 (0)