我error_log在 php.ini 文件中配置了指令,如下所示:
error_log = /path/to/logs/error_log
Run Code Online (Sandbox Code Playgroud)
然后,我配置了这样的error_reporting指令:
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
Run Code Online (Sandbox Code Playgroud)
当我检查 error_log 文件时,我看到正常的 php 警告/错误文本行:
[03-Jun-2015 08:39:00 America/Bogota] PHP Notice: Undefined index: cerrar in /fake/path/to/file2.php on line 68
[03-Jun-2015 08:40:49 America/Bogota] PHP Notice: Undefined index: in /fake/path/to/file2.php on line 344
Run Code Online (Sandbox Code Playgroud)
问题是:有没有办法改变输出格式?我的意思是,如果我可以打印,例如,导致警告的 IP 和子域。
我在 StackOverflow 和 Google 中寻找,但没有找到明确的信息或示例。
非常感谢您的帮助。
因此,同意并最终确定上面给出的所有评论,最好的方法是set_error_handler。
我为你写了一个课程。我还喜欢set_exception_handler以获得统一的体验,并将所有错误保存Error::$throwables在关闭时显示它们而不是视图(由View此处未提供的类处理)。
class Error
{
public static $error_types = array(
E_ERROR => 'E_ERROR',
E_WARNING => 'E_WARNING',
E_PARSE => 'E_PARSE',
E_NOTICE => 'E_NOTICE',
E_CORE_ERROR => 'E_CORE_ERROR',
E_CORE_WARNING => 'E_CORE_WARNING',
E_COMPILE_ERROR => 'E_COMPILE_ERROR',
E_COMPILE_WARNING => 'E_COMPILE_WARNING',
E_USER_ERROR => 'E_USER_ERROR',
E_USER_WARNING => 'E_USER_WARNING',
E_USER_NOTICE => 'E_USER_NOTICE',
E_STRICT => 'E_STRICT',
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
E_DEPRECATED => 'E_DEPRECATED',
E_USER_DEPRECATED => 'E_USER_DEPRECATED'
);
public static $shutdown = FALSE;
public static $throwables = array();
public static function set_throwable_handlers()
{
ini_set('error_reporting', E_ALL & ~E_DEPRECATED & ~E_STRICT);
ini_set('display_errors', FALSE);
ini_set('log_errors', TRUE);
ini_set('error_log', '/path/to/logs/error_log');
set_error_handler(array('Error', 'error_handler'));
set_exception_handler(array('Error', 'exception_handler'));
register_shutdown_function(array('Error', 'shutdown_handler'));
}
public static function set_throwable($error_number, $error_text, $error_file, $error_line, $error_log = TRUE)
{
if ($error_log === TRUE)
{
//provide any data you want to log to error log
error_log('PHP ' . self::$error_types[$error_number] . ' : ' . $error_text . ' in ' . $error_file . ' on line ' . $error_line);
}
//provide any data you want to class variable
self::$throwables[$error_number][] = array('type' => self::$error_types[$error_number], 'text' => $error_text, 'file' => $error_file, 'line' => $error_line);
}
public static function exception_handler(Exception $exception)
{
self::set_throwable($exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine());
}
public static function error_handler($error_number = '', $error_text = '', $error_file = '', $error_line = '')
{
self::set_throwable($error_number, $error_text, $error_file, $error_line);
}
public static function shutdown_handler()
{
$error = error_get_last();
if ($error !== NULL)
{
self::set_throwable($error['type'], $error['message'], $error['file'], $error['line'], FALSE);
}
//enables error page on shutdown & displays the throwables
//self::$shutdown = TRUE;
//
//View::display();
}
public static function throw_error($error_text, $error_number = E_USER_NOTICE)
{
trigger_error($error_text, $error_number);
exit;
}
}
Run Code Online (Sandbox Code Playgroud)