按位运算符和错误级别 (php)
我已经创建了一个用户错误处理程序,并且我想确保正确使用按位运算符。
以下是我的配置设置,用于设置以哪种方式处理哪些错误:
// 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);
这是我如何设置用户错误处理程序:
// set user error handler
set_error_handler('user_error_handler', E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);
这是错误处理程序本身:
// 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 />';
}
break;
default:
// call PHP internal error handler
return false;
}
// do not call PHP internal error handler
return true;
}
问题:
- 我的处理程序设置中使用的按位 OR 运算符将使仅当这三个错误之一发生时才调用错误处理程序?
- 在我的错误处理程序的第一行中使用的按位或运算符将使得只有当两个配置设置都设置为零时,该函数才会退出?
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 />';
}
break;
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在该行中,
比较比按位运算符更强,因此您需要括号:
然后您的代码将按照您所描述的方式工作。
In the line
the comparison is stronger than the bitwise operator, so you need parentheses:
Then your code will work as you describe.