Skip to content

Commit 0d76049

Browse files
committed
Refactor logging configuration and update handlers
1 parent 0da0615 commit 0d76049

File tree

6 files changed

+27
-8
lines changed

6 files changed

+27
-8
lines changed

config/logging.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
return [
77
'default' => Config::env('LOG_CHANNEL', 'file'),
8+
'timezone' => Config::env('LOG_TIMEZONE', 'UTC'),
89
'channels' => [
910
'file' => [
1011
'minLevel' => Config::env('LOG_LEVEL', 'debug'),

src/Handler/AbstractHandler.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
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;
1312
use KaririCode\Logging\LogLevel;
1413

1514
abstract class AbstractHandler implements LogHandler, HandlerAware
1615
{
1716
protected array $handlers = [];
17+
protected LogFormatter $formatter;
1818

1919
public function __construct(
2020
protected LoggingLogLevel $minLevel = LogLevel::DEBUG,
21-
protected LogFormatter $formatter = new LineFormatter()
2221
) {
2322
}
2423

src/Handler/ConsoleHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct(
2323
) {
2424
parent::__construct($minLevel, $formatter);
2525
$this->output = fopen('php://stdout', 'w');
26+
$this->setFormatter($formatter);
2627
$this->colorFormatter = new ConsoleColorFormatter();
2728
}
2829

src/Util/ConfigLoader/EnvLoader.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public function load(): void
2121
continue;
2222
}
2323
[$name, $value] = $this->parseEnvLine($line);
24-
putenv(sprintf('%s=%s', $name, $value));
24+
$sanitizedValue = $this->sanitizeValue($value);
25+
putenv(sprintf('%s=%s', $name, $sanitizedValue));
2526
}
2627
}
2728

@@ -50,4 +51,21 @@ private function parseEnvLine(string $line): array
5051

5152
return [trim($name), trim($value)];
5253
}
54+
55+
private function sanitizeValue(string $value): string
56+
{
57+
// Remove any potentially harmful characters
58+
$value = preg_replace('/[^a-zA-Z0-9_\-\.,@\/\\\\:;]/', '', $value);
59+
60+
// Ensure the value doesn't start with a dash (which could be interpreted as a command line option)
61+
$value = ltrim($value, '-');
62+
63+
// Limit the length of the value to prevent buffer overflow attacks
64+
$maxLength = 1000; // Adjust this value based on your requirements
65+
if (strlen($value) > $maxLength) {
66+
$value = substr($value, 0, $maxLength);
67+
}
68+
69+
return $value;
70+
}
5371
}

tests/Logger/test_config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return ['key' => 'value'];
1+
<?php return ['key' => 'value'];

tests/application.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727

2828
$serviceProvider->register();
2929

30-
// $defaultLogger = $loggerRegistry->getLogger('console');
30+
$defaultLogger = $loggerRegistry->getLogger('console');
3131

32-
// $defaultLogger->debug('User email is john.doe@example.com');
32+
$defaultLogger->debug('User email is john.doe@example.com');
3333
// $defaultLogger->info('User IP is 192.168.1.1');
3434
// $defaultLogger->notice('User credit card number is 1234-5678-1234-5678', ['context' => 'credit card']);
3535
// $defaultLogger->warning('User phone number is (11) 91234-7890', ['context' => 'phone']);
@@ -60,5 +60,5 @@
6060
// $errorLogger = $loggerRegistry->getLogger('error');
6161
// $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)