Log4j 记录到 log4j.log 而不是指定的文件名

发布于 2024-10-06 07:54:03 字数 1935 浏览 0 评论 0原文

我对 log4j 很陌生,所以请温柔一点。但这是发生的事情,我不知道为什么:它正确记录到文件,但创建的日志的文件名似乎是错误的。

这是我的 log4j 配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{HH:mm:ss,SSS}] [%-5p] (%t) [%c{1}] %m%n"/>
    </layout>
</appender>


<appender name="file" class="org.apache.log4j.FileAppender">
    <param name="File" value="log/messagecount.log" />
    <param name="Append" value="true" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{HH:mm:ss,SSS}] [%-5p] (%t) [%c{1}] - %m%n"/>
    </layout>           
 </appender>    

<root>
    <level value="debug"/>      
    <appender-ref ref="file"/>
    <!-- <appender-ref ref="rolling"/> -->
</root>

</log4j:configuration>

它在日志文件夹下创建一个 log4j.log 文件,而不是 messagecount.log 文件。 该价值属性是否没有达到我的预期?

这就是我初始化记录器的方式:

类级别变量:

private static Logger logger = Logger.getLogger( MessageCount.class );

和初始化函数:

private void initLogger() throws IOException {

    Properties props = new Properties();
    props.load(getClass().getResourceAsStream("/log4j.xml"));
    PropertyConfigurator.configure(props);

    logger.info( "----------Logger init-----------" ) ;

 //     logger.debug("Sample debug message");
 //     logger.info("Sample info message");
 //     logger.warn("Sample warn message");
 //     logger.error("Sample error message");
 //     logger.fatal("Sample fatal message");

}

log4j.xml 配置文件位于我的 src 文件夹的根目录中。

谢谢

I am very new to log4j, so please be gentle. But here is what's happening and I don't know why: it's logging correctly to a file, but the filename of the created log seems to be wrong.

Here is my log4j config:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{HH:mm:ss,SSS}] [%-5p] (%t) [%c{1}] %m%n"/>
    </layout>
</appender>


<appender name="file" class="org.apache.log4j.FileAppender">
    <param name="File" value="log/messagecount.log" />
    <param name="Append" value="true" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{HH:mm:ss,SSS}] [%-5p] (%t) [%c{1}] - %m%n"/>
    </layout>           
 </appender>    

<root>
    <level value="debug"/>      
    <appender-ref ref="file"/>
    <!-- <appender-ref ref="rolling"/> -->
</root>

</log4j:configuration>

It creates a log4j.log file under the log folder instead of a messagecount.log file.
Does that value property not do what I think it does?

This is how I init the logger:

Class level variable:

private static Logger logger = Logger.getLogger( MessageCount.class );

And the init function:

private void initLogger() throws IOException {

    Properties props = new Properties();
    props.load(getClass().getResourceAsStream("/log4j.xml"));
    PropertyConfigurator.configure(props);

    logger.info( "----------Logger init-----------" ) ;

 //     logger.debug("Sample debug message");
 //     logger.info("Sample info message");
 //     logger.warn("Sample warn message");
 //     logger.error("Sample error message");
 //     logger.fatal("Sample fatal message");

}

The log4j.xml config file is in the root of my src folder.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遇见了你 2024-10-13 07:54:03

对我有用。

尝试将 -Dlog4j.debug=true 添加到 JVM 参数中,以获取有关 log4j 正在执行的操作以及为何记录到 log4j.log 的更多信息。

Works for me.

Try adding -Dlog4j.debug=true to your JVM parameters to get more information on what log4j is doing and why it is logging to log4j.log.

找个人就嫁了吧 2024-10-13 07:54:03

我复印了一份&粘贴您的 xml 文件,对于初学者来说,配置 log4j 会引发错误,因为该文件不是有效的 xml。您忘记添加

</log4j:configuration>

为最后一行。这可能是您问题的根源吗?

修复此问题后,log4j 可以正常启动并正确创建名为 log/messagecount.log 的文件。

我正在使用 log4j 1.2.15。如果您的设置不同,请尝试发布您的 log4j 版本和环境。另外,尝试以不同的方式命名您的文件,例如 log/somelog.log,或只是 somelog.log,以便您可以了解 log4j 的行为。

I made a copy & paste of your xml file and, for starters, configuring log4j throws an error because this file isn't a valid xml. You forgot to add

</log4j:configuration>

as the last line. Could have this been the source of your problem?

After fixing this, log4j starts up fine and correctly creates a file named log/messagecount.log.

I'm using log4j 1.2.15. If your setup isn't the same, try posting your log4j version and your environment. Also, try naming your file differently, like log/somelog.log, or just somelog.log so you can see how log4j behaves.

橘亓 2024-10-13 07:54:03

知道了!这就是发生的事情,我首先要感谢 darioo 和 Dogbane 的回答/评论,因为他们帮助我解决了这个问题。

不需要 init 函数。使用 -Dlog4j.debug=true 作为 JVM 参数表明它已经加载了位于 bin 文件夹中的 xml 配置文件的一些副本。

因此,如果我去掉 init 函数,并将正确配置 xml 放入 bin 文件夹中,它就可以工作。

Got it! Here's what was happening, and I would like to first thank darioo and dogbane for their answers/comments since they helped me figure it out.

The init function is not needed. And using -Dlog4j.debug=true as a JVM parameter showed me that it was already loading some copy of the xml config file that was located in the bin folder.

So if I get rid of the init function, and place the correct config xml in the bin folder, it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文