Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I to set the error log for PHP CLI?

I have a PHP script running just fine through Apache, but it fails when running in CLI, so I want to find out what is happening.

For this, I'd like to see the error log, but the CLI error log I set is not working. I have set this in the proper php.ini file, which is confirmed when I obtain the error log details through the command line:

php -i | grep error

Result:

display_errors => Off => Off
display_startup_errors => Off => Off
error_append_string => no value => no value
error_log => /var/log/php_error_log => /var/log/php_error_log
error_prepend_string => <font color=ff0000> => <font color=ff0000>
error_reporting => 30711 => 30711
html_errors => Off => Off
ignore_repeated_errors => Off => Off
log_errors => On => On
log_errors_max_len => 1024 => 1024
track_errors => Off => Off
xmlrpc_error_number => 0 => 0
xmlrpc_errors => Off => Off
suhosin.disable.display_errors => Off => Off
suhosin.sql.bailout_on_error => Off => Off

So error_log and log_errors are both set. However, no logs are actually saved. Permissions are in place. So what can it be?

I've read the question PHP CLI won't log errors, but I could not find a solution in there.

like image 497
User402841 Avatar asked Jan 21 '12 17:01

User402841


2 Answers

PHP via CLI and HTTP request both use a different php.ini configuration. In your case I am suspicious, whether you are dealing with the correct php.ini file used by CLI PHP. Try executing:

php -i >> info.txt

This will save your phpinfo() into a local file, info.txt. Then open the file and find the setting "Loaded Configuration File" - which should point to the absolute path to the loaded php.ini file.

If this is all OK, then simply run the following CLI command to test whether function error_log works as expected. Execute via CLI:

php -r "error_log('test error', 3, './errors.log');"

And you should find in the same directory the file errors.log with the specified test error. If you cannot find it then you probably need to update your php.ini settings or set the correct write permissions, process or file owner.

like image 199
lubosdz Avatar answered Oct 13 '22 13:10

lubosdz


The logfile is probably already created by the PHP interpreter (mod_php or php_fpm, etc.) with a different user (www-data?). Therefore if you run your script as anyone else it may not have write access to the file or may not even have access to the path. You may try running it as a different user

sudo -u www-data php your_script.php
like image 23
Tomáš Fejfar Avatar answered Oct 13 '22 13:10

Tomáš Fejfar