Spring and Log4J integration – Let’s explore

Spring & Log4J

Spring Series – Learn Log4J integration by Example

Tinjauan

In this short tutorial we will directly jump into the implementation and coding part. We will not discuss much on the theoretical part. This will help the developers to get a quick reference and integrate Log4J easily with Spring framework.








How to configure?

Logger is an important part of any application and the most commonly used logging mechanism is log4j. Spring based applications can also use the power of log4j. Following is a complete example on spring and log4j integration.

  • The first component is the web.xml file which contains the path for properties file.

<context-param>

<param-name>log4jConfigLocation</param-name>

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

</context-param>

  • The second part is the log4j.properties file which contains the log level information. It entirely depends upon your application need.

# Log appender untuk pakej in.techdive,,en,log4j.logger.in.techdive = DEBUG,,en,myAppLog,,en,log4j.appender.myAppLog.File = myAppLog.log,,en,log4j.appender.myAppLog.MaxBackupIndex = 10,,en,log4j.appender.myAppLog.MaxFileSize = 5MB,,en,log4j.appender.myAppLog.Encoding = UTF-8,,en,log4j.appender.myAppLog.layout.ConversionPattern =% d,,en,m% n,,en,log4j.appender.myAppLog.layout = org.apache.log4j.PatternLayout,,en,log4j.appender.myAppLog = org.apache.log4j.RollingFileAppender,,en,Log appender untuk pakej org,,en,log4j.logger.org = ERROR,,en,springLog,,en,log4j.appender.springLog.File = springLog.log,,en,log4j.appender.springLog.MaxBackupIndex = 10,,en,log4j.appender.springLog.MaxFileSize = 5MB,,en,log4j.appender.springLog.Encoding = UTF-8,,en,log4j.appender.springLog.layout.ConversionPattern =% d,,en,log4j.appender.springLog.layout = org.apache.log4j.PatternLayout,,en,log4j.appender.springLog = org.apache.log4j.RollingFileAppender,,en,Log appender untuk pakej stdout,,en,log4j.appender.stdout = org.apache.log4j.ConsoleAppender,,en,log4j.appender.stdout.layout = org.apache.log4j.PatternLayout,,en

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







  • The 3rd part is the java class java to test the log4j configuration. If you run the application the log messages will be displayed.

package com.techalpine;

java.io.IOException import;

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 Log4JTest implements Controller

{

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

public ModelAndView handleRequest(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException

{

logger.info(“Add message here which are of info type“);

logger.debug(“Add message here which are of debug type“);

return new ModelAndView(“response”, “model”, map);

}

}








Kesimpulan

Now we know how to configure Log4J with Spring framework. The steps are very easy and can be followed quickly. Di sini kita hanya membincangkan dan bahagian pengekodan tangan supaya pemaju dapat melaksanakannya dengan mudah dengan pengetahuan belakang Log4J,,en,Kami telah menyimpan bahagian teoretikal dengan sengaja untuk memudahkannya,,en,Harap anda akan menikmatinya,,en,Kami akan merangkumi bahagian konseptual lebih terperinci dalam jawatan kami yang seterusnya,,en. We have kept the theoretical part aside intentionally to make it simple. Hope you will enjoy it. We will cover the conceptual part more in details in our next posts.

Tagged on: ,
============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share