"Spring" Spring integrates log4j in the Javaweb project

Source: Internet
Author: User

In the "Spring" Spring3.0.5 download, Configuration and HelloWorld (click to open the link) of the article you may have seen. If spring does not integrate log4j directly, the following warning about spring consolidation log4j appears. This is very annoying, on the one hand advocating high cohesion, low coupling, on the other hand, they did not integrate log4j on the warning. The program that our program Ape writes is called "coupling," and spring is called "integration." All right! You can only understand at the same time, log4j is what a ghost, spring how to integrate log4j, two questions:

Log4j:warn No Appenders could is found for logger (org.springframework.web.context.ContextLoader). Log4j:warn Initialize the log4j system properly.
Since the previous introduction is a simple example of spring in Java Engineering, log4j must run in the Javaweb project to make sense, and you have to figure out how spring works on the Javaweb project, and how spring integrates log4j in Javaweb engineering.


I. Download of spring and log4j

1, you first have to have these two javaweb components of the jar package it? How else would it be? Spring has already been described in the article "Spring" Spring3.0.5 download, Configuration and HelloWorld (click to open link). Log4j opens Apache's official website (click the Open link) For example, choose Log4j-1.2.17.zip (Windows) or log4j-1.2.17.tar.gz (Linux). The same thing as Apache, which is also integrated in some high-version tomcat, of course, you'd better make up the package in the Web-inf\lib directory, so that your project can run on all Tomcat.


2. Create a new dynamic Web Project named springlog4j in Eclipse for Java EE, and after extracting it, Put everything in spring-framework-3.0.5.release-dependencies with all the jar packages in spring-framework-3.0.5.release\dist, not including that libd file, apache-log4j-1.2.17 under Log4j-1.2.17.jar, copy to Web\lib folder.


3. Then your eclipse for Java EE is shown below. The Web-inf directory under the Web. XML, Applicationcontext.xml, log4j.properties, and Root log4j.jsp is something we're going to write for a while.



Ii. configuration of Spring and log4j

1. Web. xml

The first is the total configuration location of the project on Javaweb. We're going to declare that we want to use spring and log4j. Note that the configuration of the log4j must precede the spring configuration, otherwise if you start spring, that must be integrated log4j to not spit warning spring, because log4j has not been started, cannot find spring, but also in the wayward to spit the warning. Of course, you set those priorities also line, but, first start the direct put front, this file is not better to see it?

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "><!--log4j configuration--><listener>< listener-class>org.springframework.web.util.log4jconfiglistener</listener-class></listener>< !--the directory where the configuration file for log4j is specified. The default configuration is in the Web-inf directory--><context-param><param-name>log4jconfiglocation</param-name>< param-value>/web-inf/log4j.properties</param-value></context-param><!--Spring Configuration-->< Listener><listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener><!--Specifies the directory where the spring bean's configuration file resides. The default configuration is in the Web-inf directory--><context-param><param-name>contextconfiglocation</param-name>< Param-value>/web-inf/applicationcontext.xml</param-value></context-paRam></web-app>   
2, Log4j.properties

This guy's suffix is so long, what do you have in your way? Must be hit, all Linux is brought over to the Lord, you see people's Windows configuration file suffix is just ini3 letters!

#log4j. Rootlogger = [level], Appendername, appendername, ... log4j.rootlogger = all, console, r#consolelog4j.appender.co Nsole = Org.apache.log4j.ConsoleAppenderlog4j.appender.console.layout = Org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern =%-d{yyyy-mm-dd HH:mm:ss} [%c]-[%p] %M%N#FILELOG4J.APPENDER.R = Org.apache.log4j.rollingfileappenderlog4j.appender.r.file = c:/ Log.txtlog4j.appender.r.maxfilesize = 500kblog4j.appender.r.maxbackupindex = 1log4j.appender.r.layout = Org.apache.log4j.patternlayoutlog4j.appender.r.layout.conversionpattern=%-d{yyyy-mm-dd HH:MM:SS} [%c]-[%p]-%m%n
This thing also can't add Chinese comment, give everybody line to say. #后面还仅能写英文的东西, where do you put my big celestial program ape? I will add a Chinese note to do not let!

The first part is the total configuration section of the log4j, and all represents the output of the Debug,info,error,fatal four types of information. Generally not set to all, here just to let you see the effect. Because those debug,info information to us no meaning, also because there are many system internal files run will output debug and info Information Brush screen, brush version. The key is that the log files that are output to disk will increase in speed and waste disk space. When you play SQL Server, you don't know that. How horrible is LDB?

So the first part is generally written as:

Log4j.rootlogger = ERROR, console, R
Represents only output error and fatal errors.

After the console,r are represented in the console with the file output respectively. At the same time the code must be configured well in the following two outputs.

Part Two console #console
The first thing to do is to use the log4j specific package, this is nothing to say, the last sentence indicates the output format. After a while big strokes control output to understand what is going on.

Part III file #file

Log4j.appender.r.file=c:/log.txt means that the Web engineering error log is output to C:/log.txt. Don't be like the big gods on the internet. Log suffix, the key is to be able to open directly.

