* changes in error logging: show only in debug mode

This commit is contained in:
Max Pozdeev 2021-07-24 18:16:16 +03:00
parent 8fe4cbf882
commit 62e1dc0c96
3 changed files with 50 additions and 21 deletions

View file

@ -6,12 +6,17 @@
Licensed under the GNU GPL v2 license. See file COPYRIGHT for details.
*/
set_error_handler('myErrorHandler');
set_exception_handler('myExceptionHandler');
require_once('./init.php');
require_once(MTTINC. 'markup.php');
if (MTT_DEBUG) {
set_error_handler('myErrorHandler'); //catch Notices, Warnings
set_exception_handler('myExceptionHandler');
}
else {
ini_set('display_errors', '0');
}
$db = DBConnection::instance();
if(isset($_GET['loadLists']))
@ -802,15 +807,15 @@ function daysInMonth($m, $y=0)
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if($errno==E_ERROR || $errno==E_CORE_ERROR || $errno==E_COMPILE_ERROR || $errno==E_USER_ERROR || $errno==E_PARSE) $error = 'Error';
elseif($errno==E_WARNING || $errno==E_CORE_WARNING || $errno==E_COMPILE_WARNING || $errno==E_USER_WARNING || $errno==E_STRICT) {
if(error_reporting() & $errno) $error = 'Warning'; else return;
if ($errno==E_ERROR || $errno==E_CORE_ERROR || $errno==E_COMPILE_ERROR || $errno==E_USER_ERROR || $errno==E_PARSE) $error = 'Error';
elseif ($errno==E_WARNING || $errno==E_CORE_WARNING || $errno==E_COMPILE_WARNING || $errno==E_USER_WARNING || $errno==E_STRICT) {
if (error_reporting() & $errno) $error = 'Warning'; else return;
}
elseif($errno==E_NOTICE || $errno==E_USER_NOTICE) {
if(error_reporting() & $errno) $error = 'Notice'; else return;
elseif ($errno==E_NOTICE || $errno==E_USER_NOTICE) {
if (error_reporting() & $errno) $error = 'Notice'; else return;
}
elseif(defined('E_DEPRECATED') && ($errno==E_DEPRECATED || $errno==E_USER_DEPRECATED)) { # since 5.3.0
if(error_reporting() & $errno) $error = 'Notice'; else return;
elseif (defined('E_DEPRECATED') && ($errno==E_DEPRECATED || $errno==E_USER_DEPRECATED)) { # since 5.3.0
if (error_reporting() & $errno) $error = 'Notice'; else return;
}
else $error = "Error ($errno)"; # here may be E_RECOVERABLE_ERROR
throw new Exception("$error: '$errstr' in $errfile:$errline", -1);
@ -821,14 +826,23 @@ function myExceptionHandler($e)
// to avoid Exception thrown without a stack frame
try
{
if(-1 == $e->getCode()) {
if (-1 == $e->getCode()) {
//thrown in myErrorHandler
die2($e->getMessage()."\n". $e->getTraceAsString(), $e->getMessage());
logAndDie( $e->getMessage() );
}
die2('Exception: \''. $e->getMessage() .'\' in '. $e->getFile() .':'. $e->getLine());
$c = get_class($e);
$errText = "Exception ($c): '". $e->getMessage(). "' in ". $e->getFile(). ":". $e->getLine() ;
if (MTT_DEBUG) {
if ( count($e->getTrace()) > 0 ) {
$errText .= "\n". $e->getTraceAsString() ;
}
}
logAndDie($errText);
}
catch(Exception $e) {
die2('Exception in ExceptionHandler: \''. $e->getMessage() .'\' in '. $e->getFile() .':'. $e->getLine());
catch (Exception $e) {
logAndDie('Exception in ExceptionHandler: \''. $e->getMessage() .'\' in '. $e->getFile() .':'. $e->getLine());
}
exit;
}

View file

@ -43,7 +43,7 @@ a { color:#0000ff; cursor:pointer; text-decoration:underline; }
#msg { }
#msg .msg-text { font-weight:bold; cursor:pointer; }
#msg .msg-details { padding:1px 4px; background-color:#fff; display:none; max-width:700px; position:absolute; z-index:2; }
#msg .msg-details { padding:1px 4px; background-color:#fff; display:none; max-width:800px; position:absolute; z-index:2; white-space: pre-line; }
#msg.mtt-error .msg-text { background-color:#ff3333; }
#msg.mtt-error .msg-details { border:1px solid #ff3333; }
#msg.mtt-info .msg-text { background-color:#EFC300; }

View file

@ -19,11 +19,21 @@ if(!defined('MTTCONTENT')) define('MTTCONTENT', MTTPATH. 'content/');
if(!defined('MTTLANG')) define('MTTLANG', MTTCONTENT. 'lang/');
if(!defined('MTTTHEMES')) define('MTTTHEMES', MTTCONTENT. 'themes/');
if (getenv('MTT_ENABLE_DEBUG') == 'YES') {
define('MTT_DEBUG', true);
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('log_errors', '1');
}
else {
//ini_set('display_errors', '0');
//ini_set('log_errors', '1');
define('MTT_DEBUG', false);
}
require_once(MTTINC. 'common.php');
require_once(MTTPATH. 'db/config.php');
ini_set('display_errors', 'On');
if(!isset($config)) global $config;
Config::loadConfig($config);
unset($config);
@ -39,7 +49,7 @@ if(Config::get('db') == 'mysql')
$db->connect(Config::get('mysql.host'), Config::get('mysql.user'), Config::get('mysql.password'), Config::get('mysql.db'));
}
catch(Exception $e) {
die2("Failed to connect to mysql database: ". $e->getMessage());
logAndDie("Failed to connect to mysql database: ". $e->getMessage());
}
$db->dq("SET NAMES utf8");
}
@ -226,10 +236,15 @@ function jsonExit($data)
exit;
}
function die2($userText, $errText = null)
function logAndDie($userText, $errText = null)
{
$errText === null ? error_log($userText) : error_log($errText);
echo $userText;
if (ini_get('display_errors')) {
echo $userText;
}
else {
echo "Error! See details in error log.";
}
exit(1);
}