1

The following class uses a JInternalFrame holding a Textarea that displays all the redirected println and err statements.

public class ConsoleFrame extends JInternalFrame
{
  JTextArea outArea = new JTextArea(10,100);
  static JInternalFrame cons;
  public ConsoleFrame() 
  {
    outArea.setLineWrap(true);
    JScrollPane pain = new JScrollPane(outArea);
    //pain.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    this.add(pain);
    this.setVisible(true);
    this.setSize(1000,400); 
    this.setTitle("Groovy Console");
    this.closable = false;
    this.maximizable = false;
    this.isSelected = true;
    this.resizable = false;
    BasicInternalFrameUI ui = (BasicInternalFrameUI)this.getUI();
    Component north = ui.getNorthPane();
    MouseMotionListener[] actions =
    (MouseMotionListener[])north.getListeners(MouseMotionListener.class);

    for (int i = 0; i < actions.length; i++)
    north.removeMouseMotionListener( actions[i] );

    this.setFocusable(false);    
 //logger
    System.setErr(new PrintStream(new JTextAreaOutputStream(outArea)));
    System.setOut(new PrintStream(new JTextAreaOutputStream(outArea)));

    setConsole(this);
  }


  static public JInternalFrame getConsole(){
      return cons;
  }
  public void setConsole(JInternalFrame console){
      cons = console;
  }
  public class JTextAreaOutputStream extends OutputStream {
    JTextArea ta;

    public JTextAreaOutputStream(JTextArea t) {
      super();
      ta = t;
    }

    public void write(int i) {
      ta.append(Character.toString((char)i));
    }

    public void write(char[] buf, int off, int len) {
      String s = new String(buf, off, len);
      ta.append(s);
    }

  }

}

This class only redirects the sysout and syserr statements. What modification should I make in the code to redirect the logger statements into the textarea?

4
  • 2
    Which logging framework are you using? The built-in Java one? Log4J? Apr 13, 2011 at 7:32
  • If you are using log4j, look into the console appender.
    – Jataro
    Apr 13, 2011 at 9:27
  • is there a reason you are creating two streams instead of one? Apr 13, 2011 at 17:26
  • Check this blog TextAreaAppender Hope it helps
    – Olantobi
    May 31, 2011 at 12:21

2 Answers 2

5

You should implement a custom Log4J logger. There are very useful base classes to extend from. I would recommend using a org.apache.log4j.WriterAppender.

2

The Log4J logger is probably getting its references to System.out and System.err before you replace them. So you can either implement a custom Appender, or try to beat Log4J to the punch. The latter may be possible if you have control of startup.

2
  • Sinec log4j usually gets configured very early in the application startup, I don't think thats feasible. And it would only work if there is a ConsoleAppender configured in Log4J. But since the asker also wants to redirect his Standard streams, he might as well do this before app startup. So +1
    – Daniel
    Apr 13, 2011 at 11:15
  • @Daniel in general I agree, but in this case, it is a UI application, so I figured @VeeKay had these options. Apr 13, 2011 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.