Spring and Log4J integration – Let’s explore

Spring & Log4J

Spring Series – Learn Log4J integration by Example

Pārskats

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 fails.

<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 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







  • 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;

Importa 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 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);

}

}








Secinājums

Now we know how to configure Log4J with Spring framework. The steps are very easy and can be followed quickly. Šeit mēs esam tikai apspriedām un rokas kodēšanas daļu tā, ka izstrādātāji var ieviest to viegli ar muguras zemes zināšanām log4j,,en,Mēs esam tur teorētisko daļu malā apzināti, lai padarītu to vienkāršu,,en,Ceru, ka jums baudīt to,,en,Mēs segt konceptuālo daļu vairāk informāciju par mūsu nākamajās ziņās,,en,log4j,,mt,Kādi ir galvenie ieguvumi Programmatūras Automatizācijas testēšana,,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: ,
============================================= ============================================== Pērciet labākās Techalpine grāmatas vietnē Amazon,en,Elektriķa CT kastaņu valodas,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share