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

by Vivek Gite · 15 comments

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!

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 1 trackback }

how to log php error in a file
01.21.09 at 3:26 pm

{ 14 comments… read them below or add one }

1 Anjanesh 07.30.07 at 3:22 am

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 07.30.07 at 7:26 am

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

3 vivek 07.30.07 at 9:27 pm

@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 01.22.08 at 11:48 am

Small misspelling in title: “log fiie”

5 vivek 01.22.08 at 12:04 pm

Thanks for the heads up!

6 China Landscape 05.09.08 at 9:19 am

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

How can I get it in this case ?

7 Carl 10.09.08 at 3:06 pm

“yes I did assumed that Apache or lighttpd has a write permissions”

In the case that it doesn’t, how can this be changed?

8 vivek 10.09.08 at 3:13 pm

Carl,

Use chmod and chown command to set permissions.

9 Kurt 10.24.08 at 7:31 am

Hellow,

Is it possible to log errors of diffrent websites in different errorfiles?

I have a server that’s hosting different websites and I want that all of the php-errors are logged in different file. For each website a file.

Is that possible and how do I do it?

tnx in advance

10 vivek 10.24.08 at 11:28 am

Kurt,

You need to setup php as FastCGI which allows the -c option. With this option you can provide custom php.ini file and error log file for each user. See this article for more info. This is lighttpd specific but you should able to setup with Apache or any other server with mod_fastcgi.

HTH

11 Carl 10.24.08 at 7:37 pm

@Kurt
You might try ini_set() as well if you have trouble getting custom php.ini files to work

12 Svetainiu Kurimas 10.19.09 at 4:24 pm

Hi, does the init_set solution also work with Apache installed on Windows? It doe snot seem to be working for me.

13 Liju 12.23.09 at 5:08 am

I think it would need to enable this tag (log_errors = On) php.ini to get this complete.

But I suggest it’s not a good Idea to enable on the main configuration file where a server hosted multiple sites. Just add this entries on .htaccess make sense on such case to trouble shoot.

14 lcd 01.25.10 at 4:09 pm

You have great blog and this post is good!

Leave a Comment

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

Previous post:

Next post: