Polls

Topics

PHP Log All Errors to a Log File to Get Detailed Information

Posted by Vivek on Sunday July 29, 07 @8:11 pm

PHP offers simple but effective solution to log all errors to a log fiie.
On all production web server you must turn off displaying error to end users via a web browser. Remember PHP gives out lots of information about path, database schema and all other sort of sensitive information. You are strongly advised to use error logging in place of error displaying on production web sites. The idea is quite simple -only developer should see php error log.

How do I log all php errors to a log fiie?

Just add following line to /etc/php.ini to log errors to specified file – /var/log/php-scripts.log
# vi /etc/php.ini
Modify error_log directive
error_log = /var/log/php-scripts.log

Make sure display_errors set to Off (no errors to end users)
display_errors = Off

Save and close the file. Restart web server:
# /etc/init.d/httpd restart

How do I log errors to syslog or Windows Server Event Log?

Modify error_log as follows :
error_log = syslog

How do I see logs?

Login using ssh or download a log file /var/log/php-scripts.log using sftp:
$ sudo tail -f /var/log/php-scripts.log

Updated for accuracy!

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or full RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in...

Discussion on This Article:

  1. Anjanesh Says:

    It would be much better to have a single config php-file and define them there instead.

    ini_set("log_errors" , "1");
    ini_set("error_log" , "Errors.log.txt");
    ini_set("display_errors" , "0");

  2. Philipp Says:

    You have to watch, that the logfile is writeable by the web-server process!

  3. vivek Says:

    @Anjanesh, good point, user can set those from a php script itself.

    @Philipp, yes I did assumed that Apache or lighttpd has a write permissions

    Appreciate your posts!

  4. Alekc Says:

    Small misspelling in title: “log fiie”

  5. vivek Says:

    Thanks for the heads up!

  6. China Landscape Says:

    I have ligttpd (PHP FastCGI) and I cannot see the log.

    How can I get it in this case ?

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , ~ Last updated on: April 14, 2008

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.