简体   繁体   English

Web应用程序中的Log4j2

[英]Log4j2 in Web application

How to configure Log4j2 with a simple java web application? 如何使用简单的Java Web应用程序配置Log4j2? I went through the tutorial in apache website but it did not give any straight forward examples wrt web applications. 我在apache网站上浏览了这个教程,但它没有提供任何Web应用程序的直接示例。 I used it in stand alone applications where I had static variables for loggers. 我在独立的应用程序中使用它,我有记录器的静态变量。 But in Servlet, I think we cannot have instance variables. 但在Servlet中,我认为我们不能拥有实例变量。 Should we use as static variable inside doPost ? 我们应该在doPost中使用静态变量吗?

Also, any pointers to a configured log4j2 web app or tutorial with all the steps will be helpful as I am unable to find a right one. 此外,任何指向已配置的log4j2 Web应用程序或带有所有步骤的教程的指针都会有所帮助,因为我无法找到正确的指针。

I'm running on a Tomcat 6.0 server. 我正在运行Tomcat 6.0服务器。

Servlets 3.0 environment Servlets 3.0环境

It is such simple as to put log4j2.xml into WEB-INF/classes . log4j2.xml放入WEB-INF/classes

From documentation 文档

The Short Story Log4j 2 "just works" in Servlet 3.0 and newer web applications. 短篇小说 Log4j 2“正常工作”在Servlet 3.0和更新的Web应用程序中。 It is capable of automatically starting when the application deploys and shutting down when the application undeploys. 当应用程序部署并在应用程序取消部署时关闭时,它能够自动启动。

Servlets 2.5 environment Servlets 2.5环境

If you are using Log4j in a Servlet 2.5 web application, or if you have disabled auto-initialization with the isLog4jAutoInitializationDisabled context parameter, you must configure the Log4jServletContextListener and Log4jServletFilter in the deployment descriptor or programmatically. 如果在Servlet 2.5 Web应用程序中使用Log4j,或者已使用isLog4jAutoInitializationDisabled上下文参数禁用了自动初始化,则必须在部署描述符中或以编程方式配置Log4jServletContextListener和Log4jServletFilter。 The filter should match all requests of any type. 过滤器应匹配任何类型的所有请求。 The listener should be the very first listener defined in your application, and the filter should be the very first filter defined and mapped in your application. 监听器应该是应用程序中定义的第一个监听器,过滤器应该是应用程序中定义和映射的第一个过滤器。 This is easily accomplished using the following web.xml code: 使用以下web.xml代码可以轻松完成此操作:

<listener>
    <listener-class>org.apache.logging.log4j.core.web.Log4jServletContextListener</listener-class>
</listener>

<filter>
    <filter-name>log4jServletFilter</filter-name>
    <filter-class>org.apache.logging.log4j.core.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>

Then write configuration options: 然后编写配置选项:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM