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
log4j.xmlin your application's classpath (bin or build folder, it won't be found in src folder) and your app will find it automatically.