views:

37

answers:

2

Hi I am trying to do error logging in php codeigniter, I want to to know some bascis of error logging, 1. what are all the steps to be fallowed to log an error, 2. How an error logging file is created, 3. How to push the error message into log file( Incase, any error occurs) 4. How to do e-mail that error to the destination that we need to mail

A: 

To simply put a line in the server's error log, use PHP's error_log() function. However, that method will not send an e-mail.

First, to trigger an error:

trigger_error("Error message here", E_USER_ERROR);

By default, this will go in the server's error log file. See the ErrorLog directive for Apache. To set your own log file:

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

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

To e-mail the error, you need to set up a custom 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('[email protected]', 'I have an error', $message);
}
set_error_handler('mail_error', E_ALL^E_NOTICE);

Please see the relevant PHP documentation for more info.

Mark Eirich
+1  A: 

Codeigniter has some error logging functions built in.

Make your logs folder writable and use log_message();

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

Keyo