Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 37 ยท Monitoring and Tuning

Tomcat Access Logs and Request Metrics

Configure the Tomcat AccessLogValve, choose a log pattern with response time, rotate files and compute error rates and percentile latency from access logs.

The access log has one line for every request Tomcat handled: who asked, what they asked for, what status came back, how many bytes and, if you configure it, how long it took. When a user says "the site was slow at 3 pm", the access log is where you check. It is also the cheapest source of traffic statistics, error rates and latency percentiles, with no code changes.

This lesson shows how to configure the AccessLogValve, which pattern codes are worth having, how rotation works, and how to analyze the logs. The configuration is read-only here; the parsers and statistics are runnable Java stand-ins you can adapt for a script.

The AccessLogValve

The valve is declared in the <Host> of server.xml and is enabled in the default installation:

XML
<Valve className="org.apache.catalina.valves.AccessLogValve"
       directory="logs" prefix="localhost_access_log" suffix=".txt"
       pattern="%h %l %u %t &quot;%r&quot; %s %b %D"
       rotatable="true" renameOnRotate="false" maxDays="14"/>

By default Tomcat writes a new file each day, named localhost_access_log.2026-03-14.txt. maxDays deletes old files (available in recent versions); on older versions a cron job or logrotate does it. Set buffered="true" (the default) so log lines are written in batches instead of one disk write per request.

Pattern codes

The pattern is a mix of % codes and literal text. The most useful:

Code Meaning
%h remote host (client IP, or proxy IP unless RemoteIpValve is used)
%l remote logical user name (almost always -)
%u authenticated user, or -
%t time the request was received, [14/Mar/2026:10:15:32 +0000]
%r request line: method, URI and protocol
%s HTTP status code
%b response bytes (- if zero), %B shows 0
%D processing time in milliseconds
%T processing time in seconds (whole)
%F time until the response started being committed (first byte)
%S session id (treat as sensitive)
%{Referer}i, %{User-Agent}i request headers
%{X-Request-Id}i a request id header, to correlate with application logs

Shortcuts: pattern="common" equals %h %l %u %t "%r" %s %b, and combined adds referer and user agent. Always add %D: without a time you cannot tell fast from slow. (In Tomcat 9 and 10 %D is milliseconds; in Apache httpd it is microseconds, which trips people up.)

Do not log secrets: avoid query strings with tokens, session ids and Authorization headers. If a URL carries a secret, consider keeping it in headers or POST bodies instead.

Application logging versus access logging

Access logs say what happened at the edge. The application's own logs (and catalina.out) say why. Give each request an id, add it to both (%{X-Request-Id}i here and a logging MDC in the app) and you can jump from a slow line to the exception behind it. The troubleshooting lessons show how to read the server side.

Parsing a log line

A log in a fixed pattern is easy to parse. The stand-in below reads lines in the pattern above with a regular expression and prints the pieces.

Java Stand-in: parse access log lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Computing error rate and percentiles

Averages hide trouble: a few very slow requests barely change the mean. Operators use percentiles: p50 (the median), p95 and p99. This stand-in generates a fixed set of requests and prints counts by status class, the error rate, and latency percentiles, the numbers a dashboard would show.

Java Stand-in: error rate and p95 latency

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The mean is pulled up by a few slow requests, while p50 shows what a typical request feels like and p99 shows the worst experiences. A rise in p95 with a flat p50 usually means one slow endpoint or a dependency hiccup, and the per-path table finds it.

Rotation, retention and shipping

  • Keep 7 to 30 days on the server; ship logs to a central system (an ELK stack, Loki, a cloud log service) with an agent such as Filebeat or Fluent Bit.
  • JSON output is easier to parse than text: use a pattern that builds JSON, or the ExtendedAccessLogValve for W3C format.
  • Rotate application logs (catalina.out) with logrotate using copytruncate, because Tomcat keeps the file open.
  • Watch disk space: a full log disk can stop Tomcat from writing and cause failures far from the cause.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which pattern code logs the processing time in milliseconds?

    %D is the time taken to process the request, in milliseconds in Tomcat.
  2. 2. Why look at p95 or p99 instead of only the average?

    A few very slow requests barely move the mean, but tail percentiles reveal them.
  3. 3. What does a status of 5xx in the access log indicate?

    5xx codes mean the server failed to handle a valid request.
  4. 4. Why use RemoteIpValve together with %h?

    Without it every line behind a proxy shows the proxy address.

Summary

  • The AccessLogValve writes one line per request; always include %D and a request id.
  • Do not log secrets such as tokens and session ids.
  • Analyze status classes, error rate and p50/p95/p99 latency per path rather than just means.
  • Rotate and ship logs, and keep an eye on disk space.

Next: Reading Catalina logs and stack traces