Using log4j to log logs in spring projects

Source: Internet
Author: User
Tags log4j

(1) Introduction of log4j jar Package:

Official website: http://logging.apache.org/log4j/1.2/download.html

(2) Add log4j configuration in Web. xml:

12345678 <context-param>    <param-name>log4jConfigLocation</param-name>    <param-value>classpath:log4j.properties</param-value></context-param><context-param>    <param-name>log4jRefreshInterval</param-name>    <param-value>60000</param-value></context-param>

(3) log4j properties file Log4j.properties:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 #Root Categorylog4j.rootLogger=debug,stdout,D,E#Consolelog4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n#DEBUGlog4j.appender.D = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.D.File = ${catalina.base}/logs/debug.loglog4j.appender.D.Append = truelog4j.appender.D.Threshold = DEBUGlog4j.appender.D.layout = org.apache.log4j.PatternLayoutlog4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n#ERRORlog4j.appender.E = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.E.File =${catalina.base}/logs/error.loglog4j.appender.E.Append = truelog4j.appender.E.Threshold = ERRORlog4j.appender.E.layout = org.apache.log4j.PatternLayout  log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n #IBATIS Categorylog4j.logger.com.ibatis=DEBUGlog4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUGlog4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUGlog4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUGlog4j.logger.java.sql.Connection=DEBUGlog4j.logger.java.sql.Statement=DEBUGlog4j.logger.java.sql.PreparedStatement=DEBUGlog4j.logger.java.sql.ResultSet=DEBUG#WS Logger for reportinglog4j.logger.WS=INFO,WSlog4j.appender.WS=org.apache.log4j.DailyRollingFileAppender    log4j.appender.WS.File=${catalina.base}/logs/ws.loglog4j.appender.WS.layout=org.apache.log4j.PatternLayoutlog4j.appender.WS.DatePattern  =‘.‘yyyy-MM-dd    log4j.appender.WS.layout.ConversionPattern=%d-[ws] %p %t %c - %m%n #Other Loggerlog4j.logger.org.springframework=INFOlog4j.logger.com.ibatis.db=INFO  log4j.logger.net.sf.json=ERRORlog4j.logger.org.apache.cxf=DEBUG,WS

Note: My configuration file here is not very good, for reference only

(4) Test:

1234567891011121314151617 packagecn.zifangsky.controller;importorg.apache.log4j.Logger;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;@ControllerpublicclassTestController {    privatestaticLogger logger = Logger.getLogger(TestController.class);        @RequestMapping("/test.html")    publicvoidtest(){        logger.info("info测试");        logger.debug("debug测试 ");        logger.error("error测试");    }}

The console output is as follows:

123456 [INFO ] 2016-11-06 18:01:28,000 method:cn.zifangsky.controller.TestController.test(TestController.java:13)info测试[DEBUG] 2016-11-06 18:01:28,007 method:cn.zifangsky.controller.TestController.test(TestController.java:14)debug测试 [ERROR] 2016-11-06 18:01:28,007 method:cn.zifangsky.controller.TestController.test(TestController.java:15)error测试

Debug.log file:

123 2016-11-06 18:01:28  [ http-apr-9180-exec-10:0 ] - [ INFO ]  info测试2016-11-06 18:01:28  [ http-apr-9180-exec-10:7 ] - [ DEBUG ]  debug测试 2016-11-06 18:01:28  [ http-apr-9180-exec-10:7 ] - [ ERROR ]  error测试

Error.log file:

1 2016-11-06 18:01:28  [ http-apr-9180-exec-10:7 ] - [ ERROR ]  error测试

Using log4j logging in spring projects

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.