How to enable access log in Apache Tomcat?

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux (RHEL)
  • Red Hat Enterprise Web Server (EWS)
  • Red Hat JBoss Web Server (JWS)
  • Apache Tomcat
    • 6.x
    • 7.x
    • 8.x
    • 9.x
    • 10.x

Issue

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

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..

To enable access logs in Tomcat, locate the following piece of code in Tomcat's server.xml file.

<!--
<Valve className="org.apache.catalina.valves.AccessLogValve"
       prefix="localhost_access_log." suffix=".log"
       pattern="common" directory="${jboss.server.home.dir}/log"
       resolveHosts="false" />
-->

Remove the comment tag <!-- and -->and change the pattern="common" to the following pattern:

<Valve className="org.apache.catalina.valves.AccessLogValve"
       prefix="localhost_access_log." suffix=".log"
       pattern="%h %l %u %t %r %s %b Cookie: %{COOKIE}i Set-Cookie: %{SET-COOKIE}o Thread: %I TimeTaken: %T"
       directory="${jboss.server.home.dir}/log"
       resolveHosts="false" />

Daily log rotation is enabled by default. If deleting access logs is a requirement, add the maxDays attribute to indicate how many days are to be kept. This attribute is added in Content from bz.apache.org is not included.Tomcat 7.0.76+ so it would be available and supported on RHEL 8+ or JWS 5+ provided Tomcat distributions.

For more information about the available attributes on the AccessLogValve, refer to Tomcat's documentation:

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.