Log4j quickstart
Add the log4j jars
Section titled “Add the log4j jars”If you are using maven, add the following dependency
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version></dependency>At the time of writing, 1.2.16 was the latest version. You should change it to the latest stable version.
Add a log4j.properties configuration file
Section titled “Add a log4j.properties configuration file”Add this to the top-level classpath. The following will log INFO and higher messages to console / stdout.
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p %t %C - %m%nYou can start tweaking the pattern layout to suit your needs.
Add to spring webapp
Section titled “Add to spring webapp”If your project is a Spring webapp, you can add the following listener to web.xml.
<listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener>If you are using org.springframework.web.context.ContextLoaderListener, add the
Log4jConfigListener above it.
Specifying the log4j.properties file
Section titled “Specifying the log4j.properties file”Instead of using the default log4j.properties file, you can specify its location as a
URL using the system property log4j.configuration. e.g.
-Dlog4j.configuration=file:///c:/work/webapp_log4j.properties