Last active
February 24, 2021 18:25
-
-
Save Mte90/e09a84a4e1c61690e55b3a6e0c67ef94 to your computer and use it in GitHub Desktop.
Custom WordPress Error Handler. Support Query Monitor and can be used as mu-plugin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Better errors | |
* Description: Better errors in log | |
* Author: Daniele Scasciafratte | |
* Version: 1.0 | |
* Author URI: http://codeat.co | |
*/ | |
function handleError($code, $description, $file = null, $line = null, $context = null) { | |
list($error, $log) = mapErrorCode($code); | |
$data = array( | |
'error' => $error, | |
'file' => $file, | |
'line' => $line, | |
'context' => $context, | |
'debug' => var_dump(debug_backtrace()), | |
'filter' => current_filter(), | |
'message' => $error . ' (' . $code . '): ' . $description | |
); | |
if(isset($_SERVER['HTTP_HOST'])) { | |
$data['url'] = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; | |
} else { | |
$data['cli'] = true; | |
} | |
return error_log(print_r($data,true)); | |
} | |
/** | |
* Map an error code into an Error word, and log location. | |
* | |
* @param int $code Error code to map | |
* @return array Array of error word, and log location. | |
*/ | |
function mapErrorCode($code) { | |
$error = $log = null; | |
switch ($code) { | |
case E_PARSE: | |
case E_ERROR: | |
case E_CORE_ERROR: | |
case E_COMPILE_ERROR: | |
case E_USER_ERROR: | |
$error = 'Fatal Error'; | |
$log = LOG_ERR; | |
break; | |
case E_WARNING: | |
case E_USER_WARNING: | |
case E_COMPILE_WARNING: | |
case E_RECOVERABLE_ERROR: | |
$error = 'Warning'; | |
$log = LOG_WARNING; | |
break; | |
case E_NOTICE: | |
case E_USER_NOTICE: | |
$error = 'Notice'; | |
$log = LOG_NOTICE; | |
break; | |
case E_STRICT: | |
$error = 'Strict'; | |
$log = LOG_NOTICE; | |
break; | |
case E_DEPRECATED: | |
case E_USER_DEPRECATED: | |
$error = 'Deprecated'; | |
$log = LOG_NOTICE; | |
break; | |
default : | |
break; | |
} | |
return array($error, $log); | |
} | |
//calling custom error handler | |
set_error_handler("handleError"); | |
function handleErrorAjax() { | |
if ( defined( 'DOING_AJAX' ) ) { | |
$type = 'GET'; | |
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
$type = 'POST'; | |
} | |
error_log("Ajax, " . $type . ": " . print_r(array($_REQUEST),true)); | |
} | |
} | |
if (function_exists('add_action')) { | |
add_action('qm/collect/new_php_error', 'handleError', 99999, 5 ); | |
add_action('init', 'handleErrorAjax', 99999, 5 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment