From 5e367744b68c525d59e3c28880f3754e17a300ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Gaillard?= Date: Fri, 28 Dec 2012 16:12:28 +0100 Subject: [PATCH] [*] Core: Log the exceptions in a file #PSCFV-6334 --- classes/exception/PrestaShopException.php | 42 ++++++++++++++++++----- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/classes/exception/PrestaShopException.php b/classes/exception/PrestaShopException.php index c0920313c..ce369186a 100644 --- a/classes/exception/PrestaShopException.php +++ b/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 @@ class PrestaShopExceptionCore extends Exception '; echo '
'; echo '

['.get_class($this).']

'; - printf( - '

%s
at line %d in file %s

', - $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 @@ class PrestaShopExceptionCore extends Exception } 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 @@ class PrestaShopExceptionCore extends Exception } echo ''; } + + /** + * 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 = '

%s
at line %d in file %s

'; + if (!$html) + $format = strip_tags(str_replace('
', ' ', $format)); + + return sprintf( + $format, + $this->getMessage(), + $this->getLine(), + ltrim(str_replace(array(_PS_ROOT_DIR_, '\\'), array('', '/'), $this->getFile()), '/') + ); + } + }