【问题标题】:How to disable displaying of errors in php and log them in a file如何禁用在php中显示错误并将它们记录在文件中
【发布时间】:2011-12-29 04:03:11
【问题描述】:

我正在尝试禁用在文件中显示错误并将它们记录到日志文件中

<?php
error_reporting('E_ALL');
echo $x;
?>

当我删除 error_reporting('E_ALL') 时日志文件有效,但随后也会显示错误。 是否有其他方法可以做到这一点,但仅在特定页面上禁用错误报告。

【问题讨论】:

    标签: php error-reporting


    【解决方案1】:

    您可以创建自定义错误处理函数并通过set_error_handler()进行设置:

    set_error_handler(function($errno, $errstr, $errfile = null, $errline = 0, $errcontext = null)
    {
        // Append $errstr and other useful information to a file
        @file_put_contents('error_log', "ERROR: $errstr\n", FILE_APPEND);
    
        return true; // disable regular PHP error reporting
    });
    

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 由于网站上的许可,我无法发布源代码。
      猜你喜欢
      • 2017-01-25
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多