- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个 PHP 脚本需要执行数小时,有时由于某些原因(例如执行需要连接到互联网的脚本时出现网络问题等),执行过程会停止一段时间后做了错误的事情(并且它总是导致 E_NOTICE 当进程走向错误的方向时)
我的问题是,当您的脚本中出现警告时,是否有((任何))方法来通知您?(并停止整个脚本)
我的问题有一个伪代码:
if( ThereIs(AnyWarning) ) {
exit("Sorry! You have 1 E_NOTICE so we should stop script!");
}
我希望你明白我的意思:)
提前致谢!
最佳答案
检查 set_error_handler这将有助于您的要求。您可以退出,也可以在其中记录错误。或者,如果代码重定向到错误页面时出现错误/通知,您可以添加逻辑。
<?php
/**
* Used for logging all php notices,warings and etc in a file when error reporting
* is set and display_errors is off
* @uses used in prod env for logging all type of error of php code in a file for further debugging
* and code performance
* @author Aditya Mehrotra<aditycse@gmail.com>
*/
error_reporting(E_ALL);
ini_set("display_errors", "off");
define('ERROR_LOG_FILE', '/var/www/error.log');
/**
* Custom error handler
* @param integer $code
* @param string $description
* @param string $file
* @param interger $line
* @param mixed $context
* @return boolean
*/
function handleError($code, $description, $file = null, $line = null, $context = null) {
$displayErrors = ini_get("display_errors");
$displayErrors = strtolower($displayErrors);
if (error_reporting() === 0 || $displayErrors === "on") {
return false;
}
list($error, $log) = mapErrorCode($code);
$data = array(
'level' => $log,
'code' => $code,
'error' => $error,
'description' => $description,
'file' => $file,
'line' => $line,
'context' => $context,
'path' => $file,
'message' => $error . ' (' . $code . '): ' . $description . ' in [' . $file . ', line ' . $line . ']'
);
return fileLog($data);
}
/**
* This method is used to write data in file
* @param mixed $logData
* @param string $fileName
* @return boolean
*/
function fileLog($logData, $fileName = ERROR_LOG_FILE) {
$fh = fopen($fileName, 'a+');
if (is_array($logData)) {
$logData = print_r($logData, 1);
}
$status = fwrite($fh, $logData);
fclose($fh);
return ($status) ? true : false;
}
/**
* 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");
print_r($arra); //undefined variable
print_r($dssdfdfgg); //undefined variable
include_once 'file.php'; //No such file or directory
?>
关于php - 有什么办法可以在 php 代码中注意到 E_NOTICE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51364016/
我最近注意到一个隐藏的进程,它没有出现在进程列表中,但它消耗CPU时间和内存。这是怎么发生的,它是如何编写的,它在做什么,我们如何杀死该进程。 有什么方法可以取消隐藏此类隐藏的进程 最佳答案 简而言之
假设我有一个相当嵌套的 JS 对象,我需要对其进行 JSON 编码: var foo = { "totA": -1, "totB": -1, "totC": "13,052.0
我正在尝试使用 scrapy 抓取一些诗歌,我有这样的文本: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean co
这可能是常识,但我似乎无法找到有关该问题的任何信息。这是一些背景知识: 我有一些页面使用了 Bootstrap 的标签系统。在这些页面的 $(document).ready() 函数中是一些基于 UR
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我创建了一组新的 Controller ,当我尝试归档新代码时,在归档这些 Controller 时发生了这个错误: error: open /Users/owner12/Library/Develo
在 Xcode 12.5.1 中使用 IOS 14.5 部署目标遵循 UIDocumentPickerViewController 的现场 IOS 文档时,出现错误: Incorrect argume
我是一名优秀的程序员,十分优秀!