|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of a FireGento e.V. module. |
| 4 | + * This FireGento e.V. module is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU General Public License version 3 as |
| 6 | + * published by the Free Software Foundation. |
| 7 | + * This script is distributed in the hope that it will be useful, but WITHOUT |
| 8 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 9 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 10 | + * |
| 11 | + * PHP version 5 |
| 12 | + * |
| 13 | + * @category FireGento |
| 14 | + * @package FireGento_Logger |
| 15 | + * @author FireGento Team <team@firegento.com> |
| 16 | + * @copyright 2013 FireGento Team (http://www.firegento.com) |
| 17 | + * @license http://opensource.org/licenses/gpl-3.0 GNU General Public License, version 3 (GPLv3) |
| 18 | + */ |
| 19 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * Class FireGento_Logger_Model_Airbrake |
| 23 | + * |
| 24 | + * This file was ported from Elgentos_CodebaseExceptions_Helper_Data |
| 25 | + * into this logger module. |
| 26 | + * |
| 27 | + * https://github.com/airbrake/Airbrake-Magento/blob/master/app/code/community/Elgentos/CodebaseExceptions/Helper/Data.php |
| 28 | + * |
| 29 | + */ |
| 30 | + |
| 31 | + |
| 32 | +require_once Mage::getBaseDir('lib') . DS . 'Airbrake' . DS . 'Client.php'; |
| 33 | +require_once Mage::getBaseDir('lib') . DS . 'Airbrake' . DS . 'Configuration.php'; |
| 34 | + |
| 35 | +class FireGento_Logger_Model_Airbrake extends Zend_Log_Writer_Abstract |
| 36 | +{ |
| 37 | + |
| 38 | + public function __construct() |
| 39 | + { |
| 40 | + $apiKey = Mage::getStoreConfig('logger/airbrake/apikey'); |
| 41 | + |
| 42 | + if ($this->isDisabled()) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + $options = array(); |
| 48 | + // REQUEST_URI is not available in the CLI context |
| 49 | + if (isset($_SERVER['REQUEST_URI'])) { |
| 50 | + $requestUri = explode("/", $_SERVER['REQUEST_URI']); |
| 51 | + $options['action'] = array_pop($requestUri); |
| 52 | + $options['component'] = implode('/', array_slice($requestUri, -2)); |
| 53 | + } else { |
| 54 | + $options['action'] = $_SERVER['PHP_SELF']; |
| 55 | + $options['component'] = $_SERVER['PHP_SELF']; |
| 56 | + } |
| 57 | + |
| 58 | + $projectRoot = explode('/', $_SERVER['PHP_SELF']); |
| 59 | + array_pop($projectRoot); |
| 60 | + $options['projectRoot'] = implode('/', $projectRoot) . '/'; |
| 61 | + $options['host'] = Mage::getStoreConfig('logger/airbrake/host'); |
| 62 | + $options['secure'] = Mage::getStoreConfig('logger/airbrake/secure'); |
| 63 | + $options['environmentName'] = Mage::getStoreConfig('logger/airbrake/environment'); |
| 64 | + $options['timeout'] = Mage::getStoreConfig('logger/airbrake/timeout'); |
| 65 | + $config = new Airbrake\Configuration($apiKey, $options); |
| 66 | + $this->client = new Airbrake\Client($config); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + /** |
| 71 | + * Write a message to the log. |
| 72 | + * |
| 73 | + * @param array $event event data |
| 74 | + * @return void |
| 75 | + * @throws Zend_Log_Exception |
| 76 | + */ |
| 77 | + protected function _write($event) |
| 78 | + { |
| 79 | + $this->sendToAirbrake($event['message'], 4); |
| 80 | + } |
| 81 | + |
| 82 | + protected function isDisabled() |
| 83 | + { |
| 84 | + $apiKey = Mage::getStoreConfig('logger/airbrake/apikey'); |
| 85 | + if (strlen(trim($apiKey)) == 0) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + public function insertException($reportData) |
| 92 | + { |
| 93 | + if ($this->isDisabled()) { |
| 94 | + return; |
| 95 | + } |
| 96 | + $backtraceLines = explode("\n", $reportData[1]); |
| 97 | + $backtraces = $this->formatStackTraceArray($backtraceLines); |
| 98 | + |
| 99 | + $this->client->notifyOnError($reportData[0], $backtraces); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @param string $message |
| 104 | + * @param int $backtraceLinesToSkip Number of backtrace lines/frames to skip |
| 105 | + */ |
| 106 | + public function sendToAirbrake($message, $backtraceLinesToSkip = 1) |
| 107 | + { |
| 108 | + if ($this->isDisabled()) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + $message = trim($message); |
| 113 | + $messageArray = explode("\n", $message); |
| 114 | + if (empty($messageArray)) { |
| 115 | + return; |
| 116 | + } |
| 117 | + $errorClass = 'PHP Error'; |
| 118 | + $errorMessage = array_shift($messageArray); |
| 119 | + $backTrace = array_slice(debug_backtrace(), $backtraceLinesToSkip); |
| 120 | + |
| 121 | + $matches = array(); |
| 122 | + if (preg_match('/exception \'(.*)\' with message \'(.*)\' in .*/', $errorMessage, $matches)) { |
| 123 | + $errorMessage = $matches[2]; |
| 124 | + $errorClass = $matches[1]; |
| 125 | + } |
| 126 | + if (count($messageArray) > 0) { |
| 127 | + $errorMessage .= '... [truncated]'; |
| 128 | + } |
| 129 | + |
| 130 | + $notice = new \Airbrake\Notice; |
| 131 | + $notice->load( |
| 132 | + array( |
| 133 | + 'errorClass' => $errorClass, |
| 134 | + 'backtrace' => $backTrace, |
| 135 | + 'errorMessage' => $errorMessage, |
| 136 | + ) |
| 137 | + ); |
| 138 | + |
| 139 | + $this->client->notify($notice); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @param array $backtraceLines |
| 144 | + * @return array |
| 145 | + */ |
| 146 | + protected function formatStackTraceArray($backtraceLines) |
| 147 | + { |
| 148 | + $backtraces = array(); |
| 149 | + |
| 150 | + foreach ($backtraceLines as $backtrace) { |
| 151 | + $temp = array(); |
| 152 | + $parts = explode(': ', $backtrace); |
| 153 | + |
| 154 | + if (isset($parts[1])) { |
| 155 | + $temp['function'] = $parts[1]; |
| 156 | + } |
| 157 | + |
| 158 | + $temp['file'] = substr($parts[0], 0, stripos($parts[0], '(')); |
| 159 | + $temp['line'] = substr( |
| 160 | + $parts[0], stripos($parts[0], '(') + 1, (stripos($parts[0], ')') - 1) - stripos($parts[0], '(') |
| 161 | + ); |
| 162 | + |
| 163 | + if (!empty($temp['function'])) { |
| 164 | + $backtraces[] = $temp; |
| 165 | + } |
| 166 | + } |
| 167 | + return $backtraces; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Satisfy newer Zend Framework |
| 172 | + * |
| 173 | + * @param array|Zend_Config $config Configuration |
| 174 | + * |
| 175 | + * @return void|Zend_Log_FactoryInterface |
| 176 | + */ |
| 177 | + public static function factory($config) |
| 178 | + { |
| 179 | + |
| 180 | + } |
| 181 | +} |
0 commit comments