Apache logging services logo Apache log4j logo

Log4j Web

Contains classes that can be used to initialize logging within a web application.


Log4j provides a ServletContextListener that will initialize the LoggerContext for your web application. It accepts two initialization parameters:

  1. log4jContextName - the name to be given to the LoggerContext. If not specified this will default to the display-name of the web application.
  2. log4jConfiguration - the location of the Log4j configuration file. If the file protocol is not specified then the file will be located both in the class path and on the file system. If the location is a relative path the file location will be relative to the current working directory of the running server.

To register this listener, make sure that the log4j-web-2.0.jar file is in the classpath (either in your web application's WEB-INF/lib directory or in the container's classpath) and add the following snippet to your web.xml:


Either or both of the initialization parameters may contain variables of the form ${sys:propertyName} or ${env:propertyName} to allow substitution using either a system property or an environment variable.


The JNDIContextFilter is a Servlet Filter that can be used in conjunction with the JNDIContextSelector to identify the LoggerContext that should be used. It accepts two parameters.

  1. context-name - the name to be given to the LoggerContext. A context name must be specified.
  2. config-location - the location of the Log4j configuration file.


The Log4j web components require at least Java 6 and at least Servlet 2.5 (or Java EE 5).