Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 37 ยท Configuration

Logging and Access Logs in Tomcat

Understand Tomcat's log files, JULI configuration in logging.properties, log levels and rotation, application logging, and the AccessLogValve pattern with a Java formatter stand-in.

Logs are how a server tells you what happened. Tomcat writes several kinds: server logs about its own life (startup, deployment, errors), application logs from your code, and access logs with one line per HTTP request. Knowing which file holds what, how to change the detail level and how to keep the files from filling the disk is part of operating Tomcat. This lesson covers all three kinds.

The configuration files are shown read-only. The runnable parts use java.util.logging (the API underneath Tomcat's own logger) and a formatter that builds access-log lines with the same pattern codes as the real valve.

The log files

In CATALINA_BASE/logs:

File Content
catalina.YYYY-MM-DD.log Tomcat's own messages: startup, shutdown, connector and deployment events.
localhost.YYYY-MM-DD.log Messages about the applications of the host localhost: context startup problems, uncaught exceptions, ServletContext.log output.
localhost_access_log.YYYY-MM-DD.txt The access log: one line per request.
manager.*.log, host-manager.*.log Logs of the Manager applications.
catalina.out Everything written to standard output and error (System.out, printStackTrace, JVM messages) when started with catalina.sh start. It is not rotated by Tomcat.

Three kinds of logs

Server catalina and localhost logs
Application What your code logs
Access One line per request

catalina.out collects console output and can grow forever.

1 / 2

JULI and logging.properties

Tomcat uses JULI, a fork of java.util.logging, configured in conf/logging.properties. The file defines handlers (where to write), levels (how much) and formatters:

PROPERTIES
handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

.handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

1catalina.org.apache.juli.AsyncFileHandler.level = FINE
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
1catalina.org.apache.juli.AsyncFileHandler.maxDays = 90

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter

# Per-package levels
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.AsyncFileHandler
org.apache.tomcat.util.net.level = WARNING
  • The numeric prefix (1catalina, 2localhost) lets several handlers of the same class exist with different settings.
  • Levels from most to least detail: FINEST, FINER, FINE, CONFIG, INFO, WARNING, SEVERE. Tomcat's OFF and ALL also exist.
  • maxDays deletes old rotated files (a Tomcat addition); files rotate daily.
  • AsyncFileHandler writes on a background thread so logging does not slow requests.

To debug a connector or authentication problem, raise the level of one package temporarily (for example org.apache.catalina.authenticator.level = FINE), reproduce, and set it back. Never leave FINEST on globally in production.

A common request is: "how do I stop catalina.out growing?" Options: rotate it with logrotate using copytruncate; run Tomcat with CATALINA_OUT=/dev/null once your application logs to files (not recommended while debugging); or run under systemd, which captures output into the journal. In containers, log to the console and let the platform collect it.

Logging from your application

Applications should use a logging API (SLF4J with Logback or Log4j 2, or java.util.logging), never System.out. If your WAR bundles Logback or Log4j with a config file, its output goes where your configuration says; Tomcat's logging.properties governs only Tomcat's own messages and JUL-based logging from your app.

Two important details:

  • Libraries in WEB-INF/lib have their own logging classes (class-loader isolation), so two apps can log differently.
  • A servlet can also call getServletContext().log("message"), which writes into the localhost log.

This program configures java.util.logging the way a small web app might, with a custom single-line formatter and a handler that collects records in memory, so you can see levels filter messages.

Java Levels, handlers and formatters with java.util.logging

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 same record appears in both handlers if its level passes each handler's threshold. That is how Tomcat sends detailed output to files and only important output to the console.

The access log valve

The AccessLogValve records every request after it completes. It is nested in an Engine, Host or Context (usually the Host):

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="30"
       requestAttributesEnabled="true" />

The pattern uses these codes (the same shape as Apache httpd's):

Code Meaning
%h Remote host (client IP, or the proxy unless RemoteIpValve rewrote it).
%l Remote logical user name (always -).
%u Authenticated user, or -.
%t Time the request arrived, [10/Oct/2025:13:55:36 +0000].
%r Request line, GET /shop/cart HTTP/1.1.
%s HTTP status code.
%b Bytes sent (- when zero), %B always a number.
%D Time to process in milliseconds (%T is seconds).
%{Referer}i, %{User-Agent}i A request header.
%S Session id (avoid logging it; it is a credential).

The named patterns common (%h %l %u %t "%r" %s %b) and combined (adds referer and user agent) are shortcuts.

The stand-in below implements a handful of codes so you can see how a pattern string turns into a line.

Java Build access-log lines from a pattern (stand-in for AccessLogValve)

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.

What to look for in an access log

  • Status codes: a burst of 500 means application errors; 404s on strange paths (/wp-login.php, /.env) are scanners; 401/403 patterns show attacks on logins.
  • %D: slow endpoints. Sort by it to find what to optimize.
  • Client IPs: behind a proxy, add RemoteIpValve so %h shows the real client, and log X-Forwarded-For as a header field.
  • Bytes: large %b values reveal heavy downloads.

Common mistakes

  • Using System.out.println and e.printStackTrace() for application logging, so output mixes in catalina.out with no levels.
  • Logging passwords, session ids or full credit card numbers.
  • Forgetting rotation and cleanup (maxDays).
  • Enabling FINEST globally, which can slow the server and fill the disk in minutes.
  • Putting the access valve at the Engine level and expecting a separate file per host (use it at the Host level).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file receives System.out output when Tomcat is started with catalina.sh start?

    The launcher redirects standard output and error to logs/catalina.out, which Tomcat does not rotate by itself.
  2. 2. What does the AccessLogValve code %D record?

    %D is processing time in milliseconds (%T is in seconds).
  3. 3. Which JULI level produces more output than INFO?

    From most to least detail: FINEST, FINER, FINE, CONFIG, INFO, WARNING, SEVERE.
  4. 4. Why add RemoteIpValve when Tomcat is behind a reverse proxy?

    The valve reads X-Forwarded-For from trusted proxies and sets the remote address seen by %h and by your application.

Summary

  • Tomcat writes server logs (catalina, localhost), console output (catalina.out) and access logs.
  • conf/logging.properties configures JULI handlers, levels, directories and maxDays.
  • Applications should use a real logging API with their own configuration; avoid System.out.
  • The AccessLogValve pattern codes (%h %l %u %t %r %s %b %D) define each request line; put it in the Host.
  • Rotate and clean logs, never log secrets, and raise levels only temporarily.

Next: Realms and Authentication in Tomcat