Dimitri PISSARENKO wrote:

>Hello!
>
>Is it possible to write an appender which prints the log messages in a
>textarea of a window?
>
>If yes, are there any examples for that?
>
>Thanks
>
>Dimitri PIssarenko
>
>--
>To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
>For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>
>
>  
>
I wrote one of those a little while ago:

// basic imports
import java.util.*;
import java.io.*;
import java.text.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
 
// log4j-specific imports
import org.apache.log4j.*;
import org.apache.log4j.spi.LoggingEvent;
 
public class Log4JGUIConsoleSessionLog extends AppenderSkeleton {
 
        protected static SimpleAttributeSet simpleAttributeSet = new 
SimpleAttributeSet();
 
        protected int width;
        protected int height;
        protected int tlcornerXcoord;
        protected int tlcornerYcoord;
        protected String resetScrollPoint = "false";
        protected Document document;
        protected Log4JConsoleSessionLogFrame logFrame = null;
 
        public Log4JGUIConsoleSessionLog() {
                super();
        }
 
        public void close() {
        }
 
        public boolean requiresLayout() {
                return true;
        }
 
        public Layout getLayout() {
                return layout;
        }
        public void setLayout(Layout layout) {
                this.layout=layout;
        }
 
        public void setName(String name) {
                this.name = name;
        }
 
        // Java bean-style get/set methods for parameters
        public int getWidth() {
                return width;
        }
        public void setWidth(int width) {
                this.width = width;
        }
        public int getHeight() {
                return height;
        }
        public void setHeight(int height) {
                this.height = height;
        }
        public int getTlcornerXcoord() {
                return tlcornerXcoord;
        }
        public void setTlcornerXcoord(int tlcornerXcoord) {
                this.tlcornerXcoord = tlcornerXcoord;
        }
        public int getTlcornerYcoord() {
                return tlcornerYcoord;
        }
        public void setTlcornerYcoord(int tlcornerYcoord) {
                this.tlcornerYcoord = tlcornerYcoord;
        }
        public String getResetScrollPoint() {
                return resetScrollPoint;
        }
        public void setResetScrollPoint(String resetScrollPoint) {
                this.resetScrollPoint = resetScrollPoint;
        }
 
    /**
     * Append the event's message to the Document
     */
        protected void append(LoggingEvent event) {
 
                if (logFrame == null) {
                        logFrame = new 
Log4JConsoleSessionLogFrame(width, height);
                        document = logFrame.getDocument();
                        // Center the frame and show it
                        logFrame.validate();
                        logFrame.pack();
                        Dimension screenSize = 
Toolkit.getDefaultToolkit().getScreenSize();
                        logFrame.setLocation(tlcornerXcoord,tlcornerYcoord);
                        logFrame.setVisible(true);
                }
                try {
                        String text = getLayout().format(event);
                        document.insertString(document.getLength(), 
text, simpleAttributeSet);
                        if ("true".equals(resetScrollPoint)) {
                                
logFrame.getJTextArea().setCaretPosition(document.getLength());
                        }
                }
                catch (BadLocationException ble) {
                        ble.printStackTrace();
                }
    }


        class Log4JConsoleSessionLogFrame extends JFrame {
 
                protected Document document = null;
                protected JTextArea textArea = new JTextArea();
 
                public Log4JConsoleSessionLogFrame(int width, int height) {
                        super("Log4JConsoleSessionLog");
                        setTitle("Log4JConsoleSessionLog");
 
                        setBackground(Color.white);
                        getContentPane().setLayout(new BorderLayout());
 
                        JPanel topPanel = new JPanel();
                        topPanel.setLayout(new BorderLayout());
                        getContentPane().add(topPanel, BorderLayout.CENTER);
 
                        textArea.setEditable(false);
                        document = textArea.getDocument();
                        JScrollPane scrollPane = new JScrollPane(textArea);
                        
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                        //scrollPane.setPreferredSize(new Dimension(800, 
500));
                        scrollPane.setPreferredSize(new Dimension(width, 
height));
                        topPanel.add(scrollPane, BorderLayout.CENTER );
                }
 
                public Document getDocument() {
                        return document;
                }
 
                public JTextArea getJTextArea() {
                        return textArea;
                }
        }
}


-- 
Mike Norman - [EMAIL PROTECTED]
"'We have two ears and one mouth, so we may listen twice as much as we speak' -
Epictetus.  Aha! This obviously explains many people's attitude to Usenet: "We
have ten fingers and two eyes, so we may type five times as much drivel as we actually 
bother to read." - alt.humor.best-of-usenet



--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to