欢迎光临
一个有态度、有温度的分享型博客

在Java EE Web应用中使用log4j2时web.xml的具体配置

Servlet 3.0或者更新的Web应用

如果你的web.xml配置 “中的version为3.0或更高,而且应用运行在Tomcat7.0以上的版本中,那么就不需要额外的配置了。

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <context-param>
        <param-name>log4jConfiguration</param-name>
        <param-value>classpath:config/log4j2/log4j2.xml</param-value>
    </context-param>
</web-app>

Servlet 2.5版本

注意:log4j2是不支持2.4版本以及更旧的版本的。

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          version="2.5">
    <context-param>
        <param-name>log4jConfiguration</param-name>
        <param-value>classpath:config/log4j2/log4j2.xml</param-value>
    </context-param>
    <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 2.5 -->
    </filter-mapping>
</web-app>

参考:

  1. https://logging.apache.org/log4j/2.0/manual/webapp.html;

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址