logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sam Preston <j.sam.pres...@gmail.com>
Subject Problem with WriterAppender and threads
Date Fri, 01 Jul 2005 19:55:04 GMT
Hi all,

I've been trying to set up a WriterAppender which I can read logging
events from in order to post them to a GUI text area.  I'm using a
PipedReader / PipedWriter pair, where I pass the PipedWriter to the
WriterAppender and start a thread to read from the PipedReader and add
the new messages to the text area.  I run into problems, though, as
the 'write end' of the pipe seems to die unexpectedly.  It seems to
have something to do with the main thread ending--below is a quick
test I wrote (just prints to the console, no GUI).  For me, about half
the time it will continue to receive the 'testing...' logging events
after main finishes, and the other half of the time it will throw an
exception when I try to read from the pipe.  I'm using log4j version
1.2.9.  After the code, I've given the output I get when it dies. 
Perhaps I'm going about this all wrong?

// START TEST CODE

import java.io.*;
import javax.swing.*;

import org.apache.log4j.*;

public class WriterAppenderTest {
	
	protected static Logger logger =
Logger.getLogger(WriterAppenderTest.class.getName());
	
	// just a little class that generates one log event per second
	protected static class WriterAppenderTestWriter implements Runnable{
		protected static Logger writerLogger =
Logger.getLogger(WriterAppenderTestWriter.class.getName());

		public WriterAppenderTestWriter(){
			new Thread(this).start();
		}

		public void run(){
			while(true){
				try{
					writerLogger.info(new String("testing..."));
					Thread.sleep(1000);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}
	
	// the class that sets up and reads the events sent by the WriterAppender
	protected static class WriterAppenderTestReader implements Runnable{
		protected static final int BUFF_SIZE = 1024;
		protected char[] buff;
		protected WriterAppender writerAppender;
		protected PipedWriter pipedWriter;
		protected PipedReader pipedReader;
		
		public WriterAppenderTestReader(){
			pipedReader = new PipedReader();
			try{
				pipedWriter = new PipedWriter(pipedReader);
				writerAppender = new WriterAppender(new SimpleLayout(), pipedWriter);
				Logger.getRootLogger().addAppender(writerAppender);
				new Thread(this).start();
			}catch(IOException ioe){
				ioe.printStackTrace();
			}
		}

		public void run(){
			int n;
			buff = new char[BUFF_SIZE];
			// read data sent from WriterAppender
			while(true){
				try{
					n = pipedReader.read(buff, 0, BUFF_SIZE);
					System.out.print("Read " + n + " bytes: ");
					System.out.println(new String(buff, 0, n));
				}catch(IOException ioe){
					System.out.println("Problem reading from PipedReader, must die.\n");
					ioe.printStackTrace();
					break;
				}
			}
		}
	}

	public static void main(String args[]){
		// set up logging
		BasicConfigurator.configure();
		Logger.getRootLogger().setLevel(Level.ALL);
		// initialize, connect, and start reader / writer
		WriterAppenderTestReader watr = new WriterAppenderTestReader();
		// start generating logging events
		WriterAppenderTestWriter watw = new WriterAppenderTestWriter();
		// do a log
		//logger.info("Exiting main");
	}

}

// END TEST CODE

// OUTPUT

0 [Thread-1] INFO
ConnectionManager.test.WriterAppenderTest$WriterAppenderTestWriter  -
testing...
Read 19 bytes: INFO - testing...

0 [main] INFO ConnectionManager.test.WriterAppenderTest  - Exiting main
Read 21 bytes: INFO - Exiting main
java.io.IOException: Write end dead

        at java.io.PipedReader.read(PipedReader.java:242)
Problem reading from PipedReader, must die.

        at ConnectionManager.test.WriterAppenderTest$WriterAppenderTestReader.run(WriterAppenderTest.java:72)
        at java.lang.Thread.run(Thread.java:595)
log4j:ERROR Failed to write [INFO - testing...
].
java.io.IOException: Read end dead
        at java.io.PipedReader.receive(PipedReader.java:118)
1001 [Thread-1] INFO 

ConnectionManager.test.WriterAppenderTest$WriterAppenderTestWriter  - testing...
        at java.io.PipedReader.receive(PipedReader.java:150)
        at java.io.PipedWriter.write(PipedWriter.java:125)
        at java.io.Writer.write(Writer.java:150)
        at java.io.Writer.write(Writer.java:126)
        at org.apache.log4j.helpers.QuietWriter.write(QuietWriter.java:39)
        at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:292)
        at org.apache.log4j.WriterAppender.append(WriterAppender.java:150)
        at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:221)
        at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:57)
        at org.apache.log4j.Category.callAppenders(Category.java:194)
        at org.apache.log4j.Category.forcedLog(Category.java:379)
        at org.apache.log4j.Category.info(Category.java:654)
        at ConnectionManager.test.WriterAppenderTest$WriterAppenderTestWriter.run(WriterAppenderTest.java:37)
        at java.lang.Thread.run(Thread.java:595)
2063 [Thread-1] INFO
ConnectionManager.test.WriterAppenderTest$WriterAppenderTestWriter  -
testing...
3064 [Thread-1] INFO
ConnectionManager.test.WriterAppenderTest$WriterAppenderTestWriter  -
testing...
4066 [Thread-1] INFO
ConnectionManager.test.WriterAppenderTest$WriterAppenderTestWriter  -
testing...

[ and so on--the normal logging continues to receive the 'testing...'
logger events ]

// END OUTPUT

Thanks, and sorry about the excessive length of this message.

-- 
--------------------
J. Samuel Preston
Research Associate
Center for Advanced Engineering Environments
Old Dominion University Research Foundation

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message