This repository was archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathESentry.php
154 lines (137 loc) · 3.91 KB
/
ESentry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* ESentry class file.
*
* @package ESentry
* @version 1.0
* @author dotzero <[email protected]>
* @link http://www.dotzero.ru/
* @link https://github.com/dotzero/yii-sentry
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* YiiSentry is an extension for the Yii PHP framework that allows developers to push messages and logs
* to the Sentry (https://getsentry.com/) service or your own Sentry server.
*
* Requirements:
* - Yii Framework 1.1.14 or above
*
* Installation:
*
* - Add vendor path to your configuration file, attach component and set properties:
*
* 'aliases' => array(
* ...
* 'vendor' => realpath(__DIR__ . '/../../vendor'),
* ),
* 'components' => array(
* ...
* 'sentry' => array(
* 'class' => 'vendor.dotzero.yii-sentry.ESentry',
* 'sentryDir' => 'vendor.sentry.sentry', // Path alias of the sentry-php directory (optional)
* 'enabled' => true, // Enabled or disabled extension (optional)
* 'dsn' => '[YOUR_DSN_FROM_SENTRY_SERVER]',
* // Raven PHP options (https://github.com/getsentry/sentry-php#configuration)
* 'options' => array(
* 'site' => 'example.com',
* 'tags' => array(
* 'php_version' => phpversion(),
* ),
* ),
* ),
* ),
*
* - Add the following to your config file log section to enable ESentryLogRoute:
*
* 'routes' => array(
* ...
* array(
* 'class' => 'vendor.dotzero.yii-sentry.ESentryLogRoute',
* 'levels' => 'error, warning',
* ),
* ),
*/
class ESentry extends CApplicationComponent
{
/**
* @var string Path alias of the directory where the Raven PHP can be found.
*/
public $sentryDir = 'vendor.sentry.sentry';
/**
* @var bool Enabled or disabled extension
*/
public $enabled = true;
/**
* @var string Sentry DSN value
*/
public $dsn = null;
/**
* @var array Raven PHP options
* @see https://github.com/getsentry/sentry-php#configuration
*/
public $options = array();
/**
* @var Raven_Client Raven instance
*/
private $raven = null;
/**
* return Raven_Client
*/
public function getRavenClient()
{
if (!is_array($this->options)) {
$this->options = array();
}
if ($this->raven === null) {
$this->raven = new Raven_Client($this->dsn, $this->options);
}
return $this->raven;
}
/**
* Initializes the application component.
*/
public function init()
{
if (!$this->enabled) {
return false;
}
parent::init();
if (!class_exists('Raven_Autoloader', false)) {
Yii::import($this->sentryDir . '.*');
require_once 'lib/Raven/Autoloader.php';
Yii::registerAutoloader(array('Raven_Autoloader', 'autoload'));
}
}
/**
* Capture a message to Sentry
*
* @param string $message Message string
* @param string|array $options Additional data with a message
* @return mixed Event ID
*/
public function captureMessage($message, $options = array())
{
$client = $this->getRavenClient();
if ($options) {
$client->extra_context($options);
}
return $client->getIdent($client->captureMessage($message));
}
/**
* Capture an exception to Sentry
*
* @param $exception Exception
* @param string|array $options Additional data with a exception
* @return mixed
*/
public function captureException($exception, $options = array())
{
$client = $this->getRavenClient();
if ($options) {
$client->extra_context($options);
}
return $client->getIdent($client->captureException($exception));
}
}