我正在尝试使用两个记录器。一个用于记录到log4j文件,另一个用于添加我自己的appender,因此我可以登录到JTextArea。
我有它工作异常我的log4j文件输出消息也被发送到我的jtextarea。
class MyClass{
private static final org.slf4j.Logger log = LoggerFactory.getLogger(MyClass.class);
private static org.apache.log4j.Logger statusLog;
JTextArea textArea = new JTextArea(10,88);
// Initialize the status log
StatusLog.getInstance(textArea);
statusLog.("hello txt area");
log.("hello txt area");
}
public class StatusLog extends AppenderSkeleton {
JTextArea textArea;
private static final org.apache.log4j.Logger statusLog = org.apache.log4j.Logger.getRootLogger();
private StatusLog(JTextArea textArea){
this.textArea = textArea;
statusLog.addAppender(this);
}
private StatusLog(){}
/**
* This custructor should be used to initialize textare.
* After intial call getInstance() should be called.
* @param textArea
* @return
*/
public static StatusLog getInstance(JTextArea textArea){
return new StatusLog( textArea);
}
public static StatusLog getInstance(){
return new StatusLog();
}
@Override
public void close() {
// TODO Auto-generated method stub
}
@Override
public boolean requiresLayout() {
// TODO Auto-generated method stub
return false;
}
@Override
protected void append(LoggingEvent event) {
if(textArea!=null)
{
textArea.append(event.getMessage().toString()+"\n");
textArea.setCaretPosition(textArea.getText().length());
}
}
public static org.apache.log4j.Logger getStatuslog() {
return statusLog;
}
}
道具文件
log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./Log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.fil;e.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n