On 28/02/11 16:26, Christian Grobmeier wrote:

activateOptions calls WriterAppender.setWriter(), which first calls reset(),
which in turn calls closeWriter().
see
https://svn.apache.org/repos/asf/logging/log4j/trunk/src/main/java/org/apache/log4j/WriterAppender.java
(the base class of ConsoleAppender containing all mentioned methods)

yes, but its creating a new quiet writer afterwards. so you should be
able to write to another stream, right?

No, in my case neither I nor log4j are able to write to System.err, have a look 
at the attached code.

The expected output is (# denote writes to System.err, otherwise to System.out):

#Writing to System.err #1
configuring log4j
Test
#Writing to System.err #2
#ERROR [main]: Logging some message
Finished


But the output actually looks like:

#Writing to System.err #1
configuring log4j
Test
Finished


If I debug this application and configure Eclipse to break on any caught 
IOException, the thread pauses with this StackTrace:

Thread [main] (Suspended (exception IOException))       
        PrintStream.ensureOpen() line: 275      
        PrintStream.write(String) line: 474     
        PrintStream.print(String) line: 619     
        PrintStream.println(String) line: 756   
        Log4jTest.main(String[]) line: 29       

PrintStream.ensureOpen() line 275 is «throw new IOException("Stream closed");»
Thus no output is made. The same is true for the logging statement, since it 
uses System.err, too.

Martin
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class Log4jTest {

	public static void main(String[] args) {
		System.err.println("Writing to System.err #1");
		Logger root = Logger.getRootLogger();

		boolean inited = root.getAllAppenders().hasMoreElements();

		if (!inited) {
			System.out.println("configuring log4j");
			Layout layout = new PatternLayout("%-5p [%t]: %m%n");
			ConsoleAppender app = new ConsoleAppender(layout,
					ConsoleAppender.SYSTEM_ERR);

			app.setFollow(true);
			app.activateOptions(); // comment this out to see how it should work

			root.addAppender(app);
			root.setLevel(Level.ERROR);
		}

		System.out.println("Test");
		System.err.println("Writing to System.err #2");
		root.error("Logging some message");
		System.out.println("Finished");
	}

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to