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 ...
随机推荐
- 修复山寨版的J-Link
Fixed J-Link 1. Erase (1) Power On (2) Jump "ERASE"(JP3) (3) Wait for 5s (4) Break ...
- 解决GBK字符转UTF-8乱码问题
通过以下方法将GBK字符转成UTF-8编码格式的byte[]数组 package test; import java.io.UnsupportedEncodingException; public c ...
- delphi 怎么获取工程版本号
function GetApplicationVersion:String; // Added 取得程序版本号 var FileName:String; InfoSize,Wnd:DWORD; Ver ...
- dispay属性的block,inline,inline-block
转自下面的几位大神: http://www.cnblogs.com/KeithWang/p/3139517.html 总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 bl ...
- Android IOS WebRTC 音视频开发总结(七六)-- 探讨直播低延迟低流量的粉丝连麦技术
本文主要探讨基于WebRTC的P2P直播粉丝连麦技术 (作者:郝飞,亲加云CTO,编辑:dora),最早发表在[这里] 支持原创,转载必须注明出处,欢迎关注微信公众号blacker(微信ID:blac ...
- sql case when 操作
,(CASE WHEN (SELECT COUNT(1) FROM BookAuthorizeObject WHERE BookAuthorizeObject.BookNo=T.BookNo)=8 T ...
- codeforces 361 A - Mike and Cellphone
原题: Description While swimming at the beach, Mike has accidentally dropped his cellphone into the wa ...
- makefile多目录的.c 格式.cpp混合编译
# # c.cpp混合编译的makefile模板 # # BIN = test.exe CC = gcc CPP = g++ #这里只加入库头文件路径及库路径 INCS = -I"c:/mi ...
- webform组合查询和分页
1.组合查询(1)数据访问类 //参数1:SQL语句 参数2:哈希表public List<Users> chas(string s,Hashtable has) { List<Us ...
- 转 A Week with Mozilla's Rust
转自http://relistan.com/a-week-with-mozilla-rust/ A Week with Mozilla's Rust I want another systems la ...