I am loading the configuration file using
java.net.URL url=Thread.currentThread().getContextClassLoader().getResource("MyLog4j.xml");
url=Thread.currentThread().getContextClassLoader().getResource("MyLog4j.xml");
PropertyConfigurator.configure(url);
When running this program with log4j debug on its giving this error.
log4j: Reading configuration from URL file:/F:/TestApp/WEB-INF/classes/MyLog4j.xml
log4j: Could not find root logger information. Is this OK?
log4j: Finished configuring.
log4j:WARN No appenders could be found for logger (MyServlet).
log4j:WARN Please initialize the log4j system properly.
I have put the MyLog4j.xml in WebApp/WEB-INF/classes
This is the contents of my MyLog4j.xml
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="appender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="File" value="F:/MyLogs/MyAppLogs.log"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
</layout>
</appender>
<root>
<priority value ="debug"/>
<appender-ref ref="appender"/>
</root>
</log4j:configuration>
I have searched many question on this topic on SO and not getting it work. Can someone please help me here?
EDIT If i change the code to configure using properties files, then its working fine. Contents of properties file
# This sets the global logging level and specifies the appenders
log4j.rootLogger=INFO, myConsoleAppender
# settings for the console appender
log4j.appender.myConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.myConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
The issue is that you're using the PropertyConfigurator
to configure the MyLog4j.xml
file. For XML files, you need to use the DOMConfigurator
.
DOMConfigurator.configure(url);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With