-
Notifications
You must be signed in to change notification settings - Fork 611
/
MessageProcessor.php
58 lines (48 loc) · 1.61 KB
/
MessageProcessor.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
52
53
54
55
56
57
58
<?php
namespace craft\log;
use Illuminate\Support\Collection;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
/**
* Class MessageProcessor
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 4.0.0
*/
class MessageProcessor implements ProcessorInterface
{
public const DEFAULT_CATEGORY = 'application';
/**
* @inheritdoc
*/
public function __invoke(LogRecord $record): LogRecord
{
$record = Collection::make($record);
$record = $this->_extractCategory($record);
$record = $this->_filterEmptyContext($record, 'trace');
return new LogRecord(
datetime: $record->get('datetime'),
channel: $record->get('channel'),
level: $record->get('level'),
message: $record->get('message'),
context: $record->get('context'),
extra: $record->get('extra'),
formatted: $record->get('formatted'),
);
}
private function _extractCategory(Collection $record): Collection
{
$category = $record->pull('context.category');
$extra = Collection::make($record->get('extra'));
$extra->put('yii_category', $category ?? self::DEFAULT_CATEGORY);
$record->put('extra', $extra->all());
return $record;
}
private function _filterEmptyContext(Collection $record, string $key = null): Collection
{
$context = Collection::make($record->get('context'))
->reject(fn($v, $k) => ($key === null || $k === $key) && empty($v));
$record->put('context', $context->all());
return $record;
}
}