1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dziki\MonologSentryBundle\Handler; |
6
|
|
|
|
7
|
|
|
use Monolog\Handler\RavenHandler; |
8
|
|
|
|
9
|
|
|
class Raven extends RavenHandler |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* {@inheritdoc} |
13
|
|
|
*/ |
14
|
|
|
public function handleBatch(array $records): void |
15
|
|
|
{ |
16
|
|
|
$level = $this->level; |
17
|
|
|
|
18
|
|
|
// filter records based on their level |
19
|
|
|
$records = array_filter( |
20
|
|
|
$records, |
21
|
|
|
function ($record) use ($level) { |
22
|
|
|
return $record['level'] >= $level; |
23
|
|
|
} |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
if (!$records) { |
|
|
|
|
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// the record with the highest severity is the "main" one |
31
|
|
|
$record = array_reduce( |
32
|
|
|
$records, |
33
|
|
|
function ($highest, $record) { |
34
|
|
|
if ($record['level'] > $highest['level']) { |
35
|
|
|
return $record; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $highest; |
39
|
|
|
} |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
// the other ones are added as a context item |
43
|
|
|
$logs = []; |
44
|
|
|
foreach ($records as $r) { |
45
|
|
|
$log = $this->processRecord($r); |
46
|
|
|
|
47
|
|
|
$crumb = [ |
48
|
|
|
'category' => $log['channel'], |
49
|
|
|
'message' => $log['message'], |
50
|
|
|
'level' => strtolower($log['level_name']), |
51
|
|
|
'timestamp' => (float)$log['datetime']->format('U.u'), |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
if (array_key_exists('context', $log)) { |
55
|
|
|
if ($log['channel'] === 'request' && array_key_exists('route_parameters', $log['context'])) { |
56
|
|
|
$crumb['data']['route'] = $log['context']['route_parameters']['_route']; |
57
|
|
|
$crumb['data']['controller'] = $log['context']['route_parameters']['_controller']; |
58
|
|
|
$crumb['data']['uri'] = $log['context']['request_uri']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($log['channel'] === 'security' && array_key_exists('user', $log['context'])) { |
62
|
|
|
$crumb['data']['user'] = $log['context']['user']['username']; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->ravenClient->breadcrumbs->record($crumb); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($logs) { |
|
|
|
|
70
|
|
|
$record['context']['logs'] = (string)$this->getBatchFormatter()->formatBatch($logs); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->handle($record); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.