-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSentryLogRoute.php
113 lines (100 loc) · 2.43 KB
/
SentryLogRoute.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
<?php
namespace Intersvyaz\YiiSentry;
use CLogger;
use CLogRoute;
use Raven_Client;
use Yii;
class SentryLogRoute extends CLogRoute
{
/**
* @var string Component ID of the sentry client that should be used to send the logs
*/
public $sentryComponent = 'sentry';
/**
* @see self::getStackTrace().
* @var string
*/
public $tracePattern = '/#(?<number>\d+) (?<file>[^(]+)\((?<line>\d+)\): (?<cls>[^-]+)(->|::)(?<func>[^\(]+)/m';
/**
* Raven_Client instance from SentryComponent->getRaven();
* @var Raven_Client
*/
protected $raven;
/**
* @inheritdoc
*/
protected function processLogs($logs)
{
if (count($logs) == 0) {
return;
}
if (!$raven = $this->getRaven()) {
return;
}
foreach ($logs as $log) {
list($message, $level, $category, $timestamp) = $log;
$title = preg_replace('#Stack trace:.+#s', '', $message); // remove stack trace from title
$raven->captureMessage(
$title,
array(
'extra' => array(
'category' => $category,
),
),
array(
'level' => $level,
'timestamp' => $timestamp,
),
$this->getStackTrace($message)
);
}
}
/**
* Parse yii stack trace for sentry.
*
* Example log string:
* #22 /var/www/example.is74.ru/vendor/yiisoft/yii/framework/web/CWebApplication.php(282): CController->run('index')
* @param string $log
* @return array|string
*/
protected function getStackTrace($log)
{
$stack = array();
if (strpos($log, 'Stack trace:') !== false) {
if (preg_match_all($this->tracePattern, $log, $m, PREG_SET_ORDER)) {
$stack = array();
foreach ($m as $row) {
$stack[] = array(
'file' => $row['file'],
'line' => $row['line'],
'function' => $row['func'],
'class' => $row['cls'],
);
}
}
}
return $stack;
}
/**
* Return Raven_Client instance or false if error.
* @return Raven_Client|bool
*/
protected function getRaven()
{
if (!isset($this->raven)) {
$this->raven = false;
if (!Yii::app()->hasComponent($this->sentryComponent)) {
Yii::log("'$this->sentryComponent' does not exist", CLogger::LEVEL_TRACE, __CLASS__);
} else {
/** @var SentryComponent $sentry */
$sentry = Yii::app()->{$this->sentryComponent};
if (!$sentry || !$sentry->getIsInitialized()) {
Yii::log("'$this->sentryComponent' not initialised", CLogger::LEVEL_TRACE, __CLASS__);
} else {
$this->raven = $sentry->getRaven();
}
}
}
return $this->raven;
}
}