I have created a user error handler, and I want to make sure that I am using bitwise operators correctly.
Here are my config settings to set which errors will be handled in which way:
// user error logging level (change for production)
define('LEV_USER_ERROR_LOG_LEVEL', E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);
// user error display level (change for production)
define('LEV_USER_ERROR_DISPLAY_LEVEL', E_USER_ERROR);
Here is how I set the user error handler:
// set user error handler
set_error_handler('user_error_handler', E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);
Here is the error handler itself:
// user error handler
public static function user_error_handler($error_level, $message, $file_name, $line_number) {
if (LEV_USER_ERROR_LOG_LEVEL | LEV_USER_ERROR_DISPLAY_LEVEL == 0) return true;
switch ($error_level) {
case E_USER_ERROR:
if (LEV_USER_ERROR_LOG_LEVEL & E_USER_ERROR) {
error_log('[' . date('Y-m-d h:i:s') . '] User Error: "' . $message . '", File: "'.$file_name.'", Line: '.$line_number.', Request: "' . $_SERVER['ORIG_PATH_INFO'] . "\"\n", 3, 'application/logs/user_error_log.txt');
}
if (LEV_USER_ERROR_DISPLAY_LEVEL & E_USER_ERROR) {
echo '[' . date('Y-m-d h:i:s') . '] User Level Error: "' . $message . '", File: "'.$file_name.'", Line: '.$line_number.'<br />';
}
die;
break;
case E_USER_WARNING:
if (LEV_USER_ERROR_LOG_LEVEL & E_USER_WARNING) {
error_log('[' . date('Y-m-d h:i:s') . '] User Warning: "' . $message . '", File: "'.$file_name.'", Line: '.$line_number.', Request: "' . $_SERVER['ORIG_PATH_INFO'] . "\"\n", 3, 'application/logs/user_error_log.txt');
}
if (LEV_USER_ERROR_DISPLAY_LEVEL & E_USER_WARNING) {
echo '[' . date('Y-m-d h:i:s') . '] User Level Warning: "' . $message . '", File: "'.$file_name.'", Line: '.$line_number.'<br />';
}
break;
case E_USER_NOTICE:
if (LEV_USER_ERROR_LOG_LEVEL & E_USER_NOTICE) {
error_log('[' . date('Y-m-d h:i:s') . '] User Notice: "' . $message . '", File: "'.$file_name.'", Line: '.$line_number.', Request: "' . $_SERVER['ORIG_PATH_INFO'] . "\"\n", 3, 'application/logs/user_error_log.txt');
}
if (LEV_USER_ERROR_DISPLAY_LEVEL & E_USER_NOTICE) {
echo '[' . date('Y-m-d h:i:s') . '] User Level Notice: "' . $message . '", File: "'.$file_name.'", Line: '.$line_number.'<br />';
}
br开发者_StackOverflow社区eak;
default:
// call PHP internal error handler
return false;
}
// do not call PHP internal error handler
return true;
}
Questions:
- The bitwise OR operator used in my handler setting will make it so that the error handler is ONLY called when one of those three errors occurs?
- The bitwise OR operator used in the first line of my error handler will make it so that ONLY if both of the config settings are set to zero, the function will exit?
In the line
if (LEV_USER_ERROR_LOG_LEVEL | LEV_USER_ERROR_DISPLAY_LEVEL == 0) return true;
the comparison is stronger than the bitwise operator, so you need parentheses:
if ((LEV_USER_ERROR_LOG_LEVEL | LEV_USER_ERROR_DISPLAY_LEVEL) == 0) return true;
Then your code will work as you describe.
精彩评论