开发者

Log4j logging to log4j.log instead of specified filename

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"));
    PropertyConfigu开发者_开发知识库rator.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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