Skip to content

Commit a752812

Browse files
committed
Refactor and enhance logging anonymization:
- Updated .env.example and config/logging.php for new configurations. - Added AnonymizerStrategy interface. - Refactored Anonymizer classes for better design and maintainability. - Removed unnecessary ExceptionHandler. - Improved and added new handlers and processors. - Enhanced Encryptor and renamed ConfigHelper to Config. - Added new test configurations and updated application tests.
1 parent dcb466b commit a752812

22 files changed

+301
-163
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ LOG_CHANNEL=file
77

88
# Nível de log padrão
99
LOG_LEVEL=debug
10+
LOG_ENCRYPTION_KEY=83302e6472acda6a8aeadf78409ceda3959994991393cdafbe23d2a46a148ba4
1011

1112
# URL do Webhook do Slack para logs
1213
LOG_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX

config/logging.php

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
<?php
22

33
use KaririCode\Logging\LogLevel;
4-
use KaririCode\Logging\Util\ConfigHelper;
4+
use KaririCode\Logging\Util\Config;
55

66
return [
7-
'default' => ConfigHelper::env('LOG_CHANNEL', 'file'),
7+
'default' => Config::env('LOG_CHANNEL', 'file'),
88
'channels' => [
99
'file' => [
10-
'level' => ConfigHelper::env('LOG_LEVEL', 'debug'),
10+
'minLevel' => Config::env('LOG_LEVEL', 'debug'),
1111
'handlers' => [
1212
'file' => [
13-
'with' => ['filePath' => ConfigHelper::storagePath('logs/file2.log')],
13+
'with' => ['filePath' => Config::storagePath('logs/file2.log')],
1414
],
1515
],
1616
'processors' => [
17-
'introspection_processor' => [
18-
'class' => \KaririCode\Logging\Processor\IntrospectionProcessor::class,
19-
'with' => [
20-
'stackDepth' => 7
21-
]
22-
],
17+
'introspection_processor',
18+
'anonymizer_processor'
2319
],
2420
'formatter' => [
2521
'line' => [
@@ -30,7 +26,7 @@
3026
],
3127
],
3228
'console' => [
33-
'level' => ConfigHelper::env('LOG_LEVEL', 'debug'),
29+
'minLevel' => Config::env('LOG_LEVEL', 'debug'),
3430
'handlers' => ['console'],
3531
'formatter' => [
3632
'json' => [
@@ -42,14 +38,15 @@
4238

4339
'processors' => [
4440
'introspection_processor',
41+
'anonymizer_processor'
4542
],
4643
],
4744
'syslog' => [
48-
'level' => ConfigHelper::env('LOG_LEVEL', 'debug'),
45+
'minLevel' => Config::env('LOG_LEVEL', 'debug'),
4946
'handlers' => ['syslog'],
5047
],
5148
'slack' => [
52-
'level' => ConfigHelper::env('LOG_LEVEL', 'critical'),
49+
'minLevel' => Config::env('LOG_LEVEL', 'critical'),
5350
'handlers' => ['slack'],
5451
'formatter' => [
5552
'json' => [
@@ -64,37 +61,37 @@
6461
],
6562
],
6663
'async' => [
67-
'enabled' => ConfigHelper::env('ASYNC_LOG_ENABLED', true),
68-
'batch_size' => ConfigHelper::env('ASYNC_LOG_BATCH_SIZE', 10),
69-
'channel' => ConfigHelper::env('ASYNC_LOG_CHANNEL', 'file'),
64+
'enabled' => Config::env('ASYNC_LOG_ENABLED', true),
65+
'batch_size' => Config::env('ASYNC_LOG_BATCH_SIZE', 10),
66+
'channel' => Config::env('ASYNC_LOG_CHANNEL', 'file'),
7067
],
7168
'emergency' => [
72-
'path' => ConfigHelper::storagePath('logs/emergency.log'),
73-
'level' => LogLevel::EMERGENCY,
69+
'minLevel' => LogLevel::EMERGENCY,
70+
'path' => Config::storagePath('logs/emergency.log'),
7471
],
7572
'query' => [
76-
'enabled' => ConfigHelper::env('QUERY_LOG_ENABLED', false),
77-
'channel' => ConfigHelper::env('QUERY_LOG_CHANNEL', 'file'),
78-
'threshold' => ConfigHelper::env('QUERY_LOG_THRESHOLD', 100), // in milliseconds
73+
'enabled' => Config::env('QUERY_LOG_ENABLED', false),
74+
'channel' => Config::env('QUERY_LOG_CHANNEL', 'file'),
75+
'threshold' => Config::env('QUERY_LOG_THRESHOLD', 100), // in milliseconds
7976
'handlers' => [
8077
'console' => [
8178
'with' => ['useColors' => true],
8279
],
8380
'file' => [
84-
'with' => ['filePath' => ConfigHelper::storagePath('logs/query.log')],
81+
'with' => ['filePath' => Config::storagePath('logs/query.log')],
8582
],
8683
],
8784
],
8885
'performance' => [
89-
'enabled' => ConfigHelper::env('PERFORMANCE_LOG_ENABLED', false),
90-
'channel' => ConfigHelper::env('PERFORMANCE_LOG_CHANNEL', 'file'),
91-
'threshold' => ConfigHelper::env('PERFORMANCE_LOG_THRESHOLD', 1000), // in milliseconds
86+
'enabled' => Config::env('PERFORMANCE_LOG_ENABLED', false),
87+
'channel' => Config::env('PERFORMANCE_LOG_CHANNEL', 'file'),
88+
'threshold' => Config::env('PERFORMANCE_LOG_THRESHOLD', 1000), // in milliseconds
9289
'handlers' => [
9390
'console' => [
9491
'with' => ['useColors' => true],
9592
],
9693
'file' => [
97-
'with' => ['filePath' => ConfigHelper::storagePath('logs/performance.log')],
94+
'with' => ['filePath' => Config::storagePath('logs/performance.log')],
9895
],
9996
],
10097
'processors' => [
@@ -104,8 +101,8 @@
104101
],
105102
],
106103
'error' => [
107-
'enabled' => ConfigHelper::env('ERROR_LOG_ENABLED', true),
108-
'channel' => ConfigHelper::env('ERROR_LOG_CHANNEL', 'file'),
104+
'enabled' => Config::env('ERROR_LOG_ENABLED', true),
105+
'channel' => Config::env('ERROR_LOG_CHANNEL', 'file'),
109106
'levels' => [
110107
LogLevel::ERROR,
111108
LogLevel::CRITICAL,
@@ -114,15 +111,15 @@
114111
],
115112
],
116113
'log_cleaner' => [
117-
'enabled' => ConfigHelper::env('LOG_CLEANER_ENABLED', true),
118-
'keep_days' => ConfigHelper::env('LOG_CLEANER_KEEP_DAYS', 30),
114+
'enabled' => Config::env('LOG_CLEANER_ENABLED', true),
115+
'keep_days' => Config::env('LOG_CLEANER_KEEP_DAYS', 30),
119116
'channels' => ['single', 'file'],
120117
],
121118
'handlers' => [
122119
'file' => [
123120
'class' => \KaririCode\Logging\Handler\FileHandler::class,
124121
'with' => [
125-
'filePath' => ConfigHelper::storagePath('logs/file.log'),
122+
'filePath' => Config::storagePath('logs/file.log'),
126123
],
127124
],
128125
'console' => [
@@ -143,16 +140,16 @@
143140
'class' => \KaririCode\Logging\Handler\SlackHandler::class,
144141
'with' => [
145142
'slackClient' => \KaririCode\Logging\Util\SlackClient::create(
146-
ConfigHelper::env('LOG_SLACK_WEBHOOK_URL'),
143+
Config::env('LOG_SLACK_WEBHOOK_URL'),
147144
new \KaririCode\Logging\Resilience\CircuitBreaker(
148-
ConfigHelper::env('CIRCUIT_BREAKER_FAILURE_THRESHOLD', 3),
149-
ConfigHelper::env('CIRCUIT_BREAKER_RESET_TIMEOUT', 60)
145+
Config::env('CIRCUIT_BREAKER_FAILURE_THRESHOLD', 3),
146+
Config::env('CIRCUIT_BREAKER_RESET_TIMEOUT', 60)
150147
),
151148
new \KaririCode\Logging\Resilience\Retry(
152-
ConfigHelper::env('RETRY_MAX_ATTEMPTS', 3),
153-
ConfigHelper::env('RETRY_DELAY', 1000),
154-
ConfigHelper::env('RETRY_MULTIPLIER', 2),
155-
ConfigHelper::env('RETRY_JITTER', 100)
149+
Config::env('RETRY_MAX_ATTEMPTS', 3),
150+
Config::env('RETRY_DELAY', 1000),
151+
Config::env('RETRY_MULTIPLIER', 2),
152+
Config::env('RETRY_JITTER', 100)
156153
),
157154
new \KaririCode\Logging\Resilience\Fallback(),
158155
new \KaririCode\Logging\Util\CurlClient()
@@ -167,27 +164,39 @@
167164
'processors' => [
168165
'introspection_processor' => [
169166
'class' => \KaririCode\Logging\Processor\IntrospectionProcessor::class,
170-
'level' => LogLevel::DEBUG,
167+
'with' => [
168+
'stackDepth' => 7
169+
]
171170
],
172171
'memory_usage_processor' => [
173172
'class' => \KaririCode\Logging\Processor\Metric\MemoryUsageProcessor::class,
174-
'level' => LogLevel::DEBUG,
175173
],
176174
'execution_time_processor' => [
177175
'class' => \KaririCode\Logging\Processor\Metric\ExecutionTimeProcessor::class,
178-
'level' => LogLevel::DEBUG,
179176
],
180177
'cpu_usage_processor' => [
181178
'class' => \KaririCode\Logging\Processor\Metric\CpuUsageProcessor::class,
182-
'level' => LogLevel::DEBUG,
183179
],
184180
'metrics_processor' => [
185181
'class' => \KaririCode\Logging\Processor\MetricsProcessor::class,
186-
'level' => LogLevel::DEBUG,
187182
],
188183
'web_processor' => [
189184
'class' => \KaririCode\Logging\Processor\WebProcessor::class,
190-
'level' => LogLevel::INFO,
185+
],
186+
'anonymizer_processor' => [
187+
'class' => \KaririCode\Logging\Processor\AnonymizerProcessor::class,
188+
'with' => [
189+
'anonymizer' => new \KaririCode\Logging\Security\Anonymizer([
190+
'phone' => new \KaririCode\Logging\Security\Anonymizer\PhoneAnonymizer(),
191+
'ip' => new \KaririCode\Logging\Security\Anonymizer\IpAnonymizer(),
192+
]),
193+
],
194+
],
195+
'encryption_processor' => [
196+
'class' => \KaririCode\Logging\Processor\EncryptionProcessor::class,
197+
'with' => [
198+
'encryptor' => new \KaririCode\Logging\Security\Encryptor(Config::env('LOG_ENCRYPTION_KEY')),
199+
],
191200
],
192201
],
193202
'formatters' => [

src/Contract/AnonymizerStrategy.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Logging\Contract;
6+
7+
interface AnonymizerStrategy
8+
{
9+
public function anonymize(string $value): string;
10+
11+
public function mask(string $value): string;
12+
13+
public function getPattern(): string;
14+
}

src/Contract/Logging/LoggerConfigurableFactory.php renamed to src/Contract/LoggerConfigurableFactory.php

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

33
declare(strict_types=1);
44

5-
namespace KaririCode\Logging\Contract\Logging;
5+
namespace KaririCode\Logging\Contract;
66

77
use KaririCode\Logging\LoggerConfiguration;
88

src/Formatter/LoggerFormatterFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace KaririCode\Logging\Formatter;
66

77
use KaririCode\Contract\Logging\LogFormatter;
8-
use KaririCode\Logging\Contract\Logging\LoggerConfigurableFactory;
8+
use KaririCode\Logging\Contract\LoggerConfigurableFactory;
99
use KaririCode\Logging\LoggerConfiguration;
1010
use KaririCode\Logging\Util\ReflectionFactoryTrait;
1111

src/Handler/ConsoleHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function __construct(
2828

2929
public function handle(ImmutableValue $record): void
3030
{
31+
if (!$this->isHandling($record)) {
32+
return;
33+
}
34+
3135
$message = $this->formatter->format($record);
3236
if ($this->useColors) {
3337
$message = $this->colorFormatter->format($record->level, $message);

src/Handler/ExceptionHandler.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Handler/FileHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class FileHandler extends AbstractFileHandler
1010
{
1111
public function handle(ImmutableValue $record): void
1212
{
13-
if ($record->level->value < $this->minLevel->value) {
13+
if (!$this->isHandling($record)) {
1414
return;
1515
}
1616

src/Handler/LoggerHandlerFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace KaririCode\Logging\Handler;
66

77
use KaririCode\Contract\Logging\LogHandler;
8-
use KaririCode\Logging\Contract\Logging\LoggerConfigurableFactory;
8+
use KaririCode\Logging\Contract\LoggerConfigurableFactory;
99
use KaririCode\Logging\LoggerConfiguration;
1010
use KaririCode\Logging\LogLevel;
1111
use KaririCode\Logging\Util\ReflectionFactoryTrait;
@@ -80,9 +80,9 @@ private function createHandler(string $handlerName, array $handlerOptions): LogH
8080
$handlerConfig = $this->getHandlerConfig($handlerName, $handlerOptions);
8181

8282
$channelConfig = $this->config->get("channels.$handlerName", []);
83-
$levelConfig = ['minLevel' => $channelConfig['level'] ?? LogLevel::DEBUG];
83+
$handlerConfig['minLevel'] = LogLevel::from($channelConfig['minLevel'] ?? LogLevel::DEBUG->value);
8484

85-
return $this->createInstance($handlerClass, $handlerConfig, $levelConfig);
85+
return $this->createInstance($handlerClass, $handlerConfig);
8686
}
8787

8888
private function getHandlerConfig(string $handlerName, array $handlerOptions): array

src/Handler/SyslogUdpHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ protected function sendToSocket(string $packet): bool
4040

4141
public function handle(ImmutableValue $record): void
4242
{
43+
if (!$this->isHandling($record)) {
44+
return;
45+
}
46+
4347
$message = $this->formatter->format($record);
4448
$packet = '<' . $this->getSyslogPriority($record->level) . '>' . $message;
4549

0 commit comments

Comments
 (0)