0
votes

I have a problem with log4j under tomcat.

When Eclipse deploys new version of application to tomcat, it reloads application context. After reloading context is completed, web application level logger stops to write to console.

Only tomcat logger still works.

My configuration in log4j.xml in web appplication is following:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
    </layout>
</appender>
<root>
    <level value="INFO" />
    <appender-ref ref="stdout"/>
</root>
</log4j:configuration>

I've put log4j-1.2.16.jar to /WEB-INF/lib directory of my application. Besides that I've put another copy of log4j-1.2.16.jar to tomcat's /lib directory, according to tutorial presented on official tomcat site.

2
Could you please explain this. - Bennet
I've put some more details. I hope that this is enough. - Marcin Tomiak
Did you keep the xml file in classpath. - Bennet

2 Answers

0
votes

Use the follow option of org.apache.log4j.ConsoleAppender. This options determines if the appender honors reassignments of System.out or System.err made after configuration. By default, false.

<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <param name="Follow" value="true" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" 
               value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
    </layout>
</appender>
0
votes

Thanks to Paul's answer I've discovered the real reason why logger didn't log after reloading context. The problem was that on application initialization I was invoking the following code:

System.setErr( new PrintStream( new LoggingOutputStream( Logger.getLogger("outLog"), Level.ERROR ), true));
System.setOut( new PrintStream( new LoggingOutputStream( Logger.getLogger("outLog"), Level.INFO ), true));

This code is supposed to write all output written to console (like System.out.println) to xml log on production server, but during devlopment I'm using console so it has to be turned off.

When I added <param name="Follow" value="true" /> to my configuration, there was no output at all in any case, so thanks to this option I managed to discover the real problem.