Log4j Configuration in Spring Application

As we all know how loggers plays a crucial part in managing program flow messages  in the application.
Your application gets  better control on what messages needs to be logged to the files. This can be achieved using the Log Levels. For example., INFO, DEBUG, WARN, ERROR, FATAL etc. For more details on Log4J Framework, Read here @http://logging.apache.org/log4j/1.2/

Now lets see below the LOG4J Configuration in Spring App.

1. In Web.xml add the below entries.

<!– specify the log properties file location –>

<!– spring based logger listener, which will read the log config and start the logger service  –>

2. Add the below log4j.properties inside. WEB-INF/classes/ folder.

# Set root logger level to INFO
log4j.rootLogger=INFO, ET

# ET is set to be a File.

# ET uses PatternLayout.
log4j.appender.ET.layout.ConversionPattern=[ %-5d %-5p %-4rms ] %F : %L – %m%n

3. Now load the logger from the Log factory in your Java class.

public static Logger logger = Logger.getLogger(“ET”);
OR public static Logger logger = Logger.getLogger();
logger.info(“Test message”);

I will try to post a new material on how to use Logging effectively in the application. Wait for that 😉

R Vashi

1 thought on “Log4j Configuration in Spring Application

  1. 劉鵬飛

    log4j.properties is the default file name that the container will look for in the CLASSES folder. So can we skip the step 1(specify its location in web.xml)?


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.