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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

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

Post 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...
User avatar
lafever
Forum Commoner
Posts: 99
Joined: Sat Apr 05, 2008 2:03 pm
Location: Taylor, MI

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

Post 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');
 
Last edited by lafever on Fri May 02, 2008 3:10 pm, edited 1 time in total.
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

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

Post 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.
Post Reply