一、前言
一些研发软件会把日志打到界面上,方便工程师调试。
java里我们经常会用log4j来打日志,打到文件里,控制台都是常见操作,那么怎么把它重定向到界面里呢。
二、准备
1.win10
2.eclipse
3.jdk 1.8.0_221
4.jar
log4j:log4j-1.2.17.jar下载链接
实测可用,就是加载网页速度慢了点,为什么慢大家应该也能理解。
commons-logging:commons-logging-1.2.jar下载链接
三、结构
四、代码
1.main启动
package lyrics.main;
import java.awt.EventQueue;
import lyrics.ui.LogRedirectUI;
/**
*
* @author lyrics
* @since 2020/07/01
*/
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new LogRedirectUI();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
2.界面LogRedirectUI
package lyrics.ui;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import org.apache.log4j.Logger;
import lyrics.logger.TextAreaLogAppender;
import javax.swing.JScrollPane;
import javax.swing.JButton;
/**
*
* @author lyrics
* @since 2020/07/01
*/
public class LogRedirectUI {
private final static Logger log = Logger.getLogger(LogRedirectUI.class);
private JFrame frame;
private JScrollPane scrollPane = new JScrollPane();
private JTextArea textArea = new JTextArea();
/**
* Create the application.
*/
public LogRedirectUI() {
initialize();
initLog();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setVisible(true);
frame.setTitle("Log redirect");
frame.setBounds(100, 100, 470, 514);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
scrollPane.setBounds(0, 114, 452, 353);
frame.getContentPane().add(scrollPane);
scrollPane.setViewportView(textArea);
JButton btnButton1 = new JButton("button1");
btnButton1.setBounds(28, 53, 113, 27);
frame.getContentPane().add(btnButton1);
JButton btnButton2 = new JButton("button2");
btnButton2.setBounds(169, 53, 113, 27);
frame.getContentPane().add(btnButton2);
JButton btnButton3 = new JButton("button3");
btnButton3.setBounds(310, 53, 113, 27);
frame.getContentPane().add(btnButton3);
// 添加监听
btnButton1.addActionListener(event -> {
log.info("button 1 pressed!");
});
btnButton2.addActionListener(event -> {
log.info("button 2 pressed!");
});
btnButton3.addActionListener(event -> {
log.info("button 3 pressed!");
});
}
private void initLog() {
try {
Thread thread;
thread = new TextAreaLogAppender(textArea, scrollPane);
thread.start();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "重定向错误");
}
}
}
3.日志重定向
TextAreaLogAppender
package lyrics.logger;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* 重定向
*
*
* @author lyrics
* @since 2020/07/01
*/
public class TextAreaLogAppender extends LogAppender {
private JTextArea textArea;
private JScrollPane scroll;
public TextAreaLogAppender(JTextArea textArea, JScrollPane scroll) throws IOException {
super("textArea");
this.textArea = textArea;
this.scroll = scroll;
}
@SuppressWarnings("resource")
@Override
public void run() {
Scanner scanner = new Scanner(reader);
while (scanner.hasNextLine()) {
try {
Thread.sleep(100);
String line = scanner.nextLine();
textArea.append(line);
textArea.append("\n");
line = null;
scroll.getVerticalScrollBar().setValue(scroll.getVerticalScrollBar().getMaximum());
} catch (Exception ex) {
}
}
}
}
LogAppender
package lyrics.logger;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.Writer;
import org.apache.log4j.Appender;
import org.apache.log4j.Logger;
import org.apache.log4j.WriterAppender;
/**
*
* @author lyrics
* @since 2020/07/01
*/
public abstract class LogAppender extends Thread {
protected PipedReader reader;
public LogAppender(String appenderName) throws IOException {
Logger root = Logger.getRootLogger();
// 获取子记录器的输出源
Appender appender = root.getAppender(appenderName);
// 定义一个未连接的输入流管道
reader = new PipedReader();
// 定义一个已连接的输出流管理,并连接到reader
Writer writer = new PipedWriter(reader);
// 设置 appender 输出流
((WriterAppender) appender).setWriter(writer);
}
}
4.log4j.properties文件
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%%-d{yyyy-MM-dd HH:mm:ss} [ %p ] %m%n
log4j.appender.textArea=org.apache.log4j.ConsoleAppender
log4j.appender.textArea.layout=org.apache.log4j.PatternLayout
log4j.appender.textArea.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %p ] %m%n
log4j.rootLogger=INFO,console,textArea
五、效果
效果图一:
效果图二:
效果图三:
六、最后
本文有些地方参考了网上的资料,有些地方可能会有不足,望诸位海涵!
晚安!!!
工程下载链接:https://download.csdn.net/download/LyRics1996/12568246