Permalink
Browse files

[*] Core: Log the exceptions in a file #PSCFV-6334

  • Loading branch information...
1 parent d0eb31f commit a788394a4aa4695d329655bb3746890e0bf32a0a @rGaillard rGaillard committed Dec 28, 2012
Showing with 34 additions and 8 deletions.
  1. +34 −8 classes/exception/PrestaShopException.php
@@ -34,6 +34,7 @@ class PrestaShopExceptionCore extends Exception
*/
public function displayMessage()
{
+ header('HTTP/1.1 500 Internal Server Error');
if (_PS_MODE_DEV_ || defined('_PS_ADMIN_DIR_'))
{
// Display error message
@@ -50,12 +51,7 @@ public function displayMessage()
</style>';
echo '<div id="psException">';
echo '<h2>['.get_class($this).']</h2>';
- printf(
- '<p><b>%s</b><br /><i>at line </i><b>%d</b><i> in file </i><b>%s</b></p>',
- $this->getMessage(),
- $this->getLine(),
- ltrim(str_replace(array(_PS_ROOT_DIR_, '\\'), array('', '/'), $this->getFile()), '/')
- );
+ echo $this->getExentedMessage();
$this->displayFileDebug($this->getFile(), $this->getLine());
@@ -85,11 +81,12 @@ public function displayMessage()
}
else
{
- // If not in mode dev, launch a http 500 error
- header('HTTP/1.1 500 Internal Server Error');
+ // If not in mode dev, display an error page
if (file_exists(_PS_ROOT_DIR_.'/error500.html'))
echo file_get_contents(_PS_ROOT_DIR_.'/error500.html');
}
+ // Log the error in the disk
+ $this->logError();
exit;
}
@@ -140,4 +137,33 @@ protected function displayArgsDebug($args, $id)
}
echo '</pre>';
}
+
+ /**
+ * Log the error on the disk
+ */
+ protected function logError()
+ {
+ $logger = new FileLogger();
+ $logger->setFilename(_PS_ROOT_DIR_.'/log/'.date('Ymd').'_exception.log');
+ $logger->logError($this->getExentedMessage(false));
+ }
+
+ /**
+ * Return the content of the Exception
+ * @return string content of the exception
+ */
+ protected function getExentedMessage($html = true)
+ {
+ $format = '<p><b>%s</b><br /><i>at line </i><b>%d</b><i> in file </i><b>%s</b></p>';
+ if (!$html)
+ $format = strip_tags(str_replace('<br />', ' ', $format));
+
+ return sprintf(
+ $format,
+ $this->getMessage(),
+ $this->getLine(),
+ ltrim(str_replace(array(_PS_ROOT_DIR_, '\\'), array('', '/'), $this->getFile()), '/')
+ );
+ }
+
}

3 comments on commit a788394

Contributor

PrestaEdit replied Dec 29, 2012

Just a question... Why not use Logger::addLog() ? ;)

Owner

rGaillard replied Jan 2, 2013

Because some exceptions are made when the database is not accessible ;)

Contributor

PrestaEdit replied Jan 2, 2013

True, ... :) Thanks for the answer, ;)

Please sign in to comment.