not the most appropriate tool after all?
I have no idea about where those classes come from. But I do
not think they are needed.
See simple example below, which works for me.
I use log4j.properties instead of log4j.xml, but that should
not matter.
Arne
========================================================
Jar content
-----------
0 Sun Jun 07 15:48:38 EDT 2009 META-INF/
97 Sun Jun 07 15:48:38 EDT 2009 META-INF/MANIFEST.MF
799 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$1.class
798 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$2.class
798 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$3.class
799 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$4.class
1302 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$5.class
1291 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet$6.class
2216 Sun Jun 07 15:33:08 EDT 2009 test/LogApplet.class
2947 Sun Jun 07 15:48:34 EDT 2009 test/GuiAppender.class
487 Sun Jun 07 15:31:40 EDT 2009 log4j.properties
Manifest
--------
Class-Path: log4j-1.2.9.jar
LogApplet.java
--------------
package test;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class LogApplet extends JApplet {
private Logger log = Logger.getLogger(LogApplet.class);
private JButton debugbtn = new JButton("Log debug");
private JButton infobtn = new JButton("Log info");
private JButton warnbtn = new JButton("Log warning");
private JButton errorbtn = new JButton("Log error");
private JButton consolebtn = new JButton("Log to console");
private JButton guibtn = new JButton("Log to GUI");
private boolean console = false;
private boolean gui = false;
public void init() {
debugbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
log.debug("This is a test");
}
});
infobtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
log.info("This is a test");
}
});
warnbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
log.warn("This is a test");
}
});
errorbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
log.error("This is a test");
}
});
consolebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
console = !console;
ConsoleAppender app =
(ConsoleAppender)log.getAppender("console");
if(console) {
app.setThreshold(Level.DEBUG);
} else {
app.setThreshold(Level.OFF);
}
}
});
guibtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
gui = !gui;
GuiAppender app = (GuiAppender)log.getAppender("gui");
if(gui) {
app.setThreshold(Level.DEBUG);
app.open();
} else {
app.setThreshold(Level.OFF);
}
}
});
getContentPane().setLayout(new GridLayout(3,2 ));
getContentPane().add(debugbtn);
getContentPane().add(infobtn);
getContentPane().add(warnbtn);
getContentPane().add(errorbtn);
getContentPane().add(consolebtn);
getContentPane().add(guibtn);
}
}
GuiAppender.java
----------------
package test;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
public class GuiAppender extends AppenderSkeleton {
private JFrame f;
private JTextPane tp;
public GuiAppender() {
f = new JFrame();
f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
//f.setAlwaysOnTop(true);
tp = new JTextPane();
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(tp, BorderLayout.CENTER);
f.setSize(600, 400);
}
private Color LevelToColor(Level lvl) {
if(lvl.equals(Level.DEBUG)) {
return Color.GRAY;
} else if(lvl.equals(Level.INFO)) {
return Color.GREEN;
} else if(lvl.equals(Level.WARN)) {
return Color.YELLOW;
} else if(lvl.equals(Level.ERROR)) {
return Color.RED;
} else if(lvl.equals(Level.FATAL)) {
return Color.RED;
} else {
return Color.BLACK;
}
}
protected void append(LoggingEvent ev) {
try {
StyledDocument doc = tp.getStyledDocument();
MutableAttributeSet attrs = tp.getInputAttributes();
StyleConstants.setForeground(attrs,
LevelToColor(ev.getLevel()));
StyleConstants.setBackground(attrs, Color.LIGHT_GRAY);
doc.insertString(doc.getLength(), getLayout().format(ev),
attrs);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
public void open() {
f.setVisible(true);
}
public void close() {
f.dispose();
}
public boolean requiresLayout() {
return true;
}
}
log4j.properties
----------------
log4j.category.test.LogApplet = debug, console, gui
log4j.appender.console.threshold = off
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %-30c %d %-5p %m%n
log4j.appender.gui.threshold = off
log4j.appender.gui = test.GuiAppender
log4j.appender.gui.layout = org.apache.log4j.PatternLayout
log4j.appender.gui.layout.ConversionPattern = %-30c %d %-5p %m%n
Thanks for the reply.
just stuck it in the codebase.
I'll give it a go now and if I don't report back then all's ok, thanks.
PS. If anyone can tell me, given that I have a package "tier3Client" that