Repository navigation
Expand file tree
/
Copy pathAbstractProcessor.php
More file actions
113 lines (96 loc) · 3.11 KB
/
Copy pathAbstractProcessor.php
File metadata and controls
113 lines (96 loc) · 3.11 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\ProcessBundle\Logger;
use CleverAge\ProcessBundle\Configuration\TaskConfiguration;
use CleverAge\ProcessBundle\Manager\ProcessManager;
use CleverAge\ProcessBundle\Model\ProcessHistory;
use Monolog\LogRecord;
/**
* @internal
*/
class AbstractProcessor
{
public function __construct(
protected ProcessManager $processManager,
) {
}
public function __invoke(LogRecord $record): LogRecord
{
if ([] !== $record->context) {
$context = $this->normalizeRecordData($record->context);
$record = new LogRecord(
$record->datetime,
$record->channel,
$record->level,
$record->message,
$context,
$record->extra,
$record->formatted
);
}
$recordExtra = $record->extra;
$this->addProcessInfoToRecord($recordExtra);
$record->extra = $recordExtra;
return $record;
}
/**
* @param array<string, mixed> $record
*
* @return array<string, mixed>
*/
protected function normalizeRecordData(array $record): array
{
$newRecord = [];
foreach ($record as $recordName => $recordValue) {
$this->addToRecord($newRecord, $recordName, $recordValue);
}
return $newRecord;
}
/**
* @param array<string, mixed> $record
*/
protected function addProcessInfoToRecord(array &$record): void
{
$processHistory = $this->processManager->getProcessHistory();
if (!$processHistory instanceof ProcessHistory) {
return;
}
$this->addToRecord($record, 'process_id', $processHistory->getId());
$this->addToRecord($record, 'process_code', $processHistory->getProcessCode());
$this->addToRecord($record, 'process_context', $processHistory->getContext());
}
/**
* @param array<string, mixed> $record
*/
protected function addTaskInfoToRecord(array &$record): void
{
$taskConfiguration = $this->processManager->getTaskConfiguration();
if (!$taskConfiguration instanceof TaskConfiguration) {
return;
}
$this->addToRecord($record, 'task_code', $taskConfiguration->getCode());
$this->addToRecord($record, 'task_service', $taskConfiguration->getServiceReference());
$state = $taskConfiguration->getState();
if ($state->hasErrorOutput()) {
$this->addToRecord($record, 'error', $state->getErrorOutput());
}
if ($state->getException() instanceof \Throwable) {
$this->addToRecord($record, 'exception', $state->getException());
}
}
/**
* @param array<string, mixed> $record
*/
protected function addToRecord(array &$record, string $name, mixed $data): void
{
$record[$name] = $data;
}
}