【问题标题】:Code Igniter logging too muchCodeigniter 记录太多
【发布时间】:2009-12-18 15:46:46
【问题描述】:

在我的 CI 配置文件中,我设置了这个日志记录阈值:

$config['log_threshold'] = 1;

在index.php中,我设置了如下报错:

error_reporting(E_ERROR);

我的期望是这将记录我记录的所有 CI 错误(使用 log_message('error','my error message')),以及任何 PHP 错误。但是,我希望它不会记录 PHP 通知,只会记录错误。但是,当我查看日志文件时,似乎也记录了 PHP 通知:

错误 - 2009-12-18 13:21:50—> 严重性:通知 —> 未定义变量:pageindex /var/www/apps/OS4W/system/application/views/user/view.php 12
错误 - 2009-12-18 13:21:50—> 严重性:通知 —> 未定义变量:friendsmode /var/www/apps/OS4W/system/application/views/user/activitytable.php 207

虽然日志行以“ERROR”开头,但实际上这似乎是一个 PHP 通知,有点像警告,我不想记录。如何确保只记录 CI 和 PHP 错误,而不记录 PHP 通知?我以为error_reporting(E_ERROR) 会这样做吗?

【问题讨论】:

  • 顺便说一下,请忽略错误报告功能中多余的下划线,这是由于 StackOverflow 的预览面板中的问题。
  • 我编辑了您的格式以使用代码块而不是代码块引用。如果您愿意,可以随意回滚,但我认为使用代码块和正确的文本更有意义。
  • 感谢无眼皮,绝对有进步

标签: php codeigniter


【解决方案1】:

我通过更改库/Log.php6 中日志级别的 ID 来修复它,例如:

改变:

protected $_levels  = array('ERROR' => '1', 'DEBUG' => '2',  'INFO' => '3', 'ALL' => '4');

到:

protected $_levels  = array('ERROR' => '1', 'DEBUG' => '3',  'INFO' => '2', 'ALL' => '4');

