Force magento to log errors even when in developer mode
-
12-12-2019 - |
Question
While writing a module, an observer had an undefined variable, which would typically create an E_NOTICE
error. However, I also had the Magento Developer Mode enabled. So when Abstract.php
caught the error, it sent it over to Mage::printException
it did not log it any where. The module was part of an ajax call, the result was that I never saw the error.
Do people actually use Magento developer mode? It is starting to seem counter productive without overriding the Magento core.
Does anyone have a solution to log all errors, even when in Developer mode? Am I missing something or is modifying Mage.php
the only way?
Solution
Have a look on Mage::printException()
. Mainly it has two parts and I will give you a brief Idea.
public static function printException(Exception $e, $extra = '')
{
if (self::$_isDeveloperMode) {
print '<pre>';
if (!empty($extra)) {
print $extra . "\n\n";
}
print $e->getMessage() . "\n\n";
print $e->getTraceAsString();
print '</pre>';
} else {
$reportData = array(
!empty($extra) ? $extra . "\n\n" : '' . $e->getMessage(),
$e->getTraceAsString()
);
// retrieve server data
if (isset($_SERVER)) {
if (isset($_SERVER['REQUEST_URI'])) {
$reportData['url'] = $_SERVER['REQUEST_URI'];
}
if (isset($_SERVER['SCRIPT_NAME'])) {
$reportData['script_name'] = $_SERVER['SCRIPT_NAME'];
}
}
// attempt to specify store as a skin
try {
$storeCode = self::app()->getStore()->getCode();
$reportData['skin'] = $storeCode;
}
catch (Exception $e) {}
require_once(self::getBaseDir() . DS . 'errors' . DS . 'report.php');
}
die();
}
So when Developer Mode is on, this is what happens.
if (self::$_isDeveloperMode) {
print '<pre>';
if (!empty($extra)) {
print $extra . "\n\n";
}
print $e->getMessage() . "\n\n";
print $e->getTraceAsString();
print '</pre>';
}
die();
In short, it prints the error on the browser and then die.
But if it is not in developer mode, it will actually log the error (actually more details) in var/report/
directory.
You have specified in your question that, module which is responsible for triggering Mage::printException()
is used as a part of ajax request. I think this is why you are not viewing the printed error on your browser (since you are in developer mode). Most probably you can see the error in your Browser console.