forked from cloud-solutions-archive/zend-sentry
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModule.php
More file actions
240 lines (207 loc) · 8.47 KB
/
Copy pathModule.php
File metadata and controls
240 lines (207 loc) · 8.47 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* cloud solutions ZendSentry
*
* This source file is part of the cloud solutions ZendSentry package
*
* @package ZendSentry\Module
* @license New BSD License {@link /docs/LICENSE}
* @copyright Copyright (c) 2013, cloud solutions OÜ
*/
namespace ZendSentry;
use Zend\EventManager\EventManager;
use Zend\Mvc\MvcEvent;
use ZendSentry\Mvc\View\Http\ExceptionStrategy as SentryHttpStrategy;
use ZendSentry\Mvc\View\Console\ExceptionStrategy as SentryConsoleStrategy;
use Zend\Mvc\View\Http\ExceptionStrategy;
use Raven_Client as Raven;
use Zend\Log\Logger;
use Zend\Console\Console;
/**
* Class Module
*
* @package ZendSentry
*/
class Module
{
/**
* Translates Zend Framework log levels to Raven log levels.
*/
private $logLevels = array(
7 => Raven::DEBUG,
6 => Raven::INFO,
5 => Raven::INFO,
4 => Raven::WARNING,
3 => Raven::ERROR,
2 => Raven::FATAL,
1 => Raven::FATAL,
0 => Raven::FATAL,
);
/**
* @var Raven $ravenClient
*/
protected $ravenClient;
/**
* @var ZendSentry $zendSentry
*/
protected $zendSentry;
/**
* @var $config
*/
protected $config;
/**
* @var EventManager $eventManager
*/
protected $eventManager;
/**
* @param MvcEvent $event
*/
public function onBootstrap(MvcEvent $event)
{
// Setup RavenClient (provided by Sentry) and Sentry (provided by this module)
$this->config = $event->getApplication()->getServiceManager()->get('Config');
if (!$this->config['zend-sentry']['use-module']) {
return;
}
if (isset($this->config['zend-sentry']['raven-config']) && is_array($this->config['zend-sentry']['raven-config'])) {
$ravenConfig = $this->config['zend-sentry']['raven-config'];
} else {
$ravenConfig = array();
}
$sentryApiKey = $this->config['zend-sentry']['sentry-api-key'];
$ravenClass = 'Raven';
if (array_key_exists('raven-client', $this->config['zend-sentry'])) {
$ravenClass = $this->config['zend-sentry']['raven-client'];
}
$ravenClient = new $ravenClass($sentryApiKey, $ravenConfig);
// Register the RavenClient as a application wide service
$event->getApplication()->getServiceManager()->setService('raven', $ravenClient);
$this->ravenClient = $ravenClient;
$this->zendSentry = new ZendSentry($ravenClient);
// Get the eventManager and set it as a member for convenience
$this->eventManager = $event->getApplication()->getEventManager();
// If ZendSentry is configured to use the custom logger, attach the listener
if ($this->config['zend-sentry']['attach-log-listener']) {
$this->setupBasicLogging($event);
}
// If ZendSentry is configured to log exceptions, a few things need to be set up
if ($this->config['zend-sentry']['handle-exceptions']) {
$this->setupExceptionLogging($event);
}
// If ZendSentry is configured to log errors, register it as error handler
if ($this->config['zend-sentry']['handle-errors']) {
$this->zendSentry->registerErrorHandler(
$this->config['zend-sentry']['call-existing-error-handler'],
array_key_exists('error-reporting', $this->config['zend-sentry'])
? $this->config['zend-sentry']['error-reporting']
: -1
);
}
// If ZendSentry is configured to log shutdown errors, register it
if ($this->config['zend-sentry']['handle-shutdown-errors']) {
$this->zendSentry->registerShutdownFunction();
}
// If ZendSentry is configured to log Javascript errors, add needed scripts to the view
if ($this->config['zend-sentry']['handle-javascript-errors']) {
$this->setupJavascriptLogging($event);
}
}
/**
* @return array
*/
public function getAutoloaderConfig()
{
return array('Zend\Loader\StandardAutoloader' => array('namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
)));
}
/**
* @return mixed
*/
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
/**
* Gives us the possibility to write logs to Sentry from anywhere in the application
* Doesn't use the ZF compatible Log Writer because we want to return the Sentry event ID
* ZF Logging doesn't provide the possibility to return values from writers
*
* @param MvcEvent $event
*/
protected function setupBasicLogging(MvcEvent $event)
{
// Get the shared event manager and attach a logging listener for the log event on application level
$sharedManager = $this->eventManager->getSharedManager();
$raven = $this->ravenClient;
$sharedManager->attach('*', 'log', function($event) use ($raven) {
/** @var $event MvcEvent */
if (is_object($event->getTarget())) {
$target = get_class($event->getTarget());
} else {
$target = (string) $event->getTarget();
}
$message = $event->getParam('message', 'No message provided');
$priority = (int) $event->getParam('priority', Logger::INFO);
$message = sprintf('%s: %s', $target, $message);
$tags = $event->getParam('tags', array());
$eventID = $raven->captureMessage($message, array(), array('tags' => $tags, 'level' => $this->logLevels[$priority]));
return $eventID;
}, 2);
}
/**
* 1. Registers Sentry as exception handler
* 2. Replaces the default ExceptionStrategy so Exception that are caught by Zend Framework can still be logged
*
* @param MvcEvent $event
*/
protected function setupExceptionLogging(MvcEvent $event)
{
// Register Sentry as exception handler for exception that bubble up to the top
$this->zendSentry->registerExceptionHandler($this->config['zend-sentry']['call-existing-exception-handler']);
// Replace the default ExceptionStrategy with ZendSentry's strategy
/** @var $exceptionStrategy ExceptionStrategy */
$exceptionStrategy = $event->getApplication()->getServiceManager()->get('ViewManager')->getExceptionStrategy();
$exceptionStrategy->detach($this->eventManager);
// Check if script is running in console
$exceptionStrategy = Console::isConsole() ? (new SentryConsoleStrategy()) : (new SentryHttpStrategy());
$exceptionStrategy->attach($this->eventManager);
$exceptionStrategy->setDisplayExceptions($this->config['zend-sentry']['display-exceptions']);
$exceptionStrategy->setDefaultExceptionMessage($this->config['zend-sentry'][(PHP_SAPI == 'cli') ? 'default-exception-console-message' : 'default-exception-message']);
$ravenClient = $this->ravenClient;
// Attach an exception listener for the ZendSentry exception strategy, can be triggered from anywhere else too
$this->eventManager->getSharedManager()->attach('*', 'logException', function($event) use ($ravenClient) {
/** @var $event MvcEvent */
$exception = $event->getParam('exception');
$tags = $event->getParam('tags', array());
$eventID = $ravenClient->captureException($exception, array('tags' => $tags));
return $eventID;
});
}
/**
* Adds the necessary javascript, tries to prepend
*
* @param MvcEvent $event
*/
protected function setupJavascriptLogging(MvcEvent $event)
{
$viewHelper = $event->getApplication()->getServiceManager()->get('viewhelpermanager')->get('headscript');
$viewHelper->offsetSetFile(0, '//cdn.ravenjs.com/1.1.16/jquery,native/raven.min.js');
$publicApiKey = $this->convertKeyToPublic($this->config['zend-sentry']['sentry-api-key']);
$viewHelper->offsetSetScript(1, sprintf("Raven.config('%s').install()", $publicApiKey));
}
/**
* @param string $key
* @return string $publicKey
*/
private function convertKeyToPublic($key)
{
// Find private part
$start = strpos($key, ':', 6);
$end = strpos($key, '@');
$privatePart = substr($key, $start, $end - $start);
// Replace it with an empty string
$publicKey = str_replace($privatePart, '', $key);
return $publicKey;
}
}