After log4j.appender.r.maxfilesize = 500KB indicates that the Log.txt file size is up to 500KB, if this size is exceeded, a new file is automatically opened, and Log4j.appender.r.maxbackupindex =1 indicates that this project can only have 1 log files at most. , the new content overwrites the old content, just like the CCTV camera heads.

The log4j is finished here.

3, Applicationcontext.xml

Then there was the spring part, because this time it didn't do anything with spring, so this applicationcontext.xml:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://www.springframework.org/schema/beans" xsi:schemalocation= "http://www.springframework.org/schema/ Beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd "></beans>
I have deleted all that can be deleted.

4, log4j.jsp

Finally, the log4j.jsp is in the webcontent root directory. Write the following code in it, the entire program is the entrance to this place.

I don't even have a servlet, just to get people to see the key to the problem directly.

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "import=" Org.apache.log4j.Logger "%><%logger.getlogger" ( This.getclass ()). Fatal ("Fatal error! "); Logger.getlogger (This.getclass ()). Error ("error message! "); Logger.getlogger (This.getclass ()). info ("General info! "); Logger.getlogger (This.getclass ()). Debug ("Debug Info!"). ");%>

iii. Results of Operation

Put springlog4j this javaweb project into Tomcat, run it, and then enter it in any browser http://localhost:8080/SpringLog4j/ Log4j.jsp, after the Web page successfully loaded, directly back to eclipse to get the following results, you enter a URL refresh once, then output the following results:


At the same time, your C disk, then more than a log.txt, the contents of the following, you can see, in the program run, Spring run will spit out a lot of meaningless debug, info, trace information, for us, really useful the last four words. Therefore, it can be understood, why in the above log4j.properties (this suffix name is difficult to remember, long, again despise it!) The first part of the general does not write all, only write error, which only output error above the level of errors, that is, error and fatal. or write trace, output trace, error and fatal information.

