CodeIgniter Logging Library

June 3rd, 2008

CodeIgniter (CI) has a very good logging class but there is one thing in the class that I am not to thrilled with, The way it writes the logs. CI logging has 5 levels;

0 = Disables logging, Error logging TURNED OFF
1 = Error Messages (including PHP errors)
2 = Debug Messages
3 = Informational Messages
4 = All Messages

In the configuration file if you set the level at 3 you will get info messages (messages in the logs that you create) and everything that is less than 3. So you will get info, debug, and error messages. This will fill up your log files quite rapidly. On a project I am working on now one of the requirements is to log usage in log files. So I had to modify the logging class to log only Info messages and not everything less than what you set the threshold to. This was quite easy. I am not sure how many people want to do this but here is how you would go about modifying it in your code.

The file to edit is located in CI -> system -> libraries -> Log.php

On line 87 or around line 87 (give or take a line or two) you should find this line of code

if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))

Replace it with

if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] != $this->_threshold))

That’s It! You should now only get the Info Messages or what ever your threshold is set to in your config file. That configuration file is located in CI -> system -> application -> config -> config.php

Look for the section with the heading of “Error Logging Threshold”

As always if you have any question leave a comment.


Leave a Reply