简体   繁体   English

在Servlet 3.0应用程序中使用log4j2

[英]Using log4j2 in Servlet 3.0 application

I am trying to use Log4j2 in my Servlet 3.0 web application. 我正在尝试在Servlet 3.0 Web应用程序中使用Log4j2。 Even after configuring everything as per the official documentation, I am not able to see the logs. 即使按照官方文档配置了所有内容,我也看不到日志。

This is my web.xml: 这是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <display-name>myapplication</display-name>

    <context-param>
        <param-name>isLog4jAutoInitializationDisabled</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>isLog4jContextSelectorNamed</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfiguration</param-name>
        <param-value>log4j2.xml</param-value>
    </context-param>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

I have placed the log4j2.xml file in WEB-INF/classes: 我已经将log4j2.xml文件放在WEB-INF / classes中:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <File name="MyFile" fileName="myapplication.log" immediateFlush="true" append="false">
            <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="MyFile"/>
        </Root>
    </Loggers>
</Configuration>

This is how I am calling logger in my class: 这是我在课堂上给记录器打电话的方式:

final static Logger LOGGER = Logger.getLogger(MyClass.class);
LOGGER.info("Logging works");

Please tell me what I am doing wrong. 请告诉我我在做什么错。 I have even tried hardcoding the direct path to the log4j2.xml, but still it does not work. 我什至尝试过硬编码log4j2.xml的直接路径,但仍然无法正常工作。

With Servlet 3.0 , set isLog4jAutoInitializationDisabled to false , or remove it, and it should work . Servlet 3.0 ,将isLog4jAutoInitializationDisabled设置为false或将其删除,它应该可以工作。

If you keep it to true , you will have additional steps and things to add to web.xml : 如果您将其设置为true ,则将有其他步骤和要添加到web.xml

Once you disable auto-initialization, you must initialize Log4j as you would a Servlet 2.5 web application. 禁用自动初始化后,必须像处理Servlet 2.5 Web应用程序一样初始化Log4j。

And

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。

See here Log4j2 and Servlet 3.0 看到这里Log4j2和Servlet 3.0

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

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