Albert Capp
Albert Capp

Reputation: 41

PHP - error_log() will not print to file

Title pretty much says it all. I've been tearing my hair out trying to get this working all day. I'm in the process of creating a PHP-based login system and I need to do some debugging. The most useful thing to me right now would be the ability to write debugging messages to a file at certain points throughout a PHP program. Based on the documentation, it looks like this is what error_log() is supposed to do, but despite everything I've tried, I have had absolutely no success. Full list of everything I've tried below:

I really can't find anthing else to try on Google, so I figured I'd ask the experts, and here I am. If anyone can provide any suggestions, it would be greatly appreciated.

Upvotes: 4

Views: 2093

Answers (1)

Robert
Robert

Reputation: 20286

I guess the logs are written to sys logs because in error_log() you didn't provide the destination.

Try the following code

error_log("An error occured", 3, "/var/tmp/my-errors.log");

Be sure this file can be read by php either fpm or www-data depending on your configuration you can create it before with touch and add permissions manually with chmod

3 means that destination is a file

If you use apache then check in httpd.conf or any other place(v where it may be present the location of ErrorLog

ErrorLog "/var/log/apache2"

Then check if this file has following user group with ls -la ll etc.

 -rwxrwxr-x 1 www-data www-data 

Something like this should appear.

The other option is to set following in proper php.ini (CLI and apache have different php.ini files)

 error_log = /var/log/phperrors.log 

then

 touch /var/log/phperrors.log
 chown www-data: /var/log/phperrors.log
 chmod +rw /var/log/phperrors.log

There are no miracles but if it still doesn't work you can write and register your own error handler with set_error_handler() you can find examples how to do it in php manual. It's more like hack but it will work for sure. If it won't then it means that errors are not triggered at all then you should look if you edit the correct php.ini or use ini_set() before error is triggered.

Upvotes: 2

Related Questions