48

PHP currently will not log errors produced from the command line.

I have:

log_errors = On
error_log = /var/log/php_errors.log

in file /etc/php5/cli/php.ini.

Am I missing a further setting to get this working?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bcmcfc
  • 25,966
  • 29
  • 109
  • 181
  • for me, on centos, using php7.0 from remi repo, the issue was with `error_log = php_errors.log` in config. `error_log = /var/log/php_errors.log` was the solution. – Maxim Mazurok Jul 04 '17 at 21:24

6 Answers6

50

Please check that the user account running PHP CLI has write access to /var/log/php_errors.log.

Additionally, you can verify that you are using the correct php.ini file like this:

php -a -c /etc/php5/cli/php.ini
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
George Cummins
  • 28,485
  • 8
  • 71
  • 90
  • if nothing happens when I run that command does it mean CLI isn't using that php.ini file? – bcmcfc Jun 17 '11 at 15:15
  • 2
    I'm not sure what you mean by "nothing happens," but you can determine what config file you are using by using phpinfo() and searching for "Loaded Configuration File" – George Cummins Jun 17 '11 at 15:18
  • Additionally, remember to use `-a`, which I left out of the original post (now fixed). – George Cummins Jun 17 '11 at 15:18
  • I meant it literally; it must have been due to the missing `-a`. So if it's using the right config file, which it is, and still nothing is being written to the log, then what? – bcmcfc Jun 17 '11 at 15:50
  • Back to the first suggestion: Does your user account have write access to /var/log/php_errors.log? – George Cummins Jun 17 '11 at 16:34
  • Yes, and I alternate the testing between that location and /tmp to make sure as it ought to be able to write to /tmp at the very least! – bcmcfc Jun 17 '11 at 16:44
  • What is the value of error_reporting? Default is "E_ALL & ~E_NOTICE". – George Cummins Jun 17 '11 at 16:46
  • 1
    After reading from a few different sources and testing locally, I am starting to get the sneaky suspicion that PHP CLI doesn't use the log file, regardless of the directives. Instead, it sends output only to stderr. You can force the issue like this: $`php -a >> /var/log/php_errors.log 2>&1`. Will that accomplish what you need? – George Cummins Jun 17 '11 at 16:57
48

This question and answer thread was very helpful to me while setting up PHP CLI logging on an Ubuntu 12.04 (Precise Pangolin) environment, so I wanted to post an answer that distills what I learned. In addition to the great information provided by David Chan as well as George Cummins, I have created a logrotate.d script to ensure the PHP CLI error log doesn’t grow out of control as well as set this up so multiple users will be able to log errors to the common PHP CLI error log.

First, the default behavior of the PHP CLI is to log error messages to standard output; logging to a file is not default behavior. Which usually means logging to the same command line terminal session that is running the PHP CLI command. While the PHP ini file does have accommodations for a specified error_log additional accommodations need to be made to truly make it work.

First, I had to create an initial php_errors.log file:

sudo touch /var/log/php_errors.log

Since the server in question is used by web developers working on various projects, I have set up a common group for them called www-users. And in this case, I want the php_errors.log to be readable and writable by www-users I change the ownership of the file like this:

sudo chown root:www-users /var/log/php_errors.log

And then change the permissions of the file to this:

sudo chmod 664 /var/log/php_errors.log

Yes, from a security standpoint having a log file readable and writable by anyone in www-users is not so great. But this is a controlled shared work environment. So I trust the users to respect things like this. And besides, when PHP is run from the CLI, any user who can do that will need write access to the logs anyway to even get a log written.

Next, go into /etc/php5/cli/php.ini to adjust the default Ubuntu 12.04 settings to match this new log file:

sudo nano /etc/php5/cli/php.ini

Happily log_errors is enabled by default in Ubuntu 12.04:

log_errors = On

But to allow logging to a file we need to change the error_log to match the new file like this:

error_log = /var/log/php_errors.log

Setup a logrotate.d script.

Now that should be it, but since I don’t want logs to run out of control I set a logrotate.d for the php_errors.log. Create a file called php-cli in /etc/logrotate.d/ like this:

sudo nano /etc/logrotate.d/php-cli

And place the contents of this log rotate daemon script in there:

/var/log/php_errors.log {
        weekly
        missingok
        rotate 13
        compress
        delaycompress
        copytruncate
        notifempty
        create 664 root www-users
        sharedscripts
}

Testing the setup.

With that done, let’s test the setup using David Chan’s tip:

php -r "error_log('This is an error test that we hope works.');"

If that ran correctly, you should just be bounced back to an empty command prompt since PHP CLI errors are no longer being sent to standard output. So check the actual php_errors.log for the test error message like this:

tail -n 10 /var/log/php_errors.log

And there should be a timestamped error line in there that looks something like this:

[23-Jul-2014 16:04:56 UTC] This is an error test that we hope works.
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • 5
    This answer combines the context of what is happening, suggest specific configuration changes and how to make them, solidifies the logging w/ a log rotation and tests the configuration. Some of it was inspired by other comments, but this is wrapped up perfectly in one place. Thanks @JakeGould. – Matthew Poer Feb 02 '18 at 07:27
17

As a diagnostic, you can attempt to force a write to the error log this way:

php -c /etc/php5/cli/php.ini -r " error_log('test 123'); "

You should now see test 123 in your log:

tail /var/log/php_errors.log
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Chan
  • 7,347
  • 1
  • 28
  • 49
5

The logging/reporting behaviour of PHP is dependent on error_reporting too.

Some PHP frameworks (for example, CodeIgniter) execute an error_reporting(E_STRICT) statement or equivalent, when in production mode, which will severely reduce the number/kind of logged errors.

If you want to debug something, then you can just put the following statement right before your code:

error_reporting(E_ALL);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dangelov
  • 701
  • 6
  • 14
3

If you can't figure out what why or perhaps don't have user permissions on file php.ini, another way to debug a no-information parse error is to wrap your PHP source in another PHP source file with a body something like:

ini_set('display_errors', 1);
error_reporting(E_ALL);

include "mybustedfile.php";
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Charlie
  • 128
  • 5
2

In the PHP file

error_log("You messed up!", 3, "/var/tmp/my-errors.log");

In the terminal

tail -f /var/tmp/my-errors.log

Output

You messed up!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saurabh Chandra Patel
  • 12,712
  • 6
  • 88
  • 78