How to Log errors in Codeigniter
Posted in Codeigniter | 9th February, 2019
Tags.
You do not have to build separate functions in codeignter to log errors in you rapplications.
Codeignter has an error logging function in the file system\core\common.php:
Code Ignter Error function
function log_message($level, $message)
{
static $_log;
if ($_log === NULL)
{
// references cannot be directly assigned to static variables, so we use an array
$_log[0] =& load_class('Log', 'core');
}
$_log[0]->write_log($level, $message);
}
This function does not work by default. You must enable logging in your config file to be able to activate this.
Codeignter Error Logging activation:
You can enable error logging by setting a threshold over zero. The
| threshold determines what gets logged. Threshold options are:
|
| 0 = Disables logging, Error logging TURNED OFF
| 1 = Error Messages (including PHP errors)
| 2 = Debug Messages
| 3 = Informational Messages
| 4 = All Messages
|
| You can also pass an array with threshold levels to show individual error types
|
| array(2) = Debug Messages, without Error Messages
|
For a live site you'll usually only enable Errors (1) to be logged otherwise your log files will fill up very fast.
In your functions you will call them like this:
To logo DB error messages:
log_message('error', print_r($this->db->error(), true));
To Log customer message:
log_message('error', "My Message');
Post a comment