Skip to content

Commit 102b79c

Browse files
authored
Merge pull request #108 from firegento/develop
New Release
2 parents b2c752b + e1028bc commit 102b79c

16 files changed

+1140
-4
lines changed

src/app/code/community/FireGento/Logger/Helper/Data.php

+7
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ public function addEventMetadata(&$event, $notAvailable = null, $enableBacktrace
130130
->setLine($notAvailable)
131131
->setBacktrace($notAvailable)
132132
->setStoreCode(Mage::app()->getStore()->getCode());
133+
if (Mage::app()->getStore()->isAdmin() && isset($_SESSION)) {
134+
$session = Mage::getSingleton('admin/session');
135+
if ($session->isLoggedIn()) {
136+
$event->setAdminUserId($session->getUserId());
137+
$event->setAdminUserName($session->getUser()->getName());
138+
}
139+
}
133140

134141
// Add request time
135142
if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
}

src/app/code/community/FireGento/Logger/Model/Event.php

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
* @method $this setRequestUri(string $value)
3838
* @method string getStoreCode()
3939
* @method $this setStoreCode(string $value)
40+
* @method int getAdminUserId()
41+
* @method $this setAdminUserId(int $value)
42+
* @method string getAdminUserName()
43+
* @method $this setAdminUserName(string $value)
4044
* @method string getHttpUserAgent()
4145
* @method $this setHttpUserAgent(string $value)
4246
* @method string getHttpCookie()

src/app/code/community/FireGento/Logger/Model/Graylog2.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ protected function _write($event)
112112

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

115-
$message = $event->getMessage();
115+
$message = trim($event->getMessage());
116116

117117
$eofMessageFirstLine = strpos($message, "\n");
118118
$shortMessage = (false === $eofMessageFirstLine) ? $message :

src/app/code/community/FireGento/Logger/Model/Logstash.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,8 @@ protected function buildJSONMessage($event, $enableBacktrace = false)
107107
/** this prevents different datatypes as getHttpHost() returns either string or boolean (false) */
108108
$fields['HttpHost'] = (!Mage::app()->getRequest()->getHttpHost()) ? 'cli': Mage::app()->getRequest()->getHttpHost();
109109
$fields['LogFileName'] = $this->_logFileName;
110-
/** This is to prevent infinite loops with Cm_Redis_Session because the constructor calls the logging if
111-
* log_level >= Zend_Log::Debug */
112-
if ((int) Mage::getConfig()->getNode('global/redis_session')->descend('log_level') < Zend_Log::DEBUG) {
110+
// Only add session fields if a session was already instantiated and logger should not start a new session
111+
if (isset($_SESSION)) {
113112
$fields['SessionId'] = Mage::getSingleton("core/session")->getEncryptedSessionId();
114113
$fields['CustomerId'] = Mage::getSingleton('customer/session')->getCustomerId();
115114
}

src/app/code/community/FireGento/Logger/etc/config.xml

+8
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@
162162
<label>Redis Logger</label>
163163
<class>FireGento_Logger_Model_Redis</class>
164164
</redis>
165+
<airbrake>
166+
<label>Airbrake</label>
167+
<class>FireGento_Logger_Model_Airbrake</class>
168+
</airbrake>
165169
</writer_models>
166170
</core>
167171
</log>
@@ -176,6 +180,7 @@
176180
</template>
177181
</global>
178182
<default>
183+
179184
<logger>
180185
<general>
181186
<targets>advanced</targets>
@@ -269,6 +274,9 @@ confirmation]]></filter_request_data>
269274
<password/>
270275
<type>magento-log</type>
271276
</redis>
277+
<airbrake>
278+
<timeout>60</timeout>
279+
</airbrake>
272280
</logger>
273281
</default>
274282
<frontend>

src/app/code/community/FireGento/Logger/etc/system.xml

+62
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
%priorityName%<br/>
7272
%priority%<br/>
7373
%storeCode%<br/>
74+
%adminUserId%<br/>
75+
%adminUserName%<br/>
7476
%timeElapsed%<br/>
7577
%hostname%<br/>
7678
%requestMethod%<br/>
@@ -736,6 +738,66 @@
736738
</key>
737739
</fields>
738740
</redis>
741+
<airbrake translate="label">
742+
<label>Airbrake</label>
743+
<expanded>1</expanded>
744+
<sort_order>600</sort_order>
745+
<show_in_default>1</show_in_default>
746+
<show_in_website>1</show_in_website>
747+
<show_in_store>1</show_in_store>
748+
<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>
749+
<fields>
750+
<priority translate="label">
751+
<label>Priority Level Filter</label>
752+
<frontend_type>select</frontend_type>
753+
<source_model>firegento_logger/system_config_source_prioritydefault</source_model>
754+
<sort_order>11</sort_order>
755+
<show_in_default>1</show_in_default>
756+
<comment>Choose the lowest priority level to be logged.</comment>
757+
</priority>
758+
<apikey translate="label">
759+
<label>Airbrake API key</label>
760+
<frontend_type>text</frontend_type>
761+
<sort_order>20</sort_order>
762+
<show_in_default>1</show_in_default>
763+
<show_in_website>0</show_in_website>
764+
<show_in_store>0</show_in_store>
765+
</apikey>
766+
<host translate="label">
767+
<label>Airbrake hostname</label>
768+
<frontend_type>text</frontend_type>
769+
<sort_order>30</sort_order>
770+
<show_in_default>1</show_in_default>
771+
<show_in_website>0</show_in_website>
772+
<show_in_store>0</show_in_store>
773+
</host>
774+
<secure translate="label">
775+
<label>Use HTTPS</label>
776+
<frontend_type>select</frontend_type>
777+
<source_model>adminhtml/system_config_source_yesno</source_model>
778+
<sort_order>40</sort_order>
779+
<show_in_default>1</show_in_default>
780+
<show_in_website>0</show_in_website>
781+
<show_in_store>0</show_in_store>
782+
</secure>
783+
<environment translate="label">
784+
<label>Environment</label>
785+
<frontend_type>text</frontend_type>
786+
<sort_order>50</sort_order>
787+
<show_in_default>1</show_in_default>
788+
<show_in_website>0</show_in_website>
789+
<show_in_store>0</show_in_store>
790+
</environment>
791+
<timeout translate="label">
792+
<label>API Timeout</label>
793+
<frontend_type>text</frontend_type>
794+
<sort_order>60</sort_order>
795+
<show_in_default>1</show_in_default>
796+
<show_in_website>0</show_in_website>
797+
<show_in_store>0</show_in_store>
798+
</timeout>
799+
</fields>
800+
</airbrake>
739801
</groups>
740802
</logger>
741803
</sections>

0 commit comments

Comments
 (0)