Spring and Log4J integration – Let’s explore

Spring & Log4J

Primavera de la sèrie - Aprendre integració Log4J per exemple,,en,En aquest breu tutorial anem a saltar directament en l'aplicació i codificació part,,en,No discutirem tant en la part teòrica,,en,Això ajudarà als desenvolupadors per obtenir una referència ràpida i integrar fàcilment amb Log4J marc de Primavera,,en,Com configurar,,en,Logger és una part important de qualsevol aplicació i el mecanisme de registre més comunament utilitzat és log4j,,en,aplicacions basades en la primavera també poden utilitzar el poder de log4j,,en,El que segueix és un exemple complet en la integració de la primavera i l'log4j,,en,El primer component és l'arxiu web.xml, que conté la ruta,,en,log4jConfigLocation,,en,param-value,,en,WEB-INF / classes / log4j.properties,,en,La segona part és l'arxiu log4j.properties que conté la informació de nivell de registre,,en,Depèn completament de les seves necessitats d'aplicació,,en

Visió de conjunt

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

<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,,en,part és la classe Java,,en,per provar la configuració log4j,,en,Si s'executa l'aplicació es mostraran els missatges de registre,,en,com.techalpine paquet,,en,importació java.util.HashMap,,en,importació javax.servlet.ServletException,,en,importació javax.servlet.http.HttpServletRequest,,en,importació javax.servlet.http.HttpServletResponse,,en,importació org.springframework.web.servlet.ModelAndView,,en,importació org.springframework.web.servlet.mvc.Controller,,en,Classe de prova Logger,,en,Log4JTest classe pública implementa Controlador,,en,registrador de registre final privat = LogFactory.getLog,,en,getClass,,en,pública ModelAndView handleRequest,,en,llança ServletException,,en,logger.info,,da,Afegir missatge aquí, que són de tipus info,,en,logger.debug,,en,Afegir missatge aquí, que són del tipus de depuració,,en,tornar nova ModelAndView,,en,Ara sabem com configurar Log4J amb el marc de Primavera,,en,Els passos són molt fàcils i poden ser seguits ràpidament,,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;

importació 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(sol·licitud d'HttpServletRequest,

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”, “model”, map);

}

}








Conclusió

Now we know how to configure Log4J with Spring framework. The steps are very easy and can be followed quickly. Aquí només hem discutit i les mans a la codificació de part perquè els desenvolupadors puguin aplicar fàcilment amb un coneixement nou terreny de Log4J,,en,Hem mantingut la part teòrica de costat intencionadament perquè sigui senzill,,en,Espero que ho gaudeixin,,en,Anem a cobrir la part conceptual més en detall a les nostres properes entrades,,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.

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

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share