Page 1 of 1

How do I easily log PHP error messages to a log file?

Posted: Fri May 02, 2008 2:47 pm
by LonelyProgrammer
Hi,

I am currently working with PayPal's IPN and some of my scripts may have run-time errors or some fatal errors; however, as it is executed by IPN, I have no idea where to debug except by littering log file entries everywhere.

Is there a way to log PHP's error messages to a log file? I know that there is set_error_handler(), but it seems too much to test just one script...

Re: How do I easily log PHP error messages to a log file?

Posted: Fri May 02, 2008 2:59 pm
by lafever
You can change it in the php.ini or maybe you could try ini_set if allowed by your host?

Code: Select all

 
ini_set ('track_errors', 1);
ini_set ('log_errors',   1);
ini_set ('error_log',    '/logs/.php_errors');
 

Re: How do I easily log PHP error messages to a log file?

Posted: Fri May 02, 2008 3:10 pm
by Verminox
If you want to make it simpler than the above log, just try:

Code: Select all

<?php
function log_error($no,$msg,$file,$line)
{
    file_put_contents('error.txt',$msg."\n",FILE_APPEND);
}
 
set_error_handler('log_error');
 
?>
You can put in an unlink() at the top if you want to clear the log before each execution.