1 | <?php |
||
28 | class Module |
||
29 | { |
||
30 | /** |
||
31 | * Translates Zend Framework log levels to Raven log levels. |
||
32 | */ |
||
33 | private $logLevels = array( |
||
34 | 7 => Raven::DEBUG, |
||
35 | 6 => Raven::INFO, |
||
36 | 5 => Raven::INFO, |
||
37 | 4 => Raven::WARNING, |
||
38 | 3 => Raven::ERROR, |
||
39 | 2 => Raven::FATAL, |
||
40 | 1 => Raven::FATAL, |
||
41 | 0 => Raven::FATAL, |
||
42 | ); |
||
43 | |||
44 | /** |
||
45 | * @var Raven $ravenClient |
||
46 | */ |
||
47 | protected $ravenClient; |
||
48 | |||
49 | /** |
||
50 | * @var ZendSentry $zendSentry |
||
51 | */ |
||
52 | protected $zendSentry; |
||
53 | |||
54 | /** |
||
55 | * @var $config |
||
56 | */ |
||
57 | protected $config; |
||
58 | |||
59 | /** |
||
60 | * @var EventManager $eventManager |
||
61 | */ |
||
62 | protected $eventManager; |
||
63 | |||
64 | /** |
||
65 | * @param MvcEvent $event |
||
66 | */ |
||
67 | public function onBootstrap(MvcEvent $event) |
||
119 | |||
120 | /** |
||
121 | * @return array |
||
122 | */ |
||
123 | public function getAutoloaderConfig() |
||
129 | |||
130 | /** |
||
131 | * @return mixed |
||
132 | */ |
||
133 | public function getConfig() |
||
137 | |||
138 | /** |
||
139 | * Gives us the possibility to write logs to Sentry from anywhere in the application |
||
140 | * Doesn't use the ZF compatible Log Writer because we want to return the Sentry event ID |
||
141 | * ZF Logging doesn't provide the possibility to return values from writers |
||
142 | * |
||
143 | * @param MvcEvent $event |
||
144 | */ |
||
145 | protected function setupBasicLogging(MvcEvent $event) |
||
1 ignored issue
–
show
|
|||
146 | { |
||
147 | // Get the shared event manager and attach a logging listener for the log event on application level |
||
148 | $sharedManager = $this->eventManager->getSharedManager(); |
||
149 | $raven = $this->ravenClient; |
||
150 | $logLevels = $this->logLevels; |
||
151 | |||
152 | $sharedManager->attach('*', 'log', function($event) use ($raven, $logLevels) { |
||
153 | /** @var $event MvcEvent */ |
||
154 | if (is_object($event->getTarget())) { |
||
155 | $target = get_class($event->getTarget()); |
||
156 | } else { |
||
157 | $target = (string) $event->getTarget(); |
||
158 | } |
||
159 | $message = $event->getParam('message', 'No message provided'); |
||
160 | $priority = (int) $event->getParam('priority', Logger::INFO); |
||
161 | $message = sprintf('%s: %s', $target, $message); |
||
162 | $tags = $event->getParam('tags', array()); |
||
163 | $eventID = $raven->captureMessage($message, array(), array('tags' => $tags, 'level' => $logLevels[$priority])); |
||
164 | |||
165 | return $eventID; |
||
166 | }, 2); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * 1. Registers Sentry as exception handler |
||
171 | * 2. Replaces the default ExceptionStrategy so Exception that are caught by Zend Framework can still be logged |
||
172 | * |
||
173 | * @param MvcEvent $event |
||
174 | */ |
||
175 | protected function setupExceptionLogging(MvcEvent $event) |
||
176 | { |
||
177 | // Register Sentry as exception handler for exception that bubble up to the top |
||
178 | $this->zendSentry->registerExceptionHandler($this->config['zend-sentry']['call-existing-exception-handler']); |
||
179 | |||
180 | // Replace the default ExceptionStrategy with ZendSentry's strategy |
||
181 | if ($event->getApplication()->getServiceManager()->has('HttpExceptionStrategy')) { |
||
182 | /** @var $exceptionStrategy ExceptionStrategy */ |
||
183 | $exceptionStrategy = $event->getApplication()->getServiceManager()->get('HttpExceptionStrategy'); |
||
184 | $exceptionStrategy->detach($this->eventManager); |
||
185 | } |
||
186 | |||
187 | // Check if script is running in console |
||
188 | $exceptionStrategy = (PHP_SAPI == 'cli') ? (new SentryConsoleStrategy()) : (new SentryHttpStrategy()); |
||
189 | $exceptionStrategy->attach($this->eventManager); |
||
190 | $exceptionStrategy->setDisplayExceptions($this->config['zend-sentry']['display-exceptions']); |
||
191 | $exceptionStrategy->setDefaultExceptionMessage($this->config['zend-sentry'][(PHP_SAPI == 'cli') ? 'default-exception-console-message' : 'default-exception-message']); |
||
192 | |||
193 | $ravenClient = $this->ravenClient; |
||
194 | |||
195 | // Attach an exception listener for the ZendSentry exception strategy, can be triggered from anywhere else too |
||
196 | $this->eventManager->getSharedManager()->attach('*', 'logException', function($event) use ($ravenClient) { |
||
197 | /** @var $event MvcEvent */ |
||
198 | $exception = $event->getParam('exception'); |
||
199 | $tags = $event->getParam('tags', array()); |
||
200 | $eventID = $ravenClient->captureException($exception, array('tags' => $tags)); |
||
201 | return $eventID; |
||
202 | }); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Adds the necessary javascript, tries to prepend |
||
207 | * |
||
208 | * @param MvcEvent $event |
||
209 | */ |
||
210 | protected function setupJavascriptLogging(MvcEvent $event) |
||
217 | |||
218 | /** |
||
219 | * @param string $key |
||
220 | * @return string $publicKey |
||
221 | */ |
||
222 | private function convertKeyToPublic($key) |
||
234 | } |
||
1 ignored issue
–
show
|
|||
235 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.