Log4j quickstart

How to add the log4j logging framework to your Java project

Published: Sunday, 6 March 2011

Add the log4j jars

If you are using maven, add the following 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

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.layout.ConversionPattern=%d %p %t %C - %m%n

You can start tweaking the pattern layout to suit your needs.

Add to spring webapp

If your project is a Spring webapp, you can add the following listener to web.xml.


If you are using org.springframework.web.context.ContextLoaderListener, add the Log4jConfigListener above it.

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.