Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
OlzProcessor | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
__invoke | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
protectTokens | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Olz\Utils; |
4 | |
5 | use Monolog\Attribute\AsMonologProcessor; |
6 | use Monolog\LogRecord; |
7 | use Monolog\Processor\ProcessorInterface; |
8 | |
9 | #[AsMonologProcessor] |
10 | class OlzProcessor implements ProcessorInterface { |
11 | use WithUtilsTrait; |
12 | |
13 | public function __invoke(LogRecord $record): LogRecord { |
14 | if ($this->server()) { |
15 | $record->extra['url'] = $this->protectTokens($this->server()['REQUEST_URI'] ?? null); |
16 | $record->extra['referrer'] = $this->server()['HTTP_REFERER'] ?? null; |
17 | $record->extra['user_agent'] = $this->server()['HTTP_USER_AGENT'] ?? null; |
18 | } |
19 | $record->extra['user'] = $this->session()->get('user'); |
20 | $record->extra['auth_user'] = $this->session()->get('auth_user'); |
21 | if ($record->channel && $record->channel !== 'app') { |
22 | return $record; |
23 | } |
24 | $trace = debug_backtrace(); |
25 | $general_utils = new GeneralUtils(); |
26 | $trace_overview = $general_utils->getTraceOverview($trace); |
27 | return $record->with(channel: $trace_overview); |
28 | } |
29 | |
30 | protected function protectTokens(?string $unsanitized): ?string { |
31 | if (!$unsanitized) { |
32 | return $unsanitized; |
33 | } |
34 | return preg_replace('/(access\_token\=[a-zA-Z0-9\_\-\+\/]{3})[a-zA-Z0-9\_\-\+\/]*([a-zA-Z0-9\_\-\+\/]{3})/', '$1***$2', $unsanitized); |
35 | } |
36 | } |