Spring and Log4J integration – Let’s explore

Spring & Log4J

Serie Spring - Saber integración log4j polo Exemplo,,en,Neste pequeno tutorial, imos ir directamente para a implantación e codificación parte,,en,Non imos discutir moito sobre a parte teórica,,en,Isto axudará os desenvolvedores para obter unha referencia rápida e integrar log4j facilmente con framework Spring,,en,como configurar,,en,Rexistrador é unha parte importante de calquera aplicación e mecanismo de rexistro máis vulgarmente utilizado é o log4j,,en,aplicacións Spring base tamén pode usar o poder de log4j,,en,A continuación é un exemplo completo na integración primavera e log4j,,en,O primeiro compoñente é o arquivo web.xml que contén o camiño para,,en,log4jConfigLocation,,en,paran-value,,en,WEB-INF / clases / log4j.properties,,en,A segunda parte é o arquivo log4j.properties que contén a información nivel de rexistro,,en

Visión global

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

<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. Depende enteiramente da súa necesidade de aplicación,,en,Rexistro appender para in.techdive paquete,,en,log4j.logger.in.techdive = Depurar,,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,Rexistro appender para org paquete,,en,log4j.logger.org = Erro,,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,Rexistro appender para saída estándar paquete,,en,log4j.appender.stdout = org.apache.log4j.ConsoleAppender,,en.

# 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,,en,log4j.appender.stdout.layout.ConversionPattern =% d% p,,en,parte é a clase Java,,en,para probar a configuración log4j,,en,Se executar a aplicación as mensaxes de log aparecerá,,en,com.techalpine paquete,,en,importación java.util.HashMap,,en,importación javax.servlet.ServletException,,en,importación javax.servlet.http.HttpServletRequest,,en,importación javax.servlet.http.HttpServletResponse,,en,importación org.springframework.web.servlet.ModelAndView,,en,importación org.springframework.web.servlet.mvc.Controller,,en,Clase para probas rexistrador,,en,clase pública Log4JTest aplica Controlador,,en,Loggia privada Log final = LogFactory.getLog,,en,getClass,,en,pública ModelAndView handleRequest,,en,lanza ServletException,,en,logger.info,,da,Agregar mensaxe aquí que son de Información Tipo,,en,logger.debug,,en,Agregar mensaxe aquí que son do tipo de depuración,,en,voltar nova ModelAndView,,en,Agora sabemos como configurar log4j con framework Spring,,en

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;

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

HttpServletResponse resposta) 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”, “modelo”, mapa);

}

}








Conclusión

Now we know how to configure Log4J with Spring framework. Os pasos son moi doado e pode ser seguido rapidamente,,en,Aquí temos só discutido e hands-on de codificación parte para que os desenvolvedores poden implementar lo facilmente con un coñecemento terreo de volta de log4j,,en,Mantivemos a parte teórica do lado intencionalmente para facelo sinxelo,,en,Espero que vai divertirse,,en,Nós imos cubrir a parte conceptual máis detalles nosos próximos artigos,,en,log4j,,mt. Here we have only discussed and the hands-on coding part so that the developers can implement it easily with a back ground knowledge of Log4J. 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