Interview Questions

Spring - Log4j Configuration

Loggers are used to Log messages in any application. With Logger, your application has better control on what messages needs to be logged to the files. This can be achieved using the Log Levels.

Loggers have different Log Levels associated to it. For example., INFO, DEBUG, WARN, ERROR, FATAL etc. Based on the Log Levels configured, the Logger can put only those messages corresponding to Levels to the Log files. Rest of them can be ignored.

In this example, Let us see how to configure Log4j in Spring application.

1. Configure the location of the log4j.properties in the web.xml file.
web.xml

<context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>

In the above configuration, log4j.properties resides inside /WEB-INF/classes directory.

2. Create a file named log4j.properties insides /WEB-INF/classes with the below contents. In this property file, any Java file within in.techdive package will redirect the logs to myAppLog appender.

Spring Framework Logs will be redirected to springLog appender.

Based on your application requirements, you can configure as many Log appenders you require.

# Log appender for package in.techdive
log4j.logger.in.techdive=DEBUG,myAppLog
log4j.appender.myAppLog.File=myAppLog.log
log4j.appender.myAppLog.MaxBackupIndex=10
log4j.appender.myAppLog.MaxFileSize=5MB
log4j.appender.myAppLog.Encoding=UTF-8
log4j.appender.myAppLog.layout.ConversionPattern=%d [%c: %L] - %m%n
log4j.appender.myAppLog.layout=org.apache.log4j.PatternLayout
log4j.appender.myAppLog=org.apache.log4j.RollingFileAppender

# Log appender for package org
log4j.logger.org=ERROR,springLog
log4j.appender.springLog.File=springLog.log
log4j.appender.springLog.MaxBackupIndex=10
log4j.appender.springLog.MaxFileSize=5MB
log4j.appender.springLog.Encoding=UTF-8
log4j.appender.springLog.layout.ConversionPattern=%d [%c: %L] - %m%n
log4j.appender.springLog.layout=org.apache.log4j.PatternLayout
log4j.appender.springLog=org.apache.log4j.RollingFileAppender

# Log appender for package stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n

3. In-order to use Logger in any Java class, follow the below steps,

a. Create a Class variable for Logger as below,

private final Log logger = LogFactory.getLog(getClass());

b. Use the above logger object inside the class as below,

logger.info("Add Your Application specific Log Messages here");

LoggerTest Class

package in.techdive;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * Class for testing Logger.
 */

public class LoggerTest implements Controller
{

        private final Log       logger  = LogFactory.getLog(getClass());

        public ModelAndView handleRequest(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException
        {
                logger.info("Add Your Application specific Log Messages here");

                logger.debug("Add Your Application specific Log Messages here");

                // Add your application specific custom logic here
                HashMap map = new HashMap();

                return new ModelAndView("response", "model", map);
        }
}

Note: Refer the log4j documentation to get more information about configuring the log4j.properties file.