1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class: SentryHandler. |
5
|
|
|
* |
6
|
|
|
* @author Russell Michell 2017-2019 <[email protected]> |
7
|
|
|
* @package phptek/sentry |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace PhpTek\Sentry\Handler; |
11
|
|
|
|
12
|
|
|
use Monolog\Handler\AbstractProcessingHandler; |
13
|
|
|
use Monolog\Logger; |
14
|
|
|
use Sentry\Severity; |
15
|
|
|
use SilverStripe\Core\Injector\Injectable; |
16
|
|
|
use PhpTek\Sentry\Log\SentryLogger; |
17
|
|
|
use PhpTek\Sentry\Adaptor\SentryAdaptor; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Monolog handler to send messages to a Sentry (https://github.com/getsentry/sentry) server |
21
|
|
|
* using sentry-php (https://github.com/getsentry/sentry-php). |
22
|
|
|
*/ |
23
|
|
|
class SentryHandler extends AbstractProcessingHandler |
24
|
|
|
{ |
25
|
|
|
use Injectable; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param int $level |
29
|
|
|
* @param boolean $bubble |
30
|
|
|
* @param array $extras |
31
|
|
|
* @return void |
|
|
|
|
32
|
|
|
*/ |
33
|
|
|
public function __construct(int $level = Logger::DEBUG, bool $bubble = true, array $extras = []) |
34
|
|
|
{ |
35
|
|
|
// Returns an instance of {@link SentryLogger} |
36
|
|
|
$logger = SentryLogger::factory($extras); |
37
|
|
|
$this->client = $logger->getAdaptor(); |
|
|
|
|
38
|
|
|
|
39
|
|
|
parent::__construct($level, $bubble); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* write() forms the entry point into the physical sending of the error. The |
44
|
|
|
* sending itself is done by the current adaptor's `send()` method. |
45
|
|
|
* |
46
|
|
|
* @param array $record An array of error-context metadata with the following |
47
|
|
|
* available keys: |
48
|
|
|
* |
49
|
|
|
* - message |
50
|
|
|
* - context |
51
|
|
|
* - level |
52
|
|
|
* - level_name |
53
|
|
|
* - channel |
54
|
|
|
* - datetime |
55
|
|
|
* - extra |
56
|
|
|
* - formatted |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
protected function write(array $record) : void |
61
|
|
|
{ |
62
|
|
|
$record = array_merge($record, [ |
63
|
|
|
'timestamp' => $record['datetime']->getTimestamp(), |
64
|
|
|
'stacktrace' => SentryLogger::backtrace($record), |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
if ( |
68
|
|
|
isset($record['context']['exception']) && |
69
|
|
|
$record['context']['exception'] instanceof \Throwable |
70
|
|
|
) { |
71
|
|
|
$this->client->getSDK()->captureException($record['context']['exception']); |
72
|
|
|
} else { |
73
|
|
|
$this->client->getSDK()->captureMessage($record['formatted'], new Severity(strtolower($record['level_name']))); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return SentryAdaptor |
79
|
|
|
*/ |
80
|
|
|
public function getClient() : SentryAdaptor |
81
|
|
|
{ |
82
|
|
|
return $this->client; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.