Using log4j in Spring MVC

Source: Internet
Author: User

1. Dependent Packages
Commons-logging-1.1.3.jar, Log4j-1.2.15.jar
2. Configure Web. xml

1 <Context-param>2     <Param-name>Log4jconfiglocation</Param-name>3     <Param-value>/web-inf/log4j.properties</Param-value>4 </Context-param>5 <Listener>6     <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>7 </Listener>

3. Configuring the Log4j.properties Properties file

1 ################ FATAL, ERROR, WARN, INFO, DEBUG2 log4j.rootlogger=warn,stdout,d,e3 4 # # # stdout # #5 Log4j.appender.stdout=org.apache.log4j.consoleappender6 Log4j.appender.stdout.target=system.out7 Log4j.appender.stdout.layout=org.apache.log4j.patternlayout8 log4j.appender.stdout.layout.conversionpattern=%d{absolute}%5p%c{1}:%l-%m%n9 Ten # # # logFile # # One # # # Save error to another file # # # A Log4j.appender.d=org.apache.log4j.dailyrollingfileappender - Log4j.appender.d.file=d:/logs/test-error.log - log4j.appender.d.append=true the #error only the This file - Log4j.appender.d.threshold =warn - Log4j.appender.d.layout=org.apache.log4j.patternlayout -Log4j.appender.d.layout.conversionpattern=%-d{yyyy-mm-dd HH:MM:SS} [%t:%r]-[%p]%m%n

4, global exception handling, servlet-config.xml configuration

1 <BeanID= "Exceptionresolver"class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">2     < Propertyname= "Exceptionmappings">3         <Props>4             <propKey= "Java.lang.Exception">Errors/error</prop>5             <propKey= "Java.lang.Throwable">Errors/err</prop>6         </Props>7     </ Property>8     < Propertyname= "Statuscodes">9         <Props>Ten             <propKey= "Errors/error">500</prop> One             <propKey= "errors/404">404</prop> A         </Props> -     </ Property> -     <!--set the log output level, do not define error log messages such as warnings by default - the     < Propertyname= "Warnlogcategory"value= "WARN"></ Property> -     <!--default error page, use this default configuration when you cannot find the corresponding view for the exception specified in mappings above - -     < Propertyname= "Defaulterrorview"value= "Errors/error"></ Property> -     <!--default HTTP status code - +     < Propertyname= "Defaultstatuscode"value= "$"></ Property> - </Bean> + <!--Global Exception Configuration end -

5. Code Log Write

1 Private Final Log logger = Logfactory.getlog (Simplercontroller.  Class); 2 3 Logger.warn ("logger"); 4 logger.error ("~~info~~");

6. Reference

Spring MVC exception handling and log4j configuration

Using log4j in Spring MVC

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.