1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Monolog package. |
5
|
|
|
* |
6
|
|
|
* (c) Jordi Boggiano <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace PhpTek\Sentry\Monolog\Handler; |
15
|
|
|
|
16
|
|
|
use Monolog\Handler\RavenHandler, |
17
|
|
|
Monolog\Logger, |
18
|
|
|
Raven_Client; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Subclasses RavenHandler purely to overload its `write()` method. |
22
|
|
|
*/ |
23
|
|
|
class SentryRavenHandler extends RavenHandler |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Translates Monolog log levels to Raven log levels. |
28
|
|
|
* |
29
|
|
|
* Reproduced here because it's a private static. |
30
|
|
|
*/ |
31
|
|
|
private $logLevels = array( |
|
|
|
|
32
|
|
|
Logger::DEBUG => Raven_Client::DEBUG, |
33
|
|
|
Logger::INFO => Raven_Client::INFO, |
34
|
|
|
Logger::NOTICE => Raven_Client::INFO, |
35
|
|
|
Logger::WARNING => Raven_Client::WARNING, |
36
|
|
|
Logger::ERROR => Raven_Client::ERROR, |
37
|
|
|
Logger::CRITICAL => Raven_Client::FATAL, |
38
|
|
|
Logger::ALERT => Raven_Client::FATAL, |
39
|
|
|
Logger::EMERGENCY => Raven_Client::FATAL, |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Overloads RavenHandler::write() to allow a stacktrace to be passed |
44
|
|
|
* into Sentry. Otherwise, this method is identical to the one included |
45
|
|
|
* in the Monolog package. |
46
|
|
|
* |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
protected function write(array $record) |
50
|
|
|
{ |
51
|
|
|
$previousUserContext = false; |
52
|
|
|
$options = []; |
53
|
|
|
$options['level'] = $this->logLevels[$record['level']]; |
54
|
|
|
$options['tags'] = []; |
55
|
|
|
|
56
|
|
|
if (!empty($record['extra']['tags'])) { |
57
|
|
|
$options['tags'] = array_merge($options['tags'], $record['extra']['tags']); |
58
|
|
|
unset($record['extra']['tags']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
if (!empty($record['context']['tags'])) { |
|
|
|
|
62
|
|
|
$options['tags'] = array_merge($options['tags'], $record['context']['tags']); |
63
|
|
|
unset($record['context']['tags']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!empty($record['context']['fingerprint'])) { |
67
|
|
|
$options['fingerprint'] = $record['context']['fingerprint']; |
68
|
|
|
unset($record['context']['fingerprint']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
if (!empty($record['context']['logger'])) { |
|
|
|
|
72
|
|
|
$options['logger'] = $record['context']['logger']; |
73
|
|
|
unset($record['context']['logger']); |
74
|
|
|
} else { |
75
|
|
|
$options['logger'] = $record['channel']; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
foreach ($this->getExtraParameters() as $key) { |
79
|
|
|
foreach (['extra', 'context'] as $source) { |
80
|
|
|
if (!empty($record[$source][$key])) { |
81
|
|
|
$options[$key] = $record[$source][$key]; |
82
|
|
|
|
83
|
|
|
unset($record[$source][$key]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!empty($record['context'])) { |
89
|
|
|
$options['extra']['context'] = $record['context']; |
90
|
|
|
|
91
|
|
|
if (!empty($record['context']['user'])) { |
92
|
|
|
$previousUserContext = $this->ravenClient->context->user; |
93
|
|
|
$this->ravenClient->user_context($record['context']['user']); |
94
|
|
|
unset($options['extra']['context']['user']); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!empty($record['extra'])) { |
99
|
|
|
$options['extra']['extra'] = $record['extra']; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// New for phptek/sentry |
103
|
|
|
$stack = false; |
104
|
|
|
if (!empty($record['stack'])) { |
105
|
|
|
$stack = (bool) $record['stack']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (!empty($this->release) && !isset($options['release'])) { |
|
|
|
|
109
|
|
|
$options['release'] = $this->release; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (isset($record['context']['exception']) && ($record['context']['exception'] instanceof \Exception || (PHP_VERSION_ID >= 70000 && $record['context']['exception'] instanceof \Throwable))) { |
113
|
|
|
$options['extra']['message'] = $record['formatted']; |
114
|
|
|
$this->ravenClient->captureException($record['context']['exception'], $options); |
115
|
|
|
} else { |
116
|
|
|
$this->ravenClient->captureMessage($record['formatted'], [], $options, $stack); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($previousUserContext !== false) { |
120
|
|
|
$this->ravenClient->user_context($previousUserContext); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|