[关闭]
@Pollux 2016-07-23T03:21:54.000000Z 字数 2695 阅读 895

spring-mvc配置log4j

web


  1. <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
  2. <dependency>
  3. <groupId>org.slf4j</groupId>
  4. <artifactId>slf4j-log4j12</artifactId>
  5. <version>1.7.21</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
  8. <dependency>
  9. <groupId>org.slf4j</groupId>
  10. <artifactId>slf4j-api</artifactId>
  11. <version>1.7.21</version>
  12. </dependency>
  1. <context-param>
  2. <param-name>log4jConfigLocation</param-name>
  3. <param-value>classpath:/log4j/log4j.properties</param-value>
  4. </context-param>
  5. <context-param>
  6. <param-name>log4jRefreshInterval</param-name>
  7. <param-value>6000</param-value>
  8. </context-param>
  9. <listener>
  10. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  11. </listener>

根据/log4j/log4j.properties目录建立文件

# log4j.properties

这里一定要注意,路径的斜杠要写成\而不是\,如果不这样写,似乎无法输出!

  1. # debug<info<warn<error
  2. log4j.rootLogger=debug,CONSOLE,FILE
  3. #log4j.debug=true
  4. # CONSOLE is set to be a ConsoleAppender using a PatternLayout.
  5. log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
  6. log4j.appender.CONSOLE.Threshold=debug
  7. log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
  8. log4j.appender.CONSOLE.layout.ConversionPattern=%d %p [%c] - <%m>%n
  9. log4j.appender.FILE=org.apache.log4j.RollingFileAppender
  10. log4j.appender.FILE.File=D:\\log\\error.log
  11. log4j.appender.FILE.MaxFileSize=10000KB
  12. log4j.appender.FILE.MaxBackupIndex=1000
  13. log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
  14. log4j.appender.FILE.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
  15. log4j.logger.org.apache.ibatis=ERROR
  16. log4j.logger.org.apache.ibatis.common.jdbc.SimpleDataSource=ERROR
  17. log4j.logger.org.apache.ibatis.common.jdbc.ScriptRunner=ERROR
  18. log4j.logger.org.apache.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=ERROR
  19. log4j.logger.java.sql.Connection=ERROR
  20. log4j.logger.java.sql.Statement=ERROR
  21. log4j.logger.java.sql.PreparedStatement=ERROR
  22. log4j.logger.org.mybatis.spring = ERROR
  23. log4j.logger.org.springframework=ERROR
  1. package com.pollux.controller;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import com.pollux.pojo.user;
  10. import com.pollux.service.userservice;
  11. @Controller
  12. public class Testlog4j {
  13. Logger logger = LoggerFactory.getLogger(Testlog4j.class);
  14. @Autowired
  15. private userservice userservice;
  16. @ResponseBody
  17. @RequestMapping(value = "/sayhello")
  18. public String sayhello() {
  19. logger.debug("log4j success!");
  20. return "hello Pollux";
  21. }
  22. }
  1. <!--
  2. 2016-07-18 14:39:38,699 DEBUG [com.pollux.controller.Testlog4j] - <log4j success!>
  3. -->
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注