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