Code Igniter logging too much


In my CI config file I have this logging treshold set:

$config['log_threshold'] = 1;

In index.php, I have set the following error reporting:


My expectation is that this will log any CI errors that I log (using log_message('error','my error message')), as well as any PHP errors. However, I would expect that it will not log PHP notices, only errors. However, when I look at the log files, it seems to log PHP notices too:

ERROR - 2009-12-18 13:21:50—> Severity: Notice —> Undefined variable: pageindex /var/www/apps/OS4W/system/application/views/user/view.php 12
ERROR - 2009-12-18 13:21:50—> Severity: Notice —> Undefined variable: friendsmode /var/www/apps/OS4W/system/application/views/user/activitytable.php 207

Although the log lines start with “ERROR”, in reality this seems to be a PHP notice, kind of like a warning, that I do not want to log. How can I make sure only CI and PHP errors are logged, yet not PHP notices? I thought error_reporting(E_ERROR) would do just that?

6/16/2012 8:35:10 AM

Accepted Answer

First of all, thank you all for thinking along. After considering your advise, I decided to patch the core of CI. Unfortunately, the core classes can be extended, but not the core itself. Therefore if you apply the same patch, be sure to document it.

Here goes. In system\application\config\config.php I added the following custom config setting right below the log_treshold setting:

| 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);

As the documentation explains, in this config array you put the PHP error types that you do NOT want to log.

Next, I have patched the core file (system/codeigniter/Common.php) and edited the function _exception_handler

There are two changes. First, I moved the config loading line to the top of the method, since I need it earlier. Find the line below and you will see $config =& get_config(); under it. Delete that.

I removed the // Should we log the error? No? We're done...

Second, the check for severity is modified to check for the array that we declared. Go to the top of the method and replace the if statement that checks $severity == E_STRICT with below:

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

These patches allow for fine-grained control over PHP error logging. Normal CI logging will of course still work. As mentioned, the only downside is that this patches the core.

I hope this helps anyone. Thank you for thinking along!

4/8/2012 12:54:56 AM

For anyone else that might come across this with CodeIgniter 2.0. The problem is still the same, but the solution is "easier".

You still have to modify a core file: /system/codeigniter/Common.php

Find the _exception_handler() function (should be at the bottom), and change this line:

if ($severity == E_STRICT) to this: if ($severity == E_STRICT OR $severity == E_NOTICE)

Funny how they think E_STRICT notices will fill up the log; but E_NOTICE won't. Or maybe it's good to punish people for not coding strictly and declaring all their variables before using them? :)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow