1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class: RavenClient. |
5
|
|
|
* |
6
|
|
|
* @author Russell Michell 2017 <[email protected]> |
7
|
|
|
* @package phptek/sentry |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace SilverStripeSentry\Adaptor; |
11
|
|
|
|
12
|
|
|
use SilverStripeSentry\Adaptor\SentryClientAdaptor; |
13
|
|
|
use SilverStripeSentry\Exception\SentryLogWriterException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The RavenClient class simply acts as a bridge between the Raven PHP SDK and |
17
|
|
|
* the SentryLogWriter class itself. Any {@link SentryClientAdaptor} subclass |
18
|
|
|
* should be able to be swapped-out and used at any point. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
class RavenClient extends SentryClientAdaptor |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* It's an ERROR unless proven otherwise! |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
* @config |
29
|
|
|
*/ |
30
|
|
|
private static $default_error_level = 'ERROR'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Raven_Client |
34
|
|
|
*/ |
35
|
|
|
protected $client; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* A mapping of log-level values between Zend_Log => Raven_Client |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $logLevels = [ |
43
|
|
|
'NOTICE' => \Raven_Client::INFO, |
44
|
|
|
'WARN' => \Raven_Client::WARNING, |
45
|
|
|
'ERR' => \Raven_Client::ERROR, |
46
|
|
|
'EMERG' => \Raven_Client::FATAL |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws SentryLogWriterException |
51
|
|
|
* @return void |
|
|
|
|
52
|
|
|
*/ |
53
|
|
|
public function __construct() |
54
|
|
|
{ |
55
|
|
|
if (!$dsn = $this->getOpts('dsn')) { |
56
|
|
|
$msg = sprintf("%s requires a DSN string to be set in config.", __CLASS__); |
57
|
|
|
throw new SentryLogWriterException($msg); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->client = new \Raven_Client($dsn); |
|
|
|
|
61
|
|
|
|
62
|
|
|
// Installs all available PHP error handlers when set |
63
|
|
|
if ($this->config()->install === true) { |
64
|
|
|
$this->client->install(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Used in unit tests. |
70
|
|
|
* |
71
|
|
|
* @return Raven_Client |
72
|
|
|
*/ |
73
|
|
|
public function getSDK() |
74
|
|
|
{ |
75
|
|
|
return $this->client; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function setData($field, $data) |
82
|
|
|
{ |
83
|
|
|
switch($field) { |
84
|
|
|
case 'env': |
85
|
|
|
$this->client->setEnvironment($data); |
86
|
|
|
break; |
87
|
|
|
case 'tags': |
88
|
|
|
$this->client->tags_context($data); |
89
|
|
|
break; |
90
|
|
|
case 'user': |
91
|
|
|
$this->client->user_context($data); |
92
|
|
|
break; |
93
|
|
|
case 'extra': |
94
|
|
|
$this->client->extra_context($data); |
95
|
|
|
break; |
96
|
|
|
default: |
97
|
|
|
$msg = sprintf('Unknown field %s passed to %s.', $field, __FUNCTION__); |
98
|
|
|
throw new SentryLogWriterException($msg); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
105
|
|
|
public function getLevel($level) |
106
|
|
|
{ |
107
|
|
|
return isset($this->client->logLevels[$level]) ? |
108
|
|
|
$this->client->logLevels[$level] : |
109
|
|
|
$this->client->logLevels[self::$default_error_level]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
115
|
|
|
public function send($message, $extras = [], $data, $trace) |
116
|
|
|
{ |
117
|
|
|
// Raven_Client::captureMessage() returns an ID to identify each message |
118
|
|
|
$eventId = $this->client->captureMessage($message, $extras, $data, $trace); |
119
|
|
|
|
120
|
|
|
return $eventId ?: false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
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.