4

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

1
  • 1
    You don't need that init function. Delete it. Just put log4j.xml in your application's classpath (bin or build folder, it won't be found in src folder) and your app will find it automatically. Commented Dec 7, 2010 at 18:09

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

Whoops! copy-paste error. Also, I got log4j today, off their website, so it's version 1.2.16
@Matthew: tried with 1.2.16. Result is the same. Post more info about your environment.
@Nathan: good point. For new projects I always recommend slf4j + logback.
I tried changing the name to somelog.log and it still created a log called "log4j.log" with the appropriate contents: "[12:55:10,896] (main) INFO [MessageCount] - ----------Logger init-----------" I will edit my post to show how I call it in my java class.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.