Skip to content

Commit 99c06a4

Browse files
committed
Refactor Logging System: Enhance Logger Handler Factory and Service Provider
- Implement LoggerHandlerFactory with improved factory methods for handlers - Ensure channel and optional handler configurations are correctly merged - Add method to retrieve handlers configuration cleanly - Update LoggerServiceProvider to include better registration of default, emergency, and optional loggers - Refactor logger configuration for better clarity and maintainability - Enhance logging configuration file for better separation of concerns and flexibility
1 parent d1bcab6 commit 99c06a4

File tree

5 files changed

+54
-32
lines changed

5 files changed

+54
-32
lines changed

.env.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ ASYNC_LOG_ENABLED=true
2626

2727
# Habilitar logs de consulta
2828
QUERY_LOG_ENABLED=true
29-
QUERY_LOG_CHANNEL=daily
29+
QUERY_LOG_CHANNEL=file
3030
QUERY_LOG_THRESHOLD=100
3131

3232
# Habilitar logs de desempenho
3333
PERFORMANCE_LOG_ENABLED=false
34-
PERFORMANCE_LOG_CHANNEL=daily
34+
PERFORMANCE_LOG_CHANNEL=file
3535
PERFORMANCE_LOG_THRESHOLD=1000
3636

3737
# Habilitar logs de erro
3838
ERROR_LOG_ENABLED=true
39-
ERROR_LOG_CHANNEL=daily
39+
ERROR_LOG_CHANNEL=file
4040

4141
# Configurações do limpador de logs
4242
LOG_CLEANER_ENABLED=true

config/logging.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,11 @@
233233
'driver' => \KaririCode\Logging\Decorator\AsyncLogger::class,
234234
'batch_size' => 10,
235235
],
236-
'emergency_logger' => [
236+
'emergency' => [
237237
'path' => ConfigHelper::storagePath('logs/emergency.log'),
238238
'level' => LogLevel::EMERGENCY,
239239
],
240-
'query_logger' => [
240+
'query' => [
241241
'enabled' => ConfigHelper::env('QUERY_LOG_ENABLED', false),
242242
'channel' => ConfigHelper::env('QUERY_LOG_CHANNEL', 'file'),
243243
'threshold' => ConfigHelper::env('QUERY_LOG_THRESHOLD', 100), // in milliseconds
@@ -250,7 +250,7 @@
250250
],
251251
],
252252
],
253-
'performance_logger' => [
253+
'performance' => [
254254
'enabled' => ConfigHelper::env('PERFORMANCE_LOG_ENABLED', false),
255255
'channel' => ConfigHelper::env('PERFORMANCE_LOG_CHANNEL', 'file'),
256256
'threshold' => ConfigHelper::env('PERFORMANCE_LOG_THRESHOLD', 1000), // in milliseconds
@@ -265,7 +265,7 @@
265265
'execution_time_processor',
266266
],
267267
],
268-
'error_logger' => [
268+
'error' => [
269269
'enabled' => ConfigHelper::env('ERROR_LOG_ENABLED', true),
270270
'channel' => ConfigHelper::env('ERROR_LOG_CHANNEL', 'file'),
271271
'levels' => [

src/Handler/LoggerHandlerFactory.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace KaririCode\Logging\Handler;
66

7+
use InvalidArgumentException;
78
use KaririCode\Contract\Logging\LogHandler;
89
use KaririCode\Logging\Contract\Logging\LoggerConfigurableFactory;
910
use KaririCode\Logging\LoggerConfiguration;
@@ -14,7 +15,8 @@ class LoggerHandlerFactory implements LoggerConfigurableFactory
1415
use ReflectionFactoryTrait;
1516

1617
private array $handlerMap = [];
17-
private array $channelConfigs = [];
18+
private LoggerConfiguration $config;
19+
1820

1921
public function initializeFromConfiguration(LoggerConfiguration $config): void
2022
{
@@ -25,23 +27,43 @@ public function initializeFromConfiguration(LoggerConfiguration $config): void
2527
'syslog' => SyslogUdpHandler::class,
2628
]);
2729

28-
$this->channelConfigs = $config->get('channels', []);
30+
$this->config = $config;
2931
}
3032

31-
public function createHandlers(string $channelName): array
33+
public function createHandlers(string $handlerName): array
3234
{
33-
$channelConfig = $this->channelConfigs[$channelName] ?? [];
34-
$handlersConfig = $channelConfig['handlers'] ?? [];
35+
$handlersConfig = $this->getHandlersConfig($handlerName);
3536

3637
$handlers = [];
3738
foreach ($handlersConfig as $key => $value) {
3839
[$handlerName, $handlerOptions] = $this->extractMergedConfig($key, $value);
3940
$handlers[] = $this->createHandler($handlerName, $handlerOptions);
4041
}
41-
4242
return $handlers;
4343
}
4444

45+
private function getHandlersConfig(string $channelName): array
46+
{
47+
$channelHandlerConfig = $this->getChannelHandlersConfig($channelName);
48+
$optionalHandlerConfig = $this->getOptionalHandlersConfig($channelName);
49+
50+
return $channelHandlerConfig ?? $optionalHandlerConfig ?? [];
51+
}
52+
53+
private function getChannelHandlersConfig(string $channelName): ?array
54+
{
55+
$channelConfigs = $this->config->get('channels', []);
56+
return $channelConfigs[$channelName]['handlers'] ?? null;
57+
}
58+
59+
private function getOptionalHandlersConfig(string $channelName): ?array
60+
{
61+
$optionalHandlerConfigs = $this->config->get($channelName, []);
62+
return $optionalHandlerConfigs['handlers'] ?? $this->getChannelHandlersConfig(
63+
$optionalHandlerConfigs['channel'] ?? 'file'
64+
);
65+
}
66+
4567
private function createHandler(string $handlerName, array $handlerOptions): LogHandler
4668
{
4769
$handlerClass = $this->getClassFromMap($this->handlerMap, $handlerName);

src/Service/LoggerServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ private function registerEmergencyLogger(): void
5656

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

tests/application.php

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

2727
$serviceProvider->register();
2828

29-
// $defaultLogger = $loggerRegistry->getLogger('file');
30-
// $defaultLogger->debug('This is a debug message', ['context' => 'debug']);
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 < 2; ++$i) {
42-
// $asyncLogger->info("Async log message {$i}", ['context' => "batch {$i}"]);
43-
// }
44-
// }
29+
$defaultLogger = $loggerRegistry->getLogger('console');
30+
$defaultLogger->debug('This is a debug message', ['context' => 'debug']);
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 < 2; ++$i) {
42+
$asyncLogger->info("Async log message {$i}", ['context' => "batch {$i}"]);
43+
}
44+
}
4545

4646
$queryLogger = $loggerRegistry->getLogger('query');
4747
$queryLogger->info('Executing a query', ['query' => 'SELECT * FROM users', 'bindings' => [], 'time' => 150]);

0 commit comments

Comments
 (0)