PHP currently will not log errors produced from the command line.
I have :
log_errors = On
error_log = /var/log/php_errors.log
in /etc/php5/cli/php.ini
Am I missing a further setting to get this working?
PHP currently will not log errors produced from the command line.
I have :
log_errors = On
error_log = /var/log/php_errors.log
in /etc/php5/cli/php.ini
Am I missing a further setting to get this working?
If you can't figure out what why or perhaps don't have user permissions on php.ini, another way to debug a no-information parse error is to wrap your php source in another php source with body something like:
This question and answer thread was very helpful to me while setting up PHP CLI logging on an Ubuntu 12.04 environment, so I wanted to post an answer that distills what I learned. In addition to the great info provided by
David Chan
as well asGeorge Cummins
I have created alogrotate.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:Since the server in question is used by web developers working on various projects, I have setup a common group for them called
www-users
. And in this case, I want thephp_errors.log
to be readable and writable bywww-users
I change the ownership of the file like this:And then change the permissions of the file to this:
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:Happily
log_errors
is enabled by default in Ubuntu 12.04:But to allow logging to a file we need to change the
error_log
to match the new file like this: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 thephp_errors.log
. Create a file calledphp-cli
in/etc/logrotate.d/
like this:And place the contents of this log rotate daemon script in there:
Testing the setup.
With that done, let’s test the setup using
David Chan
’s tip above: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:And there should be a timestamped error line in there that looks something like this:
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:
The logging/reporting behaviour of PHP is dependant 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:
as a diagnostic you can attempt to force a write to the error log this way.
you should now see test 123 in your log
in PHP file
in terminal
output You messed up!