-
Notifications
You must be signed in to change notification settings - Fork 0
/
CorrelationIdAppendingProcessor.php
51 lines (43 loc) · 1.35 KB
/
CorrelationIdAppendingProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);
namespace Profesia\Monolog\Processor;
use Monolog\Logger;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
use Profesia\CorrelationId\Resolver\CorrelationIdResolverInterface;
class CorrelationIdAppendingProcessor implements ProcessorInterface
{
private CorrelationIdResolverInterface $resolver;
private string $storeKey;
public function __construct(
CorrelationIdResolverInterface $resolver,
?string $storeKey = 'correlation_id'
)
{
$this->resolver = $resolver;
$this->storeKey = $storeKey;
}
public function __invoke($record)
{
$isMonologVersion3 = (Logger::API >= 3 && $record instanceof LogRecord);
if ($isMonologVersion3 === true) {
$recordData = $record->toArray();
} else {
$recordData = $record;
}
$recordData['extra'][$this->storeKey] = $this->resolver->resolve();
if ($isMonologVersion3 === true) {
return new LogRecord(
$record->datetime,
$record->channel,
$record->level,
$record->message,
$record->context,
$recordData['extra'],
$record->formatted
);
} else {
return $recordData;
}
}
}