Welcome Guest, Not a member yet? Register   Sign In
Sample code for controlling error logging using PHP error bit codes. Enjoy.
#1

[eluser]Unknown[/eluser]
Well it took me a while but I think I found a way to make the CodeIgniter logging mechanism into something useful.

As built, changing the error_reporting() level has no effect on what kinds of php errors get logged. If it's a PHP error, and if you're recording PHP errors via your $config['log_threshold']... even if it's an E_NOTICE, it automatically gets written to the log file.

What this means, in practical terms, is that my logs were filling up with a bunch of inconsequential E_NOTICE messages. I'm not here to argue whether it's wise or not to log E_NOTICEs, I'm just here to offer you a way around it if you so desire.


Add a new configuration field like yea. It's the same bit-scheme as the native PHP error codes you might give to error_reporting()....

Code:
...
$config['log_threshold'] = 1;
$config['log_reporting_level'] = E_ALL ^ E_NOTICE;
...



Then, jam this in libraries...

Code:
class MY_Exceptions extends CI_Exceptions {
    
    function MY_Exceptions() {
        parent::CI_Exceptions();
    }
    
    function log_exception($severity, $message, $filepath, $line) {    
        $config =& get_config();
        $log_reporting = $config['log_reporting_level'];
        if (($severity & $log_reporting) == $severity) {
            $severityStr = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
            log_message('error', 'Severity: '.$severityStr.'  --> '.$message. ' '.$filepath.' '.$line, TRUE);
        }
    }
    
}

And that's about it. Now you can control both what gets displayed to the screen and what gets logged.


Next up... maybe I'll replace that model travesty with an actual ORM, and not one based on rails with its retarded pluralization scheme. (Man I want to strangle that guy.)
#2

[eluser]Unknown[/eluser]
I was just banging my head against the wall wondering why Notices were getting logged even though I set error_reporting(E_ALL ^ E_NOTICE).

Thanks for the work around.
#3

[eluser]Unknown[/eluser]
Worked perfectly, thanks much Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB