0

I'm using Apache 2 + PHP 8, and have PHP warnings and errors written in Apache error log files. Here is an example of logs being written:

[Wed Aug 10 08:15:54.502158 2022] [php:warn] [pid 15437] [client 64.124.8.31:24644] PHP Warning: Constant ABSPATH already defined in /var/www/html/wp-config.php on line 43

It is difficult to troubleshoot as I don't know the PHP file which caused the warning / error.

I would like to have something like:

[Wed Aug 10 08:15:54.502158 2022] [php:warn] [pid 15437] [client 64.124.8.31:24644] [/path/to/myFileName.php] PHP Warning: Constant ABSPATH already defined in /var/www/html/wp-config.php on line 43

Would anybody know how to configure this?

Thanks for your help.

1 Answers1

0

Apache httpd allows you to modify the error log message format by defining a custom ErrorLogFormat.

AFAIK Unfortunately that doesn't offer you exactly what you would like, but what you can do though is:

Include a %L in your ErrorLogFormat and your error log will contain an unique "Log ID" of the request.

Adjust the LogFormat used for your access_log to include a similar %L and log that same unique "Log ID" from the error log there (or '-' if nothing has been logged to the error log for this request).

Use that to correlate errors with a specific request (and file).

A more advanced config would be something along the lines of:

    ErrorLogFormat "[%{cu}t] [%-m:%-l] [R:%L] [C:%{C}L] %M"<br/>
    ErrorLogFormat request "[%{cu}t] [R:%L] Request %k on C:%{c}L pid:%P tid:%T"<br/>
    ErrorLogFormat request "[%{cu}t] [R:%L] UA:'%+{User-Agent}i'"<br/>
    ErrorLogFormat request "[%{cu}t] [R:%L] Referer:'%+{Referer}i'"<br/>
    ErrorLogFormat connection "[%{cu}t] [C:%{c}L] local\ %a remote\ %A"<br/>

    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\" [R:%L] [F:%f]" ourlogformat

    CustomLog "log/our_access_log" ourlogformat
    ErrorLog "log/our_error_log"
HBruijn
  • 77,029
  • 24
  • 135
  • 201