因为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的更多相关文章

  1. Servlet规范简介——web框架是如何注入到Servlet中的

    Servlet规范简介--web框架是如何注入到Servlet中的 引言 Web框架一般是通过一个Servlet提供统一的请求入口,将指定的资源映射到这个servlet,在这个servlet中进行框架 ...

  2. web.xml中在Servlet中获取context-param和init-param内的参数

    引自:http://blog.csdn.net/yakson/article/details/9203231 web.xml里面可以定义两种参数:1.application范围内的参数,存放在serv ...

  3. 关于spring中无法将service注入到servlet中的问题

    首先,servlet是动态网页项目区别于普通的java项目的,是动态网页项目中web.xml主要配置文件管理的,而spring只能管理普通的pojo,而没办法直接注入,尽管你的注入方式和配置方式都没有 ...

  4. JSP+Servlet中使用jspsmartupload.jar进行图片上传下载

    JSP+Servlet中使用cos.jar进行图片上传 upload.jsp <form action="FileServlet" method="post&quo ...

  5. Servlet中转发和重定向的区别

    Servlet中页面的跳转有两种方式:转发和重定向. 1.转发和重定向的区别 ①转发是服务器行为,重定向是客户端行为. ②转发是浏览器发生了一次请求,重定向至少是两次请求. ③转发地址栏中的url不会 ...

  6. [转]servlet中的service, doGet, doPost方法的区别和联系

    原文地址:http://m.blog.csdn.net/blog/ghyg525/22928567 大家都知道在javax.servlet.Servlet接口中只有init, service, des ...

  7. servlet中请求转发(forword)与重定向(sendredirect)的区别

    摘自:http://www.cnblogs.com/CodeGuy/archive/2012/02/13/2349970.html 通俗易懂 servlet请求转发与重定向的区别: request.s ...

  8. servlet中的相对路径和绝对路径 及/, ./, ../的区别

    ./ 当前目录../ 父级目录/ 根目录资源寻找都是依靠路径,资源存储方式是按照哈希表运算的,所以路径的计算其实就是哈希值的计算. servlet中,所有路径的配置都要用绝对路径. 什么是绝对路径,就 ...

  9. JSP+Servlet中使用cos.jar进行图片上传(文件上传亦然)

    链接:JSP+Servlet中使用jspsmartupload.jar进行图片上传下载 关于cos.jar,百度百科只有这么几句话(http://baike.baidu.com/subview/406 ...

随机推荐

  1. 关于Listview布局的一点经验

    1.尽量是给item一个固定高度,最外层不要设高度,里面套一层设置一个固定高度:如果用wrap_content的话,之后用alignTop等会出奇怪的问题. 2.如果要使用alignTop align ...

  2. 【练习】移动数据----infile *

    要求: ①指定bad文件: ②挂在之前将目标表delete: ③导入的的数据在控制文件中. 1.创建目录对象: :: SYS@ORA11GR2>create or replace directo ...

  3. 【练习】flushback基于时间的闪回查询

    1.创建table t1 :: SCOTT@ORA11GR2>create table t1 as select * from scott.emp; Table created. :: SCOT ...

  4. 洛谷P3372 【模板】线段树 1

    P3372 [模板]线段树 1 153通过 525提交 题目提供者HansBug 标签 难度普及+/提高 提交  讨论  题解 最新讨论 [模板]线段树1(AAAAAAAAA- [模板]线段树1 洛谷 ...

  5. C++ malloc new 的区别

    1,malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符.它们都可用于申请动态内存和释放内存. malloc实现了在堆内存管理中进行按需分配的机制,但是它不提供在C ...

  6. C++学习基础三——迭代器基础

    迭代器分为两种:一种是iterator,另一种是const_iterator.两者都可进行访问容器中的元素,不同之处是:(1)const_iterator类型只能用于读取容器内的元素,不能更改其值:而 ...

  7. Mysql中将日期转化为毫秒

    一:将毫秒值转化为指定日期格式 使用MYSQL自带的函数FROM_UNIXTIME(unix_timestamp,format). 举例: select FROM_UNIXTIME(136417651 ...

  8. OpenSSL命令系列

    1.1 ssl命令系列前言 openssl命令的格式是"openssl command command-options args",command部分有很多种命令,这些命令需要依赖 ...

  9. 三星framebuffer驱动代码分析

    一.驱动总体概述 本次的驱动代码是Samsung公司为s5pv210这款SoC编写的framebuffer驱动,对应于s5pv210中的内部外设Display Controller (FIMD)模块. ...

  10. 深入理解js——执行上下文

    什么是"执行上下文"?暂且不下定义,先看一段代码: 第一句报错,a未定义,很正常.第二句.第三句输出都是undefined,说明浏览器在执行console.log(a)时,已经知道 ...