The Performance Impact of log4j

The potential performance impact of logging in an application is significant. For a typical production application, thousands or even hundreds of thousands of log messages can be logged every day, depending on the logging threshold that’s configured. The consequences might be even worse if exception stack traces are logged due to errors in the application.

The log4j documentation states that much attention has been given to minimizing the performance impact of logging using log4j. Nothing is free, however, and there is an additional cost associated with generating log messages. The question is, does that cost outweigh the benefits gained from the additional logging information?

The time cost of creating a log message using log4j depends on several factors:

  • The type of appender being used

  • The log4j layout being used to format the messages

  • The parameter construction time to create the log message

  • The depth of the logger hierarchy and where the log threshold is assigned

The type of appender has much to do with how long it will take to create a log message. Logging a message using an SMTP appender will take a different amount of time than logging to the filesystem. On a typical machine using Java 1.3, logging a message to the filesystem will take anywhere from 20 microseconds to upwards of 300 microseconds, depending on which layout you use. While this is a very small number, over time it can add up.

Probably the most significant impact on logging is what information ...

Get Programming Jakarta Struts, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.