How to customize the access log in Apache HTTPD?

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux (RHEL)
  • Red Hat Software Collections (RHSCL)
  • Red Hat Enterprise Web Server (EWS)
  • Red Hat JBoss Web Server (JWS)
    • 3.0.x
  • Red Hat JBoss Core Services (JBCS)
  • Apache HTTPD
    • 2.2.x
    • 2.4.x

Issue

  • How to customize the acess log format?
  • How to enable friendly access log times in Apache HTTPD?

Resolution


Disclaimer: The following information has been provided by Red Hat, but is outside the scope of the posted This content is not included.Service Level Agreements and This content is not included.Scope of Coverage. Red Hat does not support software contained in the EPEL repository, or other third-party repositories, or from third-party non-Red Hat sources. The information is provided as-is without representations or warranties about the suitability or accuracy of the information provided. The intent is to provide information to accomplish the system's needs. Use of the information below is at the user's own risk.

Links contained herein to external website(s) are provided for convenience only. Red Hat has not reviewed the links and is not responsible for the content or its availability. The inclusion of any link to an external website does not imply endorsement by Red Hat of the website or their entities, products or services. You agree that Red Hat is not responsible or liable for any loss or expenses that may result due to your use of (or reliance on) the external site or content.


This solution is part of the [Master] Configuring Access Logs in Red Hat Middleware Products..

In Apache's httpd.conf search for the following lines:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
...(snip)...
CustomLog logs/access_log combined

It's possible to create a custom LogFormat to show more information like the cookies and the time the request takes to finish.

For example, add a third LogFormat:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%h %l %u %t \"%r\" %>s %b %{Referer}i %{User-Agent}i Cookie: \"%{COOKIE}i\" Set-Cookie: \"%{SET-COOKIE}o\" PID: %P TID: %{tid}P TimeTaken: %T TimeTaken(us): %D" custom

And then, update the CustomLog directive to use the "custom" format instead of combined:

CustomLog logs/access_log custom

A restart is needed for the above to be applied.

After these changes, the access logs generated will be in a format that is much easier to digest at first sight.

For more format parameters, you can check the Content from httpd.apache.org is not included.mod_log_config documentation

Also, if HTTPD is used as a balancer with mod_proxy_balancer, mod_jk, or mod_cluster it's possible to log which backend node handled the request. Refer to How to log which node handled a request from httpd?.

Components
Category
Tags

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.