Skip to content

Commit

Permalink
Merge pull request #108 from firegento/develop
Browse files Browse the repository at this point in the history
New Release
  • Loading branch information
daim2k5 authored Oct 7, 2017
2 parents b2c752b + e1028bc commit 102b79c
Show file tree
Hide file tree
Showing 16 changed files with 1,140 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/app/code/community/FireGento/Logger/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ public function addEventMetadata(&$event, $notAvailable = null, $enableBacktrace
->setLine($notAvailable)
->setBacktrace($notAvailable)
->setStoreCode(Mage::app()->getStore()->getCode());
if (Mage::app()->getStore()->isAdmin() && isset($_SESSION)) {
$session = Mage::getSingleton('admin/session');
if ($session->isLoggedIn()) {
$event->setAdminUserId($session->getUserId());
$event->setAdminUserName($session->getUser()->getName());
}
}

// Add request time
if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
Expand Down
181 changes: 181 additions & 0 deletions src/app/code/community/FireGento/Logger/Model/Airbrake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php
/**
* This file is part of a FireGento e.V. module.
* This FireGento e.V. module is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
* This script is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* PHP version 5
*
* @category FireGento
* @package FireGento_Logger
* @author FireGento Team <[email protected]>
* @copyright 2013 FireGento Team (http://www.firegento.com)
* @license http://opensource.org/licenses/gpl-3.0 GNU General Public License, version 3 (GPLv3)
*/


/**
* Class FireGento_Logger_Model_Airbrake
*
* This file was ported from Elgentos_CodebaseExceptions_Helper_Data
* into this logger module.
*
* https://github.com/airbrake/Airbrake-Magento/blob/master/app/code/community/Elgentos/CodebaseExceptions/Helper/Data.php
*
*/


require_once Mage::getBaseDir('lib') . DS . 'Airbrake' . DS . 'Client.php';
require_once Mage::getBaseDir('lib') . DS . 'Airbrake' . DS . 'Configuration.php';

class FireGento_Logger_Model_Airbrake extends Zend_Log_Writer_Abstract
{

public function __construct()
{
$apiKey = Mage::getStoreConfig('logger/airbrake/apikey');

if ($this->isDisabled()) {
return;
}


$options = array();
// REQUEST_URI is not available in the CLI context
if (isset($_SERVER['REQUEST_URI'])) {
$requestUri = explode("/", $_SERVER['REQUEST_URI']);
$options['action'] = array_pop($requestUri);
$options['component'] = implode('/', array_slice($requestUri, -2));
} else {
$options['action'] = $_SERVER['PHP_SELF'];
$options['component'] = $_SERVER['PHP_SELF'];
}

$projectRoot = explode('/', $_SERVER['PHP_SELF']);
array_pop($projectRoot);
$options['projectRoot'] = implode('/', $projectRoot) . '/';
$options['host'] = Mage::getStoreConfig('logger/airbrake/host');
$options['secure'] = Mage::getStoreConfig('logger/airbrake/secure');
$options['environmentName'] = Mage::getStoreConfig('logger/airbrake/environment');
$options['timeout'] = Mage::getStoreConfig('logger/airbrake/timeout');
$config = new Airbrake\Configuration($apiKey, $options);
$this->client = new Airbrake\Client($config);
}


/**
* Write a message to the log.
*
* @param array $event event data
* @return void
* @throws Zend_Log_Exception
*/
protected function _write($event)
{
$this->sendToAirbrake($event['message'], 4);
}

protected function isDisabled()
{
$apiKey = Mage::getStoreConfig('logger/airbrake/apikey');
if (strlen(trim($apiKey)) == 0) {
return true;
}
return false;
}

public function insertException($reportData)
{
if ($this->isDisabled()) {
return;
}
$backtraceLines = explode("\n", $reportData[1]);
$backtraces = $this->formatStackTraceArray($backtraceLines);

$this->client->notifyOnError($reportData[0], $backtraces);
}

/**
* @param string $message
* @param int $backtraceLinesToSkip Number of backtrace lines/frames to skip
*/
public function sendToAirbrake($message, $backtraceLinesToSkip = 1)
{
if ($this->isDisabled()) {
return;
}

$message = trim($message);
$messageArray = explode("\n", $message);
if (empty($messageArray)) {
return;
}
$errorClass = 'PHP Error';
$errorMessage = array_shift($messageArray);
$backTrace = array_slice(debug_backtrace(), $backtraceLinesToSkip);

$matches = array();
if (preg_match('/exception \'(.*)\' with message \'(.*)\' in .*/', $errorMessage, $matches)) {
$errorMessage = $matches[2];
$errorClass = $matches[1];
}
if (count($messageArray) > 0) {
$errorMessage .= '... [truncated]';
}

$notice = new \Airbrake\Notice;
$notice->load(
array(
'errorClass' => $errorClass,
'backtrace' => $backTrace,
'errorMessage' => $errorMessage,
)
);

$this->client->notify($notice);
}

/**
* @param array $backtraceLines
* @return array
*/
protected function formatStackTraceArray($backtraceLines)
{
$backtraces = array();

foreach ($backtraceLines as $backtrace) {
$temp = array();
$parts = explode(': ', $backtrace);

if (isset($parts[1])) {
$temp['function'] = $parts[1];
}

$temp['file'] = substr($parts[0], 0, stripos($parts[0], '('));
$temp['line'] = substr(
$parts[0], stripos($parts[0], '(') + 1, (stripos($parts[0], ')') - 1) - stripos($parts[0], '(')
);

if (!empty($temp['function'])) {
$backtraces[] = $temp;
}
}
return $backtraces;
}

/**
* Satisfy newer Zend Framework
*
* @param array|Zend_Config $config Configuration
*
* @return void|Zend_Log_FactoryInterface
*/
public static function factory($config)
{

}
}
4 changes: 4 additions & 0 deletions src/app/code/community/FireGento/Logger/Model/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
* @method $this setRequestUri(string $value)
* @method string getStoreCode()
* @method $this setStoreCode(string $value)
* @method int getAdminUserId()
* @method $this setAdminUserId(int $value)
* @method string getAdminUserName()
* @method $this setAdminUserName(string $value)
* @method string getHttpUserAgent()
* @method $this setHttpUserAgent(string $value)
* @method string getHttpCookie()
Expand Down
2 changes: 1 addition & 1 deletion src/app/code/community/FireGento/Logger/Model/Graylog2.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected function _write($event)

Mage::helper('firegento_logger')->addEventMetadata($event);

$message = $event->getMessage();
$message = trim($event->getMessage());

$eofMessageFirstLine = strpos($message, "\n");
$shortMessage = (false === $eofMessageFirstLine) ? $message :
Expand Down
5 changes: 2 additions & 3 deletions src/app/code/community/FireGento/Logger/Model/Logstash.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ protected function buildJSONMessage($event, $enableBacktrace = false)
/** this prevents different datatypes as getHttpHost() returns either string or boolean (false) */
$fields['HttpHost'] = (!Mage::app()->getRequest()->getHttpHost()) ? 'cli': Mage::app()->getRequest()->getHttpHost();
$fields['LogFileName'] = $this->_logFileName;
/** This is to prevent infinite loops with Cm_Redis_Session because the constructor calls the logging if
* log_level >= Zend_Log::Debug */
if ((int) Mage::getConfig()->getNode('global/redis_session')->descend('log_level') < Zend_Log::DEBUG) {
// Only add session fields if a session was already instantiated and logger should not start a new session
if (isset($_SESSION)) {
$fields['SessionId'] = Mage::getSingleton("core/session")->getEncryptedSessionId();
$fields['CustomerId'] = Mage::getSingleton('customer/session')->getCustomerId();
}
Expand Down
8 changes: 8 additions & 0 deletions src/app/code/community/FireGento/Logger/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@
<label>Redis Logger</label>
<class>FireGento_Logger_Model_Redis</class>
</redis>
<airbrake>
<label>Airbrake</label>
<class>FireGento_Logger_Model_Airbrake</class>
</airbrake>
</writer_models>
</core>
</log>
Expand All @@ -176,6 +180,7 @@
</template>
</global>
<default>

<logger>
<general>
<targets>advanced</targets>
Expand Down Expand Up @@ -269,6 +274,9 @@ confirmation]]></filter_request_data>
<password/>
<type>magento-log</type>
</redis>
<airbrake>
<timeout>60</timeout>
</airbrake>
</logger>
</default>
<frontend>
Expand Down
62 changes: 62 additions & 0 deletions src/app/code/community/FireGento/Logger/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
%priorityName%<br/>
%priority%<br/>
%storeCode%<br/>
%adminUserId%<br/>
%adminUserName%<br/>
%timeElapsed%<br/>
%hostname%<br/>
%requestMethod%<br/>
Expand Down Expand Up @@ -736,6 +738,66 @@
</key>
</fields>
</redis>
<airbrake translate="label">
<label>Airbrake</label>
<expanded>1</expanded>
<sort_order>600</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment><![CDATA[Please read: <a href="http://www.proxiblue.com.au/blog/airbrake-with-magento">Enable Report Exception Logging</a> to complete the Airbrake setup.]]></comment>
<fields>
<priority translate="label">
<label>Priority Level Filter</label>
<frontend_type>select</frontend_type>
<source_model>firegento_logger/system_config_source_prioritydefault</source_model>
<sort_order>11</sort_order>
<show_in_default>1</show_in_default>
<comment>Choose the lowest priority level to be logged.</comment>
</priority>
<apikey translate="label">
<label>Airbrake API key</label>
<frontend_type>text</frontend_type>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</apikey>
<host translate="label">
<label>Airbrake hostname</label>
<frontend_type>text</frontend_type>
<sort_order>30</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</host>
<secure translate="label">
<label>Use HTTPS</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>40</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</secure>
<environment translate="label">
<label>Environment</label>
<frontend_type>text</frontend_type>
<sort_order>50</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</environment>
<timeout translate="label">
<label>API Timeout</label>
<frontend_type>text</frontend_type>
<sort_order>60</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</timeout>
</fields>
</airbrake>
</groups>
</logger>
</sections>
Expand Down
Loading

0 comments on commit 102b79c

Please sign in to comment.