We are getting "Allowed memory exhausted with PHP" where PHP is asking for Gigabytes of memory. How can I format the Apache error log to see which URL it was accessing ?


taken from the debian apache2.conf default file:

# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
# If you are behind a reverse proxy, you might want to change %h into %{X-Forwarded-For}i
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

See Apache 2 Manual for the meaning of the different format signs % or Apache 2 Manual ErrorlogFormat Directive for having the error log in a specific format since apache 2.4 aswell.

in short :

Define the output format in your main config file and the output file for example per vhost in the VirtualHost Directive. You can define the config file for all your sites in the main file aswell, if you want to.

something like would be needed to add (in case its not there yet):

    LogLevel warn

    CustomLog /var/log/apache2/access.log combined
    ErrorLog /var/log/apache2/error.log

Depending on your exact setup you might want to change the location slightly.

Dont forgot to logrotate them if you have a high traffic site.

Normally this should be (at least in debian) the default setting for how i remember it.

If you have empty logs you might want to check the permissions of the folder/files so that the user the apache2 process runs under can access and write to them.

Remember a reload of the process is (for what i know) needed for rereading the configuration files.

  • 1
    Also discovered Apache 2.4 has "ErrorLogFormat" directive to specifically change it for ErrorLog. – Rishav May 27 '14 at 8:48
  • good to know, thanks for the info, i changed the answer to include your comment. – Dennis Nolte May 27 '14 at 8:55
  • I don't see how this helps when looking at the error log. You'd have to match info from the error log, like IP and time, to the access log to figure out what URL was requested. – jla May 5 '16 at 21:33

The accepted answer does not answer the original question as it logs to access.log and not to error.log

you need to put the url in an environment variable and then call it from ErrorLogFormat like this:

SetEnvIf Request_URI "(^.*$)" RURI=$1
ErrorLogFormat "%{cu}t %a %l %M URI:%{RURI}e"
  • I tried to add this to apache2.conf and configtest sais its bogus. Invalid command 'ErrorLogFormat', perhaps misspelled or defined by a module not included in the server configuration Action 'configtest' failed. – user1888089 Mar 25 '16 at 14:41
  • @eichertc ErrorLogFormat is >= Apache 2.4, you probably have 2.2 – Rudie Apr 10 '16 at 9:22

I got the same issue. When you look at your error log and want to know which URL triggered it.

  1. ErrorLogFormat is only available in Apache 2.4

  2. The log token for custom log and error log are different, you cannot simply use the custom log token and put it in ErrorLogFormat

    ErrorLogFormat token: https://httpd.apache.org/docs/2.4/mod/core.html#errorlogformat

    CustomLog token: https://httpd.apache.org/docs/2.4/mod/mod_log_config.html#formats

  3. It maybe possible to use variable and put it in ErrorLogFormat, but for my own testing I couldn't make it to work.

    SetEnvIf Request_URI "(^.*$)" RURI=$1
    SetEnvIf QUERY_STRING "(^.*$)" QSTRING=$1
    ErrorLogFormat "%t [URI:%{RURI}e] [QSTRING:%{QSTRING}e] %M"
  1. At last, it turns out that the "correct" way is to use the token LogID and reference it from CustomLog
    LogFormat "[LogID: %L] %t \"%r\" %h" custom-with-id
    ErrorLogFormat "[LogID: %L] %t %M"

Hope it helps

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.