Servlet中使用Log4j2
因为Servlet常用的版本有两个,即2.5与3.0。要在web application中使用Log4j2,还需要加入log4j-web的jar包。log4j通过web.xml中的context参数log4jConfiguration来查找配置文件。如果没有的话,那么会在WEB-INF目录下查找以“log4j2”开头命名的xml文件。如果不止一个log4j2开头的文件,那么会优先使用log4j2-name命名的文件,name为项目名称。否则会使用第一个文件。
log4j2能够在Servlet3.0下正常使用且不用配置。因为在Servlet3.0的API中加入了ServletContainerInitializer,它自动启动了Filter和ServletContextListener(这两个类在Servlet2.5中需要配置)。官方文档有一个注意事项:
Important Note! For performance reasons, containers often ignore certain JARs known not to
contain TLDs or ServletContainerInitializers and do not scan them for web-fragments and
initializers. Importantly, Tomcat 7 <7.0.43 ignores all JAR files named log4j*.jar, which prevents this
feature from working. This has been fixed in Tomcat 7.0.43, Tomcat 8, and later. In Tomcat 7 <7.0.43
you will need to change catalina.properties and remove "log4j*.jar" from the jarsToSkip property. You may need to do something similar on other containers if they skip scanning Log4j JAR files.
在tomcat版本小于7.0.43的时候,需要修改catalina.properties文件的 jarToSkip配置,把“log4j*.jar”去掉。
如果你不想让log4j2自动启动,那么可以配置isLog4jAutoInitializationDisabled参数。
<context-param>
<param-name>isLog4jAutoInitializationDisabled</param-name>
<param-value>true</param-value>
</context-param>
在Servlet2.5中,需要我们自己配置filter和Listener。配置规则如下:
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener
</listener-class>
</listener>
<filter>
<filter-name>log4jServletFilter</filter-name>
<filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log4jServletFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
<dispatcher>ASYNC</dispatcher><!-- Servlet 3.0 w/ disabled auto-initialization only; not supported in
</filter-mapping>
以上两种情况都允许自定义context parameters。有3个参数可供配置:
isLog4jContextSelectorNamed:布尔类型配置,由它选择是否使用JndiContextSelector。如果它设为true的话,那么log4jContextName一定要配置或者在web.xml中指定display-name。并且log4jConfiguration也要配置一个URL,这个URL是log4j2的配置文件地址,但这个不是必须要配置的。
log4jContextName:配置display-name。
log4jConfiguration:log4j配置文件的路径。
示例如下:
<context-param>
<param-name>isLog4jContextSelectorNamed</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>log4jContextName</param-name>
<param-value>myApplication</param-value>
</context-param>
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>file:///etc/myApp/myLogging.xml</param-value>
</context-param>
这里的log4jConfiguration可以写绝对地址,也可以写相对地址。
Servlet中使用Log4j2的更多相关文章
- Servlet规范简介——web框架是如何注入到Servlet中的
Servlet规范简介--web框架是如何注入到Servlet中的 引言 Web框架一般是通过一个Servlet提供统一的请求入口,将指定的资源映射到这个servlet,在这个servlet中进行框架 ...
- web.xml中在Servlet中获取context-param和init-param内的参数
引自:http://blog.csdn.net/yakson/article/details/9203231 web.xml里面可以定义两种参数:1.application范围内的参数,存放在serv ...
- 关于spring中无法将service注入到servlet中的问题
首先,servlet是动态网页项目区别于普通的java项目的,是动态网页项目中web.xml主要配置文件管理的,而spring只能管理普通的pojo,而没办法直接注入,尽管你的注入方式和配置方式都没有 ...
- JSP+Servlet中使用jspsmartupload.jar进行图片上传下载
JSP+Servlet中使用cos.jar进行图片上传 upload.jsp <form action="FileServlet" method="post&quo ...
- Servlet中转发和重定向的区别
Servlet中页面的跳转有两种方式:转发和重定向. 1.转发和重定向的区别 ①转发是服务器行为,重定向是客户端行为. ②转发是浏览器发生了一次请求,重定向至少是两次请求. ③转发地址栏中的url不会 ...
- [转]servlet中的service, doGet, doPost方法的区别和联系
原文地址:http://m.blog.csdn.net/blog/ghyg525/22928567 大家都知道在javax.servlet.Servlet接口中只有init, service, des ...
- servlet中请求转发(forword)与重定向(sendredirect)的区别
摘自:http://www.cnblogs.com/CodeGuy/archive/2012/02/13/2349970.html 通俗易懂 servlet请求转发与重定向的区别: request.s ...
- servlet中的相对路径和绝对路径 及/, ./, ../的区别
./ 当前目录../ 父级目录/ 根目录资源寻找都是依靠路径,资源存储方式是按照哈希表运算的,所以路径的计算其实就是哈希值的计算. servlet中,所有路径的配置都要用绝对路径. 什么是绝对路径,就 ...
- JSP+Servlet中使用cos.jar进行图片上传(文件上传亦然)
链接:JSP+Servlet中使用jspsmartupload.jar进行图片上传下载 关于cos.jar,百度百科只有这么几句话(http://baike.baidu.com/subview/406 ...
随机推荐
- ios系统的中arm指令集
arm结构处理器,几乎所有的手机都基于arm,其在嵌入式系统中应用非常广泛. ARM 处理器因为低功耗和小尺寸而闻名,它的性能在同等功耗的产品中也很出色.这里我们注意一点,模拟器并不运行arm代码,软 ...
- 创建线程方式-NSOperation
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- EXT学习之——获取下拉框combobox的值与显示名
//申请科室 var comboboxdept = new Ext.form.ComboBox({ xtype: "combobox", name: "Gender&qu ...
- 纸上谈兵:排序算法简介及C实现
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 排序算法(Sorting Algorithm)是计算机算法的一个组成部分. 排序的 ...
- js进度条实现
1.先设置CSS样式(可自定义) /*#region 进度条 */ .progbar { background-color: #e1e1e1; width:auto; color: #222; hei ...
- 树莓派B+上手小记--使用HDMI线连接显示器
入手还算比较顺利,一开始使用网上下的别人精简的OS,发现ACT及PWR灯一直亮着,上网查说用HDMI连接显示器需要修改配置文件config.txt,但修改后情况依旧. 如果还是用官方的系统试试吧,上网 ...
- netstat大量time_wait连接
http://chembo.iteye.com/blog/1503770 http://www.2cto.com/os/201007/54067.html http://blog.csdn.net/d ...
- POJ(3468)
A Simple Problem with Integers http://poj.org/problem?id=3468 #include <stdio.h> struct node { ...
- 浅谈Eclipse寻找JVM(JRE)的顺序机制(转)
转自:http://blog.csdn.net/teedry/article/details/6666850 Eclipse也是一个普通的Java程序,因此必须有一个JRE做为运行环境. ...
- [翻译]Shape comparison language
link: http://www.cnblogs.com/yhlx125/p/3635623.html Shape comparison language 首先说说我遇到的一个问题: IR ...