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 pathESentryLogRoute.php
136 lines (128 loc) · 4.06 KB
/
ESentryLogRoute.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
<?php
/**
* ESentryLogRoute 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 ESentryLogRoute extends CLogRoute
{
/**
* @var string Component ID of the ESentry
*/
public $sentryComponent = 'sentry';
/**
* Processes log messages and sends them to specific destination.
* Derived child classes must implement this method.
*
* @param array $logs list of messages. Each array element represents one message
* with the following structure:
* array(
* [0] => message (string)
* [1] => level (string)
* [2] => category (string)
* [3] => timestamp (float, obtained by microtime(true));
* @return mixed
*/
protected function processLogs($logs)
{
if (count($logs) == 0) {
return false;
}
$client = $this->getRavenClient();
if ($client) {
foreach ($logs AS $log) {
$strings = explode(PHP_EOL, $log[0]);
$reg = '/(\#[0-9]+ [\/a-zA-Z0-9.]+?)(\([0-9]+\):\ ?)([^\(]*)(\(.*\))/';
$replace = '$1$2$3()';
$result = preg_replace($reg, $replace, $strings);
$trace = implode(PHP_EOL, $result);
$client->captureMessage(
$trace,
array(
'level' => $log[1],
'category' => $log[2],
'timestamp' => $log[3],
)
);
}
}
}
/**
* Returns the object of Raven_Client class.
* Also ensure ESentry that application component exists and is initialised.
*
* @return bool|Raven_Client The object of Raven_Client class, or false if the
* client is not available
*/
private function getRavenClient()
{
if (Yii::app()->hasComponent($this->sentryComponent)) {
$sentry = Yii::app()->getComponent($this->sentryComponent);
if ($sentry->getIsInitialized()) {
return $sentry;
} else {
Yii::log(
$this->sentryComponent . ' is not initialised',
CLogger::LEVEL_TRACE,
'application.ESentryLogRoute'
);
}
} else {
Yii::log(
$this->sentryComponent . ' does not exist',
CLogger::LEVEL_TRACE,
'application.ESentryLogRoute'
);
}
return false;
}
}