Log4j is logged in log4j.log instead of the specified file name

I am very new to log4j, so please be careful. But here's what happens, and I don’t know why: it logs the file correctly, but the name of the created log seems incorrect.

Here is my log4j configurator:

<?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 the log4j.log file in the log folder instead of the messagecount.log file. Does this property affect what I think?

This is how I start the recorder:

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 configuration file is in the root of my src folder.

thanks

+4
source share
3 answers

Got! Here's what happened, and I would like to first thank darioo and dogbane for their answers / comments, as they helped me understand this.

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

So, if I get rid of the init function and put the correct config xml in the bin folder, it will work.

0
source

It works for me.

Try adding -Dlog4j.debug=true to your JVM options to get more information about what log4j.log does and why it is logged in log4j.log .

+2
source

I made a copy and paste of your xml file, and for starters, setting log4j throws an error because this file is not a valid xml. You forgot to add

 </log4j:configuration> 

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

After fixing, log4j launches a fine and correctly creates a file called log/messagecount.log .

I am using log4j 1.2.15. If your settings do not match, try publishing the version of log4j and your environment. Also, try naming your file differently, for example log/somelog.log or just somelog.log so you can see how somelog.log behaves.

0
source

All Articles