简体   繁体   English

未加载log4j2配置

[英]log4j2 configuration is not loaded

Can you help me spotting out the missing step for my configuration? 你能帮我找出配置中缺少的步骤吗?

I'm trying to add logger to my very simple web application: in order to do so I'm using log4j2 (beta9). 我正在尝试将logger添加到我非常简单的Web应用程序中:为了这样做,我使用的是log4j2(beta9)。

I wrote down my log4j2.xml as follows 我写下我的log4j2.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} /%L/[%M] - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="prova.Hello" level="trace" additivity="false">
          <AppenderRef ref="Console"/>
        </Logger>
        <Logger name="servletstest.TestLogger" level="trace" additivity="false">
          <AppenderRef ref="Console"/>
        </Logger>
        <Root level="trace">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

and I put it in the WEB-INF folder. 我把它放在WEB-INF文件夹中。

I have then a simple servlet which does the following 我有一个简单的servlet,它执行以下操作

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
    /* TODO output your page here. You may use following sample code. */
    out.println("<!DOCTYPE html>");
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet TestLogger</title>");            
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet TestLogger at " + request.getContextPath() + "</h1>");
    out.println("</body>");
    out.println("</html>");
    logger.error("error");
    logger.info("info");
    logger.trace("trace");
    logger.debug("debug");
} finally {
    out.close();
}

As you can see it is just a try to see if the logger works as expected, but it does not. 正如您所看到的,只是尝试查看记录器是否按预期工作,但事实并非如此。 By reading here web app I don't need to configure the web.xml of my application because it runs servlet 3.0, infact if I try to add the configuration I get the following error 通过阅读这里的web应用程序我不需要配置我的应用程序的web.xml,因为它运行servlet 3.0,事实上如果我尝试添加配置我得到以下错误

javax.servlet.UnavailableException: In a Servlet 3.0+ application, you must not define a log4jServletFilter in web.xml. Log4j 2 defines this for you automatically.

But it seems not to load the log4j2 configuration file, since the output I excpect is similar to 但似乎没有加载log4j2配置文件,因为我所感知的输出类似于

10:57:55.490 [http-bio-8084-exec-5] ERROR servletstest.TestLogger - error
10:57:55.490 [http-bio-8084-exec-5] INFO servletstest.TestLogger - info
10:57:55.490 [http-bio-8084-exec-5] TRACE servletstest.TestLogger - trace
10:57:55.490 [http-bio-8084-exec-5] DEBUG servletstest.TestLogger - debug

instead I get only 相反,我只得到

10:57:55.490 [http-bio-8084-exec-5] ERROR servletstest.TestLogger - error

it looks like is not able to load the xml file and it sets by default the logger level to error only. 它看起来像是无法加载xml文件,它默认情况下将记录器级别设置为仅错误。 In the same web page is written 在同一个网页上写

In Tomcat 7 <7.0.43 you will need to change catalina.properties and remove "log4j*.jar" from the jarsToSkip property. 在Tomcat 7 <7.0.43中,您需要更改catalina.properties并从jarsToSkip属性中删除“log4j * .jar”。 You may need to do something similar on other containers if they skip scanning Log4j JAR files. 如果他们跳过扫描Log4j JAR文件,您可能需要在其他容器上执行类似的操作。

so I edited the catalina.properties becasuse I'm running tomcat 7.0.41 and I removed the log4j*.jar from the jartoskip property. 所以我编辑了catalina.properties因为我正在运行tomcat 7.0.41而我从jartoskip属性中删除了log4j * .jar。

What am I doing wrong? 我究竟做错了什么?

by the way in the web.xml I have this 顺便说一句,在web.xml中我有这个

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
    <display-name>test</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>prova</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>TestLogger</servlet-name>
        <servlet-class>servletstest.TestLogger</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>TestLogger</servlet-name>
        <url-pattern>/TestLogger</url-pattern>
    </servlet-mapping>
     <listener>
        <listener-class>org.apache.logging.log4j.core.web.Log4jServletContextListener</listener-class>
    </listener>

</web-app>

You can notice that version is 2.5 and not properly 3.0 is perhaps this affecting me somehow? 你可以注意到version是2.5而不正确3.0也许这会影响我吗? or jersey configuration? 还是球衣配置?

EDIT: I tried again and changed the web.xml to the following in order to have a web app version of 3.0 but it does not load the configuration.. what am I doing wrong? 编辑:我再次尝试并将web.xml更改为以下内容,以便有一个3.0的Web应用程序版本但它没有加载配置..我做错了什么?

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
    <servlet>
        <servlet-name>Prova</servlet-name>
        <servlet-class>test.Prova</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Prova</servlet-name>
        <url-pattern>/Prova</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

thanks in advance for the help. 在此先感谢您的帮助。

正如我在评论中所猜测的那样,它需要放在WEB-INF/classes ,而不是WEB-INF

If you are using eclipse ide for developing purpose you can put the log4j2.xml file in src folder. 如果您正在使用eclipse ide进行开发,则可以将log4j2.xml文件放在src文件夹中。

在此输入图像描述

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

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