2015-05-10 09:54:34 [Org.springframework.web.context.contextloader]-[info]-Root webapplicationcontext: Initialization started2015-05-10 09:54:34 [org.springframework.web.context.support.xmlwebapplicationcontext]-[ INFO]-Refreshing Root webapplicationcontext:startup date [Sun May 09:54:34 CST 2015]; Root of context hierarchy2015-05-10 09:54:34 [Org.springframework.beans.factory.xml.xmlbeandefinitionreader]-[info] -Loading XML Bean definitions from servletcontext resource [/web-inf/applicationcontext.xml]2015-05-10 09:54:34 [ Org.springframework.beans.factory.xml.defaultdocumentloader]-[debug]-Using JAXP provider [ Com.sun.org.apache.xerces.internal.jaxp.documentbuilderfactoryimpl]2015-05-10 09:54:34 [ Org.springframework.beans.factory.xml.pluggableschemaresolver]-[trace]-Trying to resolve XML entity with public ID [ NULL] and System ID [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd]2015-05-10 09:54:34 [ Org.springframework.beans.factory.xml.PluggableSchemaResolVer]-[debug]-Loading schema mappings from [meta-inf/spring.schemas]2015-05-10 09:54:34 [ Org.springframework.beans.factory.xml.pluggableschemaresolver]-[debug]-Loaded Schema mappings: {/HTTP/ Www.springframework.org/schema/oxm/spring-oxm-3.0.xsd=org/springframework/oxm/config/spring-oxm-3.0.xsd, http:/ /www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd , http://www.springframework.org/schema/jms/spring-jms-3.0.xsd=org/springframework/jms/config/ Spring-jms-3.0.xsd, http://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/ Config/spring-task-3.0.xsd, http://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/ Aop/config/spring-aop-3.0.xsd, Http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework /aop/config/spring-aop-2.0.xsd, http://www.springframework.org/schema/oxm/spring-oxm.xsd=org/springframework/ Oxm/config/spring-oxm-3.0.xsd, Http://www.springfraMework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd, HTTP// www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/ Spring-beans-3.0.xsd, http://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/ Config/spring-jee-2.5.xsd, http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/ Config/spring-aop-3.0.xsd, Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework /beans/factory/xml/spring-beans-2.0.xsd, http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd=org/ Springframework/web/servlet/config/spring-mvc-3.0.xsd, http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd, http://www.springframework.org /schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd, HTTP// Www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/spRingframework/transaction/config/spring-tx-2.5.xsd, http://www.springframework.org/schema/context/ Spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd, HTTP// Www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd=org/springframework/jdbc/config/spring-jdbc-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/ Spring-tool-3.0.xsd, http://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/ Config/spring-tx-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/ Beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/util/spring-util-2.5.xsd=org/ Springframework/beans/factory/xml/spring-util-2.5.xsd, http://www.springframework.org/schema/lang/ Spring-lang.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd, Http://www.springframework.org/schema /lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lAng-2.5.xsd, http://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/ Spring-jee-3.0.xsd, Http://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config /spring-jee-2.0.xsd, http://www.springframework.org/schema/context/spring-context.xsd=org/springframework/ Context/config/spring-context-3.0.xsd, http://www.springframework.org/schema/jee/spring-jee.xsd=org/ Springframework/ejb/config/spring-jee-3.0.xsd, http://www.springframework.org/schema/jms/spring-jms-2.5.xsd=org /springframework/jms/config/spring-jms-2.5.xsd, http://www.springframework.org/schema/jms/spring-jms.xsd=org/ Springframework/jms/config/spring-jms-3.0.xsd, http://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org /springframework/aop/config/spring-aop-2.5.xsd, http://www.springframework.org/schema/mvc/spring-mvc.xsd=org/ Springframework/web/servlet/config/spring-mvc-3.0.xsd, http://www.springframework.org/schema/jdbc/ Spring-jdbc.xsd=org/springframework/jdbc/config/spring-jdbc-3.0.xsd, http://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/ Transaction/config/spring-tx-2.0.xsd, http://www.springframework.org/schema/tx/spring-tx-3.0.xsd=org/ Springframework/transaction/config/spring-tx-3.0.xsd, http://www.springframework.org/schema/context/ Spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd, HTTP// Www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd, http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/ Spring-util-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/ Scripting/config/spring-lang-3.0.xsd, http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/ Springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/lang/ Spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http:www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/ Spring-beans-2.5.xsd}2015-05-10 09:54:34 [Org.springframework.beans.factory.xml.pluggableschemaresolver]-[debug] -Found XML Schema [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd] in classpath:org/ Springframework/beans/factory/xml/spring-beans-3.0.xsd2015-05-10 09:54:34 [ Org.springframework.beans.factory.xml.defaultbeandefinitiondocumentreader]-[debug]-Loading Bean Definitions2015-05-10 09:54:34 [Org.springframework.beans.factory.xml.xmlbeandefinitionreader]-[debug]-Loaded 0 Bean definitions from the location pattern [/web-inf/applicationcontext.xml]2015-05-10 09:54:34 [ Org.springframework.web.context.support.xmlwebapplicationcontext]-[debug]-Bean factory for Root webapplicationcontext:org.s[email protected]5eedf162:defining beans []; Root of Factory hierarchy2015-05-10 09:54:34 [org.springframework.web.context.support.xmlwebapplicationcontext]-[ DEBUG]-Unable To locate Messagesource with Name ' Messagesource ': Using default [[Email protected]0824]2015-05-10 09:54:34 [Org.sp Ringframework.web.context.support.xmlwebapplicationcontext]-[debug]-Unable to locate Applicationeventmulticaster With Name ' Applicationeventmulticaster ': Using default [org.[email protected]708a538f]2015-05-10 09:54:34 [ Org.springframework.ui.context.support.uiapplicationcontextutils]-[debug]-Unable to locate ThemeSource with name ' Themesource ': Using default [o[email protected]1fce66ba]2015-05-10 09:54:34 [ Org.springframework.beans.factory.support.defaultlistablebeanfactory]-[info]-Pre-instantiating singletons in org.s[email protected]5eedf162:defining beans []; Root of Factory hierarchy2015-05-10 09:54:34 [org.springframework.web.context.support.xmlwebapplicationcontext]-[ DEBUG]-Unable to locate lifecycleprocessor with Name ' lifecycleprocessor ': Using default [[EMAIL&NBSP;PROTECTED]BC2C83] 2015-05-10 09:54:34 [Org.springframework.beans.factorY.support.defaultlistablebeanfactory]-[debug]-Returning cached instance of singleton beans ' Lifecycleprocessor ' 2015-05-10 09:54:34 [org.springframework.web.context.support.xmlwebapplicationcontext]-[trace ]-publishing event in Root webapplicationcontext:org.springframework.context.event.contextrefreshedevent[source= Root webapplicationcontext:startup Date [Sun May 09:54:34 CST 2015]; Root of context hierarchy]2015-05-10 09:54:34 [Org.springframework.web.context.contextloader]-[debug]-Published root Webapplicationcontext as ServletContext attribute with name [ Org.springframework.web.context.webapplicationcontext.root]2015-05-10 09:54:34 [ Org.springframework.web.context.contextloader]-[info]-Root Webapplicationcontext:initialization completed in 230 Ms2015-05-10 09:54:48 [Org.apache.jsp.log4j_jsp]-[fatal]-fatal error! 2015-05-10 09:54:48 [Org.apache.jsp.log4j_jsp]-[error]-error message! 2015-05-10 09:54:48 [Org.apache.jsp.log4j_jsp]-[info]-General information! 2015-05-10 09:54:48 [Org.apache.jsp.Log4j_jsP]-[debug]-Debug Info! 

Iv. Summary

Log4j generally do not write directly in log4j.jsp like this. Usually in those Try-catch exception structure in the catch inside, perhaps some operation files, database of key data between, directly to the programmer to see. Program apes have time to look at that in the Log.txt, also like security guards have time to look at the CCTV, to determine whether your Web project is in the end is not normal. Can become part of the system operation.

"Spring" Spring integrates log4j in the Javaweb project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.