Spring and Log4J integration – Let’s explore

Spring & Log4J

Spring Series - Lär Log4j integration genom exempel,,en,I denna korta handledning kommer vi direkt hoppa in i genomförandet och kodning del,,en,Vi kommer inte att diskutera mycket på den teoretiska delen,,en,Detta kommer att hjälpa utvecklare att få en snabb referens och integrera Log4j enkelt med Spring Framework,,en,Så här konfigurerar,,en,Logger är en viktig del av alla program och den vanligaste loggning mekanism är log4j,,en,Spring baserade applikationer kan också använda kraften i log4j,,en,Nedan följer en fullständig exempel på våren och log4j integration,,en,Den första komponenten är den web.xml fil som innehåller sökvägen för,,en,log4jConfigLocation,,en,param-värde,,en,WEB-INF / klasser / log4j.properties,,en,Den andra delen är den log4j.properties fil som innehåller information om loggnivån,,en,Det beror helt på din ansökan behöver,,en

Översikt

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

<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 för paket 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 = 5 MB,,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 för paket 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 = 5 MB,,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 för paket 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,,en,delen är java klassen,,en,att testa log4j konfiguration,,en,Om du kör programmet logg meddelanden visas,,en,import java.util.HashMap,,en,import javax.servlet.ServletException,,en,import javax.servlet.http.HttpServletRequest,,en,import javax.servlet.http.HttpServletResponse,,en,import org.springframework.web.servlet.ModelAndView,,en,import org.springframework.web.servlet.mvc.Controller,,en,Klass för att testa Logger,,en,public class Log4JTest implementerar Controller,,en,privat slut Log logger = LogFactory.getLog,,en,getClass,,en,offentliga ModelAndView handleRequest,,en,kastar ServletException,,en,logger.info,,da,Lägg meddelande här som är av info typ,,en,logger.debug,,en,Lägg meddelande här som är av debug typ,,en,återvända ny ModelAndView,,en,Nu vet vi hur man konfigurerar Log4j med Spring Framework,,en,Stegen är mycket enkelt och kan följas snabbt,,en [%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;

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

}

}








Slutsats

Now we know how to configure Log4J with Spring framework. The steps are very easy and can be followed quickly. Här har vi bara diskuterat och hands-on kodande delen så att utvecklare kan genomföra det enkelt med en tillbaka marken kunskap om Log4j,,en,Vi har hållit den teoretiska delen åt sidan avsiktligt att göra det enkelt,,en,Hoppas du kommer att njuta av det,,en,Vi kommer att täcka den konceptuella delen mer i detalj i vår nästa inlägg,,en,Log4j,,mt. 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