| 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) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | public function getAutoloaderConfig() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return mixed |
||
| 133 | */ |
||
| 134 | public function getConfig() |
||
| 138 | /** @noinspection PhpUnusedParameterInspection */ |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Gives us the possibility to write logs to Sentry from anywhere in the application |
||
| 142 | * Doesn't use the ZF compatible Log Writer because we want to return the Sentry event ID |
||
| 143 | * ZF Logging doesn't provide the possibility to return values from writers |
||
| 144 | * |
||
| 145 | * @param MvcEvent $event |
||
| 146 | */ |
||
| 147 | protected function setupBasicLogging(MvcEvent $event) |
||
| 148 | { |
||
| 149 | // Suppress inspection |
||
| 150 | unset($event); |
||
| 151 | // Get the shared event manager and attach a logging listener for the log event on application level |
||
| 152 | $sharedManager = $this->eventManager->getSharedManager(); |
||
| 153 | $raven = $this->ravenClient; |
||
| 154 | $logLevels = $this->logLevels; |
||
| 155 | |||
| 156 | $sharedManager->attach('*', 'log', function($event) use ($raven, $logLevels) { |
||
| 157 | /** @var $event MvcEvent */ |
||
| 158 | if (is_object($event->getTarget())) { |
||
| 159 | $target = get_class($event->getTarget()); |
||
| 160 | } else { |
||
| 161 | $target = (string) $event->getTarget(); |
||
| 162 | } |
||
| 163 | $message = $event->getParam('message', 'No message provided'); |
||
| 164 | $priority = (int) $event->getParam('priority', Logger::INFO); |
||
| 165 | $message = sprintf('%s: %s', $target, $message); |
||
| 166 | $tags = $event->getParam('tags', array()); |
||
| 167 | $eventID = $raven->captureMessage($message, array(), array('tags' => $tags, 'level' => $logLevels[$priority])); |
||
| 168 | |||
| 169 | return $eventID; |
||
| 170 | }, 2); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * 1. Registers Sentry as exception handler |
||
| 175 | * 2. Replaces the default ExceptionStrategy so Exception that are caught by Zend Framework can still be logged |
||
| 176 | * |
||
| 177 | * @param MvcEvent $event |
||
| 178 | */ |
||
| 179 | protected function setupExceptionLogging(MvcEvent $event) |
||
| 180 | { |
||
| 181 | // Register Sentry as exception handler for exception that bubble up to the top |
||
| 182 | $this->zendSentry->registerExceptionHandler($this->config['zend-sentry']['call-existing-exception-handler']); |
||
| 183 | |||
| 184 | // Replace the default ExceptionStrategy with ZendSentry's strategy |
||
| 185 | if ($event->getApplication()->getServiceManager()->has('HttpExceptionStrategy')) { |
||
| 186 | /** @var $exceptionStrategy ExceptionStrategy */ |
||
| 187 | $exceptionStrategy = $event->getApplication()->getServiceManager()->get('HttpExceptionStrategy'); |
||
| 188 | $exceptionStrategy->detach($this->eventManager); |
||
| 189 | } |
||
| 190 | |||
| 191 | // Check if script is running in console |
||
| 192 | $exceptionStrategy = (PHP_SAPI == 'cli') ? (new SentryConsoleStrategy()) : (new SentryHttpStrategy()); |
||
| 193 | $exceptionStrategy->attach($this->eventManager); |
||
| 194 | $exceptionStrategy->setDisplayExceptions($this->config['zend-sentry']['display-exceptions']); |
||
| 195 | $exceptionStrategy->setDefaultExceptionMessage($this->config['zend-sentry'][(PHP_SAPI == 'cli') ? 'default-exception-console-message' : 'default-exception-message']); |
||
| 196 | |||
| 197 | $ravenClient = $this->ravenClient; |
||
| 198 | |||
| 199 | // Attach an exception listener for the ZendSentry exception strategy, can be triggered from anywhere else too |
||
| 200 | $this->eventManager->getSharedManager()->attach('*', 'logException', function($event) use ($ravenClient) { |
||
| 201 | /** @var $event MvcEvent */ |
||
| 202 | $exception = $event->getParam('exception'); |
||
| 203 | $tags = $event->getParam('tags', array()); |
||
| 204 | $eventID = $ravenClient->captureException($exception, array('tags' => $tags)); |
||
| 205 | return $eventID; |
||
| 206 | }); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Adds the necessary javascript, tries to prepend |
||
| 211 | * |
||
| 212 | * @param MvcEvent $event |
||
| 213 | */ |
||
| 214 | protected function setupJavascriptLogging(MvcEvent $event) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $key |
||
| 226 | * @return string $publicKey |
||
| 227 | */ |
||
| 228 | private function convertKeyToPublic($key) |
||
| 240 | } |
||
|
1 ignored issue
–
show
|
|||
| 241 |
This check marks files that end in a newline character, i.e. an empy line.