Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Different loggers can be used to log web application outputs. The following instructions are for Log4j java.util.logging.

Web Application logging with

...

To include log4j logging in your web application add the following dependency to your maven project file:

...


<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.13</version>
    <type>jar</type>
    <properties>
       <war.bundle>true</war.bundle>
    </properties>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.0.4</version>
    <type>jar</type>
    <url>http://jakarta.apache.org/commons/logging</url>
    <properties>
       <war.bundle>true</war.bundle>
    </properties>
</dependency>

java.util.logging

All you have to do is create a file called loggingCreate a file called log4j.properties in WEB-INF/classes with the following content, where you have replaced APPLICATION_OUTPUT_FILE with your choice of output file name (it would be nice to have in it the name of the application that produced the file):

Code Block
#
#handlers Configures Log4j as the Tomcat system logger
#

#= org.apache.juli.FileHandler

############################################################
# ConfigureHandler the logger to output info level messages into a rolling log filespecific properties.
#
log4j.rootLogger=INFO, R

#
# Configuration for a rolling log file
#
log4j.appender.R= Describes specific configuration info for Handlers.
############################################################

org.apache.log4j.RollingFileAppender

#
# Edit the next line to point to your logs directory.
# The last part of the name is the log file name.
#
log4j.appender.R.File=juli.FileHandler.level = FINE
org.apache.juli.FileHandler.directory = ${catalina.base}/logs/APPLICATION_OUTPUT_FILE.log
log4j.appender.R.layout=org.apache.log4jjuli.PatternLayout
log4j.appender.R.MaxFileSize=5036KB
log4j.appender.R.MaxBackupIndex=4

#
# Print the date in ISO 8601 format
#
log4j.appender.R.layout.ConversionPattern=%d %-5p - %m%n

The above configuration creates a rolling output file. Once the file reaches 0.5 Mb in size it is renamed by adding an index to its end and a new log file is created. A maximum of 4 log files is kept.

...

FileHandler.prefix = APPLICATION_OUTPUT_FILE.

If you now redeploy your application all the output from your application should be logged in the file you chose.

...