Skip to content

Commit 00f2289

Browse files
committed
fix: Initialize $formatter property in AbstractHandler and subclasses
- Prevent uninitialized property access errors by ensuring $formatter is initialized in AbstractHandler. - Update all handler constructors to accept a LogFormatter parameter and pass it to the parent constructor. - Set a default LineFormatter if no formatter is provided.
1 parent 0d76049 commit 00f2289

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

src/Handler/AbstractHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
use KaririCode\Contract\Logging\LogHandler;
1010
use KaririCode\Contract\Logging\LogLevel as LoggingLogLevel;
1111
use KaririCode\Contract\Logging\Structural\HandlerAware;
12+
use KaririCode\Logging\Formatter\LineFormatter;
1213
use KaririCode\Logging\LogLevel;
1314

1415
abstract class AbstractHandler implements LogHandler, HandlerAware
1516
{
1617
protected array $handlers = [];
17-
protected LogFormatter $formatter;
1818

1919
public function __construct(
2020
protected LoggingLogLevel $minLevel = LogLevel::DEBUG,
21+
protected LogFormatter $formatter = new LineFormatter()
2122
) {
2223
}
2324

tests/application.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,35 @@
3030
$defaultLogger = $loggerRegistry->getLogger('console');
3131

3232
$defaultLogger->debug('User email is john.doe@example.com');
33-
// $defaultLogger->info('User IP is 192.168.1.1');
34-
// $defaultLogger->notice('User credit card number is 1234-5678-1234-5678', ['context' => 'credit card']);
35-
// $defaultLogger->warning('User phone number is (11) 91234-7890', ['context' => 'phone']);
36-
// $defaultLogger->error('This is an error message with email john.doe@example.com', ['context' => 'error']);
37-
// $defaultLogger->critical('This is a critical message with IP 192.168.1.1', ['context' => 'critical']);
38-
// $defaultLogger->alert('This is an alert message with credit card 1234-5678-1234-5678', ['context' => 'alert']);
39-
// $defaultLogger->emergency('This is an emergency message with phone number 123-456-7890', ['context' => 'emergency']);
33+
$defaultLogger->info('User IP is 192.168.1.1');
34+
$defaultLogger->notice('User credit card number is 1234-5678-1234-5678', ['context' => 'credit card']);
35+
$defaultLogger->warning('User phone number is (11) 91234-7890', ['context' => 'phone']);
36+
$defaultLogger->error('This is an error message with email john.doe@example.com', ['context' => 'error']);
37+
$defaultLogger->critical('This is a critical message with IP 192.168.1.1', ['context' => 'critical']);
38+
$defaultLogger->alert('This is an alert message with credit card 1234-5678-1234-5678', ['context' => 'alert']);
39+
$defaultLogger->emergency('This is an emergency message with phone number 123-456-7890', ['context' => 'emergency']);
4040

41-
// $asyncLogger = $loggerRegistry->getLogger('async');
42-
// if ($asyncLogger) {
43-
// for ($i = 0; $i < 3; ++$i) {
44-
// $asyncLogger->info("Async log message {$i}", ['context' => "batch {$i}"]);
45-
// }
46-
// }
41+
$asyncLogger = $loggerRegistry->getLogger('async');
42+
if ($asyncLogger) {
43+
for ($i = 0; $i < 3; ++$i) {
44+
$asyncLogger->info("Async log message {$i}", ['context' => "batch {$i}"]);
45+
}
46+
}
4747

48-
// $queryLogger = $loggerRegistry->getLogger('query');
49-
// $queryLogger->info('Executing a query', ['time' => 90, 'query' => 'SELECT * FROM users', 'bindings' => []]);
48+
$queryLogger = $loggerRegistry->getLogger('query');
49+
$queryLogger->info('Executing a query', ['time' => 90, 'query' => 'SELECT * FROM users', 'bindings' => []]);
5050

51-
// $queryLogger = $loggerRegistry->getLogger('query');
52-
// $queryLogger->info('Executing a query', ['query' => 'SELECT * FROM users', 'bindings' => []]);
51+
$queryLogger = $loggerRegistry->getLogger('query');
52+
$queryLogger->info('Executing a query', ['query' => 'SELECT * FROM users', 'bindings' => []]);
5353

54-
// $performanceLogger = $loggerRegistry->getLogger('performance');
55-
// $performanceLogger->debug('Performance logging', ['execution_time' => 1000, 'additional_context' => 'example']);
54+
$performanceLogger = $loggerRegistry->getLogger('performance');
55+
$performanceLogger->debug('Performance logging', ['execution_time' => 1000, 'additional_context' => 'example']);
5656

57-
// $performanceLogger = $loggerRegistry->getLogger('performance');
58-
// $performanceLogger->debug('Performance logging');
57+
$performanceLogger = $loggerRegistry->getLogger('performance');
58+
$performanceLogger->debug('Performance logging');
5959

60-
// $errorLogger = $loggerRegistry->getLogger('error');
61-
// $errorLogger->error('This is a critical error.', ['context' => 'Testing error logger']);
60+
$errorLogger = $loggerRegistry->getLogger('error');
61+
$errorLogger->error('This is a critical error.', ['context' => 'Testing error logger']);
6262

63-
// $slackLogger = $loggerRegistry->getLogger('slack');
64-
// $slackLogger->critical('Este é um teste de mensagem crítica enviada para o Slack');
63+
$slackLogger = $loggerRegistry->getLogger('slack');
64+
$slackLogger->critical('Este é um teste de mensagem crítica enviada para o Slack');

0 commit comments

Comments
 (0)