Business Websites, Ecommerce solution, Process Automation!

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


You can use the following HTML tags: <a><br><strong><b><em><i><blockquote><code><ul><ol><li><del>


CAPTCHA Image
Reload Image

01

Build YourWebsites

>>
Showcase your business online! Whether you need a website for your associations,business, ecommerce or affiliate marketing Call us Now
02

MaintainWeb Assets

Maintain Your Web Assets To ensure optimal performance and 99.9% uptime.We help you fix issues,on-page SEO optimization
03

Build NewAdd-ons

>>
We help our Clients to add new features: Additional pages, add payment gateway, load products to your store or build a new e-commerce store.