java
package util;
import org.apache.log4j.*;
import org.apache.log4j.spi.LoggingEvent;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.io.*;
public class Log4j {
public static final Logger LOGGER = Logger.getLogger(Log4j.class);
//配置为 界面输出 和 文件输出
public static void configureLogger(JTextPane textPane) {
// 创建UI界面输出的Appender
JTextPaneAppender uiAppender = new JTextPaneAppender(textPane);
//uiAppender.setLayout(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %c [%p] - %m%n"));
uiAppender.setLayout(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} [%p] - %m%n"));
uiAppender.activateOptions();
Logger.getRootLogger().addAppender(uiAppender);
// 创建文件输出的Appender
try {
FileAppender fileAppender = new FileAppender(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} [%p] - %m%n"), "../logfile.log");
fileAppender.setAppend(true); // 如果true,则追加到现有的日志文件末尾;如果false,则覆盖文件中的现有内容
fileAppender.activateOptions();
// 将Appender添加到日志记录器
Logger.getRootLogger().addAppender(fileAppender);
} catch (IOException e) {
e.printStackTrace();
return;
}
LOGGER.setLevel(Level.DEBUG);
}
//配置为 控制台输出 和 文件输出
public static void configureLogger() {
// 创建控制台输出的Appender
ConsoleAppender consoleAppender = new ConsoleAppender();
consoleAppender.setLayout(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %c [%p] - %m%n"));
consoleAppender.setTarget(ConsoleAppender.SYSTEM_OUT);
consoleAppender.activateOptions();
// 创建文件输出的Appender
try {
FileAppender fileAppender = new FileAppender(new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %c [%p] - %m%n"), "logs/logfile.log");
fileAppender.setAppend(true); // 如果true,则追加到现有的日志文件末尾;如果false,则覆盖文件中的现有内容
fileAppender.activateOptions();
// 将Appender添加到日志记录器
Logger.getRootLogger().addAppender(fileAppender);
} catch (IOException e) {
e.printStackTrace();
return;
}
LOGGER.setLevel(Level.DEBUG);
}
public static void AppendText(JTextPane textPane,String text/*文本内容*/,Color textColor/*文本颜色*/) throws BadLocationException {
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setForeground(set, textColor);//设置文本颜色
StyledDocument doc = textPane.getStyledDocument();
doc.setParagraphAttributes(textPane.getText().length(), doc.getLength() - textPane.getText().length(), set, false);
doc.insertString(doc.getLength(),text,set);
textPane.setCaretPosition(doc.getLength());
}
private static class JTextPaneAppender extends AppenderSkeleton {
private JTextPane textPane;
public JTextPaneAppender(JTextPane textPane) {
this.textPane = textPane;
}
@Override
protected void append(LoggingEvent event) {
Color textColor ;
if (event.getLevel().equals(Level.DEBUG)) {
textColor = Color.GRAY;
} else if (event.getLevel().equals(Level.INFO)) {
textColor = Color.BLACK;
} else if (event.getLevel().equals(Level.WARN)) {
textColor = Color.ORANGE;
} else if (event.getLevel().equals(Level.ERROR)) {
textColor = Color.RED;
} else if (event.getLevel().equals(Level.FATAL)) {
textColor = Color.MAGENTA;
} else {
textColor = Color.BLACK;
}
String logMessage = layout.format(event);
SwingUtilities.invokeLater(() -> {
try {
AppendText(textPane,logMessage, textColor);
} catch (BadLocationException e) {
e.printStackTrace();
}
});
}
@Override
public void close() {
// Nothing to do here
}
@Override
public boolean requiresLayout() {
return true;
}
}
private static JTextPane textPane;
public static void main(String[] args) {
// 创建并显示UI界面
createUI();
// 配置log4j日志记录器
configureLogger(textPane);
// 示例日志输出
LOGGER.debug("Debug log message");
LOGGER.info("Info log message");
LOGGER.warn("Warn log message");
LOGGER.error("Error log message chenhao");
LOGGER.fatal("Fatal log message chenhao");
// 关闭log4j日志记录器
LogManager.shutdown();
}
private static void createUI() {
JFrame frame = new JFrame("Log Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
textPane = new JTextPane();
textPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textPane);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
}