【讨论】:

    【解决方案2】:

    根据http://us2.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting 的 PHP 错误报告文档

    在 PHP 4 和 PHP 5 中默认值 是 E_ALL & ~E_NOTICE。这个设置 不显示 E_NOTICE 级别错误。 您可能希望在 发展。

    我会尝试将您的 error_reporting() 更改为“E_ALL & ~E_NOTICE”,看看是否可行。

    达娜

    编辑:嗯,我说得太早了。我试过了,它阻止了通知显示在屏幕上,但仍将其记录到日志文件中。

    解决方案:

    好的,我想我想通了。在 common.php 文件中有一个名为“_exception_handler”的函数来处理日志记录过程。它对当前的严重级别和 error_reporting 级别进行了一些按位比较,以查看它是否应该记录到屏幕上,但它不这样做以记录到日志文件。 IT 只是传递所有内容,除了 E_STRICT 消息,它无论如何都会扔掉。

    你可以做的就是在这个函数的最后一行用它们用来记录到屏幕的相同的 IF 语句来包装。所以整个函数就变成了:

    function _exception_handler($severity, $message, $filepath, $line)
    {   
     // We don't bother with "strict" notices since they will fill up
     // the log file with information that isn't normally very
     // helpful.  For example, if you are running PHP 5 and you
     // use version 4 style class functions (without prefixes
     // like "public", "private", etc.) you'll get notices telling
     // you that these have been deprecated.
    
    if ($severity == E_STRICT)
    {
        return;
    }
    
    $error =& load_class('Exceptions');
    
    // Should we display the error?
    // We'll get the current error_reporting level and add its bits
    // with the severity bits to find out.  
    if (($severity & error_reporting()) == $severity)
    {
        $error->show_php_error($severity, $message, $filepath, $line);
    }
    
    // Should we log the error?  No?  We're done...
    $config =& get_config();
    if ($config['log_threshold'] == 0)
    {
        return;
    }
    
    if (($severity & error_reporting()) == $severity)
    {
        $error->log_exception($severity, $message, $filepath, $line);
    }
    }
    

    我认为这会解决的。然后你可以使用

    error_reporting(E_ALL & ~E_NOTICE);
    

    在您的 index.php 中。当然,我们在这里编辑核心。也许有一种方法可以替代?

    达娜

    【讨论】:

    • 对于普通 PHP,我认为这是正确的,但我认为 Code Igniter 将所有 PHP 错误(无论是通知错误还是实际错误)视为相同,并且会全部记录或不记录。
    • 是的,我今天早上一直在玩它。尝试在服务器级别更改它,但 CI 忽略了这些。除了可能深入 CI 核心之外,看不到任何解决方法。
    • 非常感谢您的调查,我很感激。我将您的补丁应用于核心,但发现当使用 error_reporting(E_ALL & ~E_NOTICE) 时,它会在屏幕上显示通知,听起来很奇怪。我在 CI 论坛上读到有日志记录插件,所以我想我会调查那条路径。如果我成功了,我会在这里报告,以便大家受益。
    • 我在使用 E_ALL 和 ~E_NOTICE 时没有体验到日志记录到屏幕。奇怪的。是的,我很想看看你发现了什么。
    【解决方案3】:

    首先,感谢大家的思考。在考虑了您的建议后,我决定修补 CI 的核心。不幸的是,可以扩展核心类,但不能扩展核心本身。因此,如果您应用相同的补丁,请务必将其记录在案。

    来了。在 system\application\config\config.php 中,我在 log_treshold 设置下方添加了以下自定义配置设置:

    /*
    |--------------------------------------------------------------------------
    | Error Logging Exclusions (custom config addition by Ferdy Christant)
    |--------------------------------------------------------------------------
    |
    | By default, CI will log all PHP errors, whether it is a notice, warning
    | or error. Or, by setting the above treshold to 0, it will log nothing
    | In most cases, however, you will want to log PHP errors but not the notices
    | In the array below, simply place the PHP error constant that you do NOT
    | want to see logged. 
    |
    | For a live site you'll usually use the config as follow:
    |
    | $config['exclude_logging'] = array(E_STRICT,E_NOTICE);
    |
    */
    
    $config['exclude_logging'] = array(E_STRICT,E_NOTICE);
    

    正如文档所解释的,在这个配置数组中,您放置了您想要记录的 PHP 错误类型。

    接下来,我已经修补了核心文件(system/codeigniter/Common.php)并编辑了函数_exception_handler

    有两个变化。首先,我将配置加载行移到方法的顶部,因为我更早需要它。找到下面的行,你会看到 $config =& get_config();在它下面。删除那个。

    我删除了 // 我们应该记录错误吗?不?我们完成了...

    其次,检查严重性被修改为检查我们声明的数组。转到方法的顶部并将检查 $severity == E_STRICT 的 if 语句替换为以下内容:

    $config =& get_config();
    if (in_array($severity,$config['exclude_logging']))
    {
    return;
    }
    

    这些补丁允许对 PHP 错误日志进行细粒度控制。正常的 CI 日志当然仍然有效。如前所述,唯一的缺点是这会修补核心。

    我希望这对任何人都有帮助。感谢您的思考!

    【讨论】:

      【解决方案4】:

      对于可能使用 CodeIgniter 2.0 遇到此问题的任何其他人。问题还是一样,但解决方案“更简单”。

      你还是要修改一个核心文件:/system/codeigniter/Common.php

      找到_exception_handler() 函数(应该在底部),并更改这一行:

      if ($severity == E_STRICT) 致此: if ($severity == E_STRICT OR $severity == E_NOTICE)

      有趣的是他们认为E_STRICT 通知 会填满日志;但E_NOTICE 不会。或者也许惩罚那些没有严格编码并在使用它们之前声明所有变量的人是件好事? :)

      【讨论】:

      • 谢谢!我正在尝试 2.x 的所有其他东西,但都没有奏效。确实如此。
      【解决方案5】:

      您只需在 error_reporting 方法调用中添加一个下划线即可表明 PHP 不会报告这些通知:

      error_reporting(E_ERROR);
      

      Code Igniter 会将来自 PHP 的任何错误(无论是通知、警告还是致命错误等)视为 CI 日志中的错误。

      编辑:没关系,刚刚看到你的评论。不确定您的错误报告发生了什么。

      【讨论】:

      • 您的回答仍然很有趣,特别是您说“Code Igniter 会将 PHP 的任何错误(无论是通知、警告还是致命错误等)视为 CI 日志中的错误。 ”。我是疯了还是 CI 的这种非常不受欢迎的行为?这种使之成为全有或全无的交易:要么记录大量日志,要么根本不记录任何内容,甚至不记录错误。如果我错了,请纠正我,但这种方式似乎严重阻碍了 PHP 的错误报告。
      • 这是使用配置选项设置的,一切或一切都不正确。你从哪里听到的?
      • 我没有“听到”这个,我知道。我已经对其进行了测试和调试,这就是 CI 的工作原理。您谈论的配置选项控制 CI 错误,而不是 PHP 错误。它要么记录所有 PHP 错误,要么根本不记录。您可以使用该配置设置控制 CI 错误。
      【解决方案6】:

      我一直只是修改 CodeIgniter 的日志库。 Ferdy 说 CI 将记录所有内容或不记录任何内容是正确的。这是非常不受欢迎的。

      【讨论】:

        【解决方案7】:

        试试这个,无需修补 CI 核心:

        $hook['pre_controller'] = array(
            'class'    => 'MY_Commonfunction_hook',
            'function' => 'hook',
            'filename' => 'MY_Commonfunction_hook.php',
            'filepath' => 'hooks');
        
        class MY_Commonfunction_hook {
            public function hook() {
                set_error_handler('_my_exception_handler');
            }
        }
        
        function _my_exception_handler($severity, $message, $filepath, $line) {
            if ($severity == E_STRICT) {
                return;
            }
            $_error = & load_class('Exceptions', 'core');
            if (($severity & error_reporting()) == $severity) {
                for ($i = ob_get_level(); $i > 0; $i--) {
                    @ob_end_clean();
                }
                $_error->show_php_error($severity, $message, $filepath, $line);
            }
            if (config_item('log_threshold') == 0) {
                return;
            }
            if (($severity & error_reporting()) == $severity) {
                $_error->log_exception($severity, $message, $filepath, $line);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-12
          • 2018-08-04
          • 1970-01-01
          • 2013-06-02
          相关资源
          最近更新 更多