转自:https://blog.csdn.net/luohai859/article/details/52250807
这里要实现web项目中利用Spring来使用Log4j
(1)接上面的工程,然后再导入Spring的包
(2)web.xml增加
1 <!-- 设置根目录 --> 2 <context-param> 3 <param-name>webAppRootKey</param-name> 4 <param-value>webapp.root</param-value> 5 </context-param> 6 7 <context-param> 8 <param-name>log4jConfigLocation</param-name> 9 <param-value>/WEB-INF/classes/log4j.properties</param-value> 10 </context-param> 11 <!-- 3000表示 开一条watchdog线程每60秒扫描一下配置文件的变化;这样便于日志存放位置的改变 --> 12 <context-param> 13 <param-name>log4jRefreshInterval</param-name> 14 <param-value>3000</param-value> 15 </context-param> 16 <listener> 17 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 18 </listener>
整个内容如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 id="WebApp_ID" version="3.0"> 6 <display-name>LogLearning</display-name> 7 8 <servlet> 9 <servlet-name>Log4JTestServlet</servlet-name> 10 <servlet-class>com.mucfc.Log4JTestServlet</servlet-class> 11 </servlet> 12 13 <!--用来启动 log4jConfigLocation的servlet --> 14 <!-- <servlet> 15 <servlet-name>Log4JInitServlet</servlet-name> 16 <servlet-class>com.mucfc.Log4JInitServlet</servlet-class> 17 <init-param> 18 <param-name>log4j-properties-location</param-name> 19 <param-value>/WEB-INF/classes/log4j.properties</param-value> 20 </init-param> 21 <load-on-startup>1</load-on-startup> 22 </servlet>--> 23 24 <servlet-mapping> 25 <servlet-name>Log4JTestServlet</servlet-name> 26 <url-pattern>/test</url-pattern> 27 </servlet-mapping> 28 29 <!-- Spring 容器加载 --> 30 <listener> 31 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 32 </listener> 33 <context-param> 34 <param-name>contextConfigLocation</param-name> 35 <param-value>classpath:applicationContext.xml</param-value> 36 </context-param> 37 38 <!-- 设置根目录 --> 39 <context-param> 40 <param-name>webAppRootKey</param-name> 41 <param-value>webapp.root</param-value> 42 </context-param> 43 44 <context-param> 45 <param-name>log4jConfigLocation</param-name> 46 <param-value>/WEB-INF/classes/log4j.properties</param-value> 47 </context-param> 48 <!-- 3000表示 开一条watchdog线程每60秒扫描一下配置文件的变化;这样便于日志存放位置的改变 --> 49 <context-param> 50 <param-name>log4jRefreshInterval</param-name> 51 <param-value>3000</param-value> 52 </context-param> 53 <listener> 54 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 55 </listener> 56 57 </web-app>
这里Log4JInitServlet.java就相当于没用到了。
(2)applicationContext.xml
没有内容:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> </beans>
(3)这样日志就跟随Spring窗口启动而启动了
程序一运行,就会自动把日志打印
log.log
error.log为空,因为它只打印error级别以上的信息
浏览器输入http://localhost:8080/LogLearning2/test
然后打开文件