2

I develop a local script quick & dirty and let it run on the command line. I have no server installed.

My php ini lies at /usr/local/etc/php/7.4/php.ini

I activated the error log by using

error_reporting = E_ALL
display_errors = On
log_errors = On
error_log = /var/log/php_errors.log

I saved the changes but don't find a log written at /var/log/php_errors.log

What do I have to do to get my errors logged in a file?

Output of

$ php --ini
Configuration File (php.ini) Path: /usr/local/etc/php/7.4
Loaded Configuration File:         /usr/local/etc/php/7.4/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.4/conf.d
Additional .ini files parsed:      /usr/local/etc/php/7.4/conf.d/ext-opcache.ini


$ which php
/usr/local/bin/php
Calamity Jane
  • 1,557
  • 4
  • 21
  • 41
  • check `/var/log/apache2/error.log` or `/var/log/apache2/access.log` – aviboy2006 Sep 11 '20 at 10:12
  • 1
    @aviboy2006 I think you didnt read all the question. See sentence 2 `I have no server installed` – RiggsFolly Sep 11 '20 at 10:16
  • 3
    First check that the PHP CLI is using that ini file by doing `php --ini`. That will show you the path to the `Loaded Configuration File:` – RiggsFolly Sep 11 '20 at 10:17
  • mac comes with default Apache2 server configuration. I said by @RiggsFolly check are you changing right php.ini file or not. Because whenever php cli command its reload php.ini configuration changes. – aviboy2006 Sep 11 '20 at 10:19
  • 2
    @aviboy2006 Ah yes, but I hope, unlike Windows 10 and IIS it is not automatically started unless you actually ask it to be :) – RiggsFolly Sep 11 '20 at 10:22
  • for the location, you seem to be editing the 'brewed' php ini files. Are you certain you are actually running `that` php ? At terminal, `which php` will reveal the actual path to the executed version... make certain you are running the one whose ini files you are editing. Typically, the logs would be in `/usr/local/var/log/*.log` – YvesLeBorg Sep 11 '20 at 10:37
  • @YvesLeBorg you are correct that there are logs tail -f /usr/local/var/log/php-fpm.log but their last entry is from 15th August, that is last month. So they are clearly not used by my script. – Calamity Jane Sep 11 '20 at 10:49

1 Answers1

0

My understanding is that the php cli outputs errors to the command line in much the same way as a bash script. Therefore you can output your errors to your own log file in the same way.

Here is a simple script file error_test.php which which has two commands. First echoes a string - second echoes an undefined variable which will create an error:

<?php

echo "hello\n";
echo $test;

run the script as

php error_test.php

and it will output the following:

hello
PHP Notice:  Undefined variable: test in /home/phi/Projects/sandbox/error_test.php on line 3

Like a bash script, we can redirect the stderr output to a file with 2>

php error_test.php 2> error.log
cat error.log

This replaces the log file each time the command is run. To append the errors to the log file use 2>>.

php error_test.php 2>> error.log

To ignore errors, redirect to /dev/null

php error_test.php 2>> /dev/null
phi ARCHITECT
  • 59
  • 1
  • 3