Log4j2创建日志文件,但不写入日志文件 [英] Log4j2 creates log file but doesn't write to it

查看:1622
本文介绍了Log4j2创建日志文件,但不写入日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tomcat 8.0.3和log4j 2.0-rc1部署GWT应用程序(Servlet 2.5).

I'm using Tomcat 8.0.3 and log4j 2.0-rc1 to deploy a GWT application (servlet 2.5).

我相信我已修复了常见的日志配置错误,但仍然没有任何内容记录到控制台或文件中.

I believe I fixed the usual log configuration errors, but still nothing is logged to the console or file.

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

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>file:///C:/Users/name/Desktop/log4j2.xml</param-value>
</context-param>

<filter-mapping>
    <filter-name>log4jServletFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

这是 log4j2.xml 配置:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%p] %c{1}: %m%n"/>
        </Console>
        <File name="infoFile" fileName="${sys:catalina.home}/logs/info.log" >
            <PatternLayout pattern="[%p] %c{1}: %m%n"/>
        </File>
    </Appenders>

    <Loggers>
        <Root level="info">
            <Appender-Ref ref="Console"/>
            <Appender-Ref ref="infoFile"/>
        </Root>
    </Loggers>
</Configuration>

我将log4j2 api和core都添加到了我的Maven依赖项中,我可以看到它们在WEB-INF\classes\中爆炸了.我还将它们从catalina.properties的skipJar列表中删除.

I added both the log4j2 api and core to my Maven dependencies and I can see them exploded in WEB-INF\classes\. I've also removed them from the skipJar list in catalina.properties.

展开战争后,在localhost或catalina日志中不会引发任何错误或警告.这是本地主机日志:

When the war is deployed, no errors or warnings are thrown in the localhost or catalina log. This is the localhost log:

INFO  ContextListener: contextInitialized()
INFO  SessionListener: contextInitialized()
INFO  Log4jServletContextListener ensuring that Log4j starts up properly.
INFO  Log4jServletFilter initialized.

奇怪的是, info.log 文件已创建,但从未写入.
我还在使用Hibernate,其日志确实显示在控制台中,这可能是问题吗?

The strange thing is that the info.log file is created, but never written to.
I'm also using Hibernate whose logs do show up in the console, could this be the problem?

最后,这就是我使用记录器的方式:

Finally, this is how I'm using the logger:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

private static final Logger LOG = LogManager.getLogger(MyClass.class.getName());

public void myMethod() {
    LOG.info("Logging some message");
}

(log4j2配置文件临时位于我的桌面上,因为似乎我无法在路径中使用空格)

(The log4j2 config file is temporarily on my desktop because it seems that I can't use spaces in the path)

推荐答案

据我所知,您的配置是正确的,并且所有设置均按照log4j2

As far as I can see your configuration is correct and all is set up as described in the log4j2 manual page for web apps.

您可以尝试做的一件事是将log4j2.xml配置文件放在类路径中(在WEB-INF/classes下),然后删除log4jConfiguration上下文参数设置.但是由于您看到正在创建一个info.log文件,因此很可能已经正确发现了log4j2.xml文件,因此将其放在类路径中可能无济于事. (不过值得一试.)

One thing you could try is putting the log4j2.xml config file in the classpath (under WEB-INF/classes) and removing the log4jConfiguration context-param setting. But since you are seeing an info.log file being created, it is likely that the log4j2.xml file is already discovered correctly, so putting it in the classpath may not help. (Worth a try though.)

我可以想到另外两个原因,为什么日志文件中什么也不显示:日志输出被缓冲,并且没有在每个事件上刷新该缓冲区.但是,FileAppender的默认值为InstantFlush = true,因此这意味着rc1中引入了一个新的错误.这是可能的,但不太可能(当前中继源看起来正确).

Two other reasons I can think of why nothing appears in the log file: log output is buffered and the buffer is not flushed on every event. However, the default for FileAppender is immediateFlush=true, so that would mean a new bug was introduced in rc1. This is possible, but not likely (current trunk source looks correct).

最后,是否有可能不会调用记录日志的方法(在上面的示例中为myMethod)?有没有办法验证此方法是否被调用?

Finally, would it be possible that your method that does the logging (myMethod in your example above) is not called? Is there a way to verify that this method was called?

这篇关于Log4j2创建日志文件,但不写入日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