Logging

Inspiration

“Explain to an experienced sysadmin who knows nothing about Tomcat or Java (but a lot about system utilities etc) how to set up a coherent and easy-to-manage logging system for Tomcat (and applications therein), including (safe) log rotation, archiving, cleanup etc.”
André Warnier

Types of logs

Access logs

Tomcat logs

Application logs

Logging Frameworks

There are lots

There are even frameworks for the frameworks

java.util.logging

JRE came a little late to the party

Similar to log4j

java.util.logging

Loggers are in a hierarchy

org

org.apache

org.apache.tomcat

Typically follows Java package hierarchy

Logger names typically match class names

Don't have to

java.util.logging

Set levels at any point in the hierarchy

Add handlers at any point in the hierarchy

Option to pass log message up hierarchy

java.util.logging

Is not class loader aware

Configuration file only allows one instance of a Handler type

JULI

Java Util Logging Implementation

Commons logging hard-coded to use java.util.logging

Class loader aware

Allows multiple handlers of same type

Tomcat logging

A closer look at Tomcat's default logging.properties

Production changes

Remove the ConsoleHandler

Nothing should output to STDOUT or STDERR during normal operations

catalina.YYYY-MM-DD.log should be mostly empty

Deployment and undeployment may trigger some logging

Application logging

Servlet API provides very basic logging

log(String)

log(String, Throwable)

Not sufficiently flexible for most use cases

Application logging

Could use anything

Direct to stdout

Servlet API

Logging framework routed to files

Logging framework routed to stdout

java.util.logging

Frameworks

Configure to files outside of docBase

Configure to redirect to java.util.logging then configure via JULI

Servlet API

Passed to Container's logger

log(String) at INFO

log(String, Throwable) at ERROR

Configure via JULI

stdout

Or stderr

Time for a training course

swallowOutput on Context

All output for one request gets redirected to a single INFO log messages

JULI

rotation: rotatable (daily rotation)

archiving: no support

clean-up: maxDays

Access logs

Logs every request

Separate from logging framework

Optimised for performance

Access logs

rotation: rotatable, fileDateFormat, renameOnRotate

archiving: no support

clean-up: maxDays

Demonstration

JSP that outputs to stdout