4.Spring中使用Log4j
转自: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的更多相关文章
- spring中加入log4j
spring中加入log4j <context-param> <param-name>log4jConfigLocation</param-name> <pa ...
- 玩转Spring MVC(五)----在spring中整合log4j
在前边的基础上,本文主要总结一下如何在spring 中配置log4j,在本文末尾会给出完整项目的链接. 首先是web.xml中要新添加的代码: <!-- 6. 配置log4j --> &l ...
- Spring中使用log4j随笔
web.xml中的配置: <!--由Sprng载入的log4j配置文件位置--> <context-param> <param-name>log4jConfigLo ...
- Spring 中配置log4j日志功能
一,添加log4j依赖包 可从官网上下载该依赖包log4j-x.x.xx.jar,下载后 build path,添加依赖包 二,创建 log4j.properties 配置文件 log4j.prope ...
- Spring中使用Log4j记录日志
以下内容引用自http://wiki.jikexueyuan.com/project/spring/logging-with-log4j.html: 例子: pom.xml: <project ...
- spring中log4j的使用---转载
原文链接:http://www.codeceo.com/article/log4j-usage.html 日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供 ...
- Spring boot中使用log4j
我们知道,Spring Boot中默认日志工具为logback,但是对于习惯了log4j的开发者,Spring Boot依然可以很好的支持,只是需要做一些小小的配置功能.Spring Boot使用lo ...
- Spring boot中使用log4j记录日志
之前在Spring Boot日志管理一文中主要介绍了Spring Boot中默认日志工具(logback)的基本配置内容.对于很多习惯使用log4j的开发者,Spring Boot依然可以很好的支持, ...
- Spring Boot中对log4j进行多环境不同日志级别的控制
之前介绍了在<Spring boot中使用log4j记录日志>,仅通过log4j.properties对日志级别进行控制,对于需要多环境部署的环境不是很方便,可能我们在开发环境大部分模块需 ...
随机推荐
- 【BZOJ1702】[usaco2007margold]队列平衡
原来蛤习线性探测的时候要每次加一个稍大一点的数而不是每次+1……连蛤习都不会写,我tmd是有多弱 原题: N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色, ...
- Hadoop storm大数据分析 知识体系结构
最近工作工作有用到hadoop 和storm,最近看到一个网站上例句的hadoop 和storm的知识体系.所以列出来供大家了解和学习.来自哪个网站就不写了以免以为我做广告额. 目录结构知识点还是挺全 ...
- stardog 基本试用(社区版)
stardog 是一个知识图谱的实现,实现了sparql 以及graphql 协议,使用起来也比较简单,官方文档挺全 下载 社区版,注册之后会有邮件通知,里面会包含license 以及软件包 下载地址 ...
- SocketIO4Net.Client
Project Description SocketIO4Net.Client Update as of 11/02/2013 A develop branch is up at https://gi ...
- /etc/inittab 学习
1.文件内容 2.内容讲解 http://www.2cto.com/os/201108/98426.html init的进程号是1(ps -aux | less),从这一点就能看出,init进程是系统 ...
- C语言sscanf用法解析与正则表达式支持
最近学习算法和输入输出用到的基本知识,首先是我自己写的一份代码参考和学习了很多资源 后面会给出参考资料,他们写得更加详细,正则表达式的支持确实是一大亮点所在 #include<iostream& ...
- Zigbee 的 mesh功能设置
1. 在编译选项中加入ZIGBEEPRO 最大的节点深度为20,网络模式为mesh网络 如果不配置,则为HOME_CONTROLS,支持5级路由深度,每个路由器最多可连接20个节点(最多包括6个路由器 ...
- linux 标准I/O (二)
<Uinx 环境高级编程笔记> 以前经常遇到两种I/O操作 一类是f打头的fopen, fread, fwrite 一类是没有f打头的open, read, fwrite 原来一个是U ...
- 蓝桥杯 算法训练 ALGO-116 最大的算式
算法训练 最大的算式 时间限制:1.0s 内存限制:256.0MB 问题描述 题目很简单,给出N个数字,不改变它们的相对位置,在中间加入K个乘号和N-K-1个加号,(括号随便加)使最终结果尽量 ...
- bzoj2565 最长双回文子串
Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为“abc”,逆序为“cba”,不相同).输入长度为n的串S,求S的最长双回文子串T, ...