How to register errors in CodeIgniter (PHP)

I want to log errors in PHP CodeIgniter. How to enable error logging?

I have a few questions:

  • What are all the steps to register an error?
  • How is an error log file created?
  • How to output an error message to a log file (whenever an error occurs)?
  • How do you email this error to an email address?
+59
php logging codeigniter error-handling
Jul 09 '10 at 4:21
source share
4 answers

CodeIgniter has some built-in error logging features.

  • Make your / application / logs folder writable
  • In /application/config/config.php set $config['log_threshold'] = 1;
    or use a larger number, depending on how many details you want in your magazines.
  • Use log_message('error', 'Some variable did not contain a value.');
  • To send an email, you need to extend the main method of the CI_Exceptions log_exceptions() class. You can do it yourself or use this . More on kernel extension here

See http://www.codeigniter.com/user_guide/general/errors.html

+112
Jul 09 '10 at 6:23
source share

To simply put a line in the server error log, use the PHP function error_log (). However, this method will not send an email.

First, to cause an error:

 trigger_error("Error message here", E_USER_ERROR); 

By default, this will be displayed in the server error log file. See the ErrorLog directive for Apache. To install your own log file:

 ini_set('error_log', 'path/to/log/file'); 

Note that the log file you select must already exist and be a rewritable server process. The easiest way to make a file writable is to make the server owner the owner of the file. (The server user may be nobody, _www, apache, or something else, depending on your OS distribution.)

To send an email, you need to configure your own error handler:

 function mail_error($errno, $errstr, $errfile, $errline) { $message = "[Error $errno] $errstr - Error on line $errline in file $errfile"; error_log($message); // writes the error to the log file mail('you@yourdomain.com', 'I have an error', $message); } set_error_handler('mail_error', E_ALL^E_NOTICE); 

See the relevant PHP documentation for more information.

+17
Jul 09 '10 at 5:22
source share

Also make sure that you enable codeigniter to log the type of messages you want in the configuration file.

ie $config['log_threshold'] = [log_level ranges 0-4];

+2
Jan 30 '16 at 13:43
source share

More about question 4. How do you send this error by email to an email address? The error_log function also has an email address. http://php.net/manual/en/function.error-log.php

Yeah, here I found an example showing usage. Send error message via email using error_log ()

 error_log($this->_errorMsg, 1, ADMIN_MAIL, "Content-Type: text/html; charset=utf8\r\nFrom: ".MAIL_ERR_FROM."\r\nTo: ".ADMIN_MAIL); 
0
Jan 22 '17 at 13:16
source share



All Articles