转自:https://blog.csdn.net/luohai859/article/details/52250807

这里要实现web项目中利用Spring来使用Log4j

(1)接上面的工程,然后再导入Spring的包

(2)web.xml增加

 <!-- 设置根目录 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param> <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<!-- 3000表示 开一条watchdog线程每60秒扫描一下配置文件的变化;这样便于日志存放位置的改变 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>3000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

整个内容如下:

 <?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"
id="WebApp_ID" version="3.0">
<display-name>LogLearning</display-name> <servlet>
<servlet-name>Log4JTestServlet</servlet-name>
<servlet-class>com.mucfc.Log4JTestServlet</servlet-class>
</servlet> <!--用来启动 log4jConfigLocation的servlet -->
<!-- <servlet>
<servlet-name>Log4JInitServlet</servlet-name>
<servlet-class>com.mucfc.Log4JInitServlet</servlet-class>
<init-param>
<param-name>log4j-properties-location</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>--> <servlet-mapping>
<servlet-name>Log4JTestServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping> <!-- Spring 容器加载 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 设置根目录 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param> <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<!-- 3000表示 开一条watchdog线程每60秒扫描一下配置文件的变化;这样便于日志存放位置的改变 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>3000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener> </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

然后打开文件

4.Spring中使用Log4j的更多相关文章

  1. spring中加入log4j

    spring中加入log4j <context-param> <param-name>log4jConfigLocation</param-name> <pa ...

  2. 玩转Spring MVC(五)----在spring中整合log4j

    在前边的基础上,本文主要总结一下如何在spring 中配置log4j,在本文末尾会给出完整项目的链接. 首先是web.xml中要新添加的代码: <!-- 6. 配置log4j --> &l ...

  3. Spring中使用log4j随笔

    web.xml中的配置: <!--由Sprng载入的log4j配置文件位置--> <context-param> <param-name>log4jConfigLo ...

  4. Spring 中配置log4j日志功能

    一,添加log4j依赖包 可从官网上下载该依赖包log4j-x.x.xx.jar,下载后 build path,添加依赖包 二,创建 log4j.properties 配置文件 log4j.prope ...

  5. Spring中使用Log4j记录日志

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/logging-with-log4j.html: 例子: pom.xml: <project ...

  6. spring中log4j的使用---转载

    原文链接:http://www.codeceo.com/article/log4j-usage.html 日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供 ...

  7. Spring boot中使用log4j

    我们知道,Spring Boot中默认日志工具为logback,但是对于习惯了log4j的开发者,Spring Boot依然可以很好的支持,只是需要做一些小小的配置功能.Spring Boot使用lo ...

  8. Spring boot中使用log4j记录日志

    之前在Spring Boot日志管理一文中主要介绍了Spring Boot中默认日志工具(logback)的基本配置内容.对于很多习惯使用log4j的开发者,Spring Boot依然可以很好的支持, ...

  9. Spring Boot中对log4j进行多环境不同日志级别的控制

    之前介绍了在<Spring boot中使用log4j记录日志>,仅通过log4j.properties对日志级别进行控制,对于需要多环境部署的环境不是很方便,可能我们在开发环境大部分模块需 ...

随机推荐

  1. vault 集群搭建(active standby 模式)

        参考架构图: consul server cluster 搭建 consul 基本配置格式 { "server": true, "node_name": ...

  2. centos下yum安装pip失败

    [root@wfm ~]# yum -y install pip Loaded plugins: fastestmirror, refresh-packagekit, securityLoading ...

  3. chrome浏览器network面板出现:Provisional headers are shown 提示

    一般来说,如果看到这个提示,说明这个请求并没有发送出去. 具体原因有多种: 请求被某些扩展如 Adblock 拦截了 请求被墙了 走本地缓存或者 dataurl 的请求 client发送请求后,由于各 ...

  4. yarn 制作 npm 包

    yarn publish yarn info [package_name]

  5. POJ2559最大矩形面积——单调栈

    题目:http://poj.org/problem?id=2559 #include<iostream> #include<cstdio> using namespace st ...

  6. URL中的hash(井号)

    1.#的含义 #代表网页中的一个位置,其右边的字符,就是该位置的标识符.比如 http://www.example.com/index.html#print 就是代表index.html中的print ...

  7. Microsoft Dynamics CRM4.0 和 Microsoft Dynamics CRM 2011 JScript 方法对比

    CRM 2011 如果需要再IE里面调试,可以按F12在前面加上contentIFrame,比如 contentIFrame.document.getElementById("字段" ...

  8. Windows 7 With Sp1 简体中文旗舰版

    Windows 7 With Sp1 简体中文旗舰版(MSDN官方原版) 安装Windows 7对于硬件配置的基本要求: •1 GHz 32 位或 64 位处理器 •1 GB 内存(基于32 位)或 ...

  9. C++ 常用函数方法

    /* * 拆分字符串 * 参数: * strData 字符串 * split 分隔符 * 返回: * 返回动态数组std::vector<std::string> ,记得要delete 内 ...

  10. 把普通的JavaProject变成MavenProject

    1,安装Maven Eclipse插件 2,在项目上点右键->Configure->Convert to maven project 3,在项目上点右键->Run as->Ru ...