Change color and format of java.util.logging.Logger output in Eclipse(在 Eclipse 中更改 java.util.logging.Logger 输出的颜色和格式)
问题描述
我正在寻找一种方法来更改 Eclipse 中 java.util.logging.Logger 的日志输出颜色.由于我还没有真正找到解决改变颜色并将其与 Logger 类相结合的解决方案,因此我想在此处记录我的解决方案.
I was looking for a way to change the color of a log output from java.util.logging.Logger in Eclipse. Since I haven't really found a solution that addresses changing color and combining that with the Logger class I would like to document my solution here.
推荐答案
新建一个继承自 Formatter 的类
Create a new class which inherits from Formatter
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
public class LogFormatter extends Formatter
{
// ANSI escape code
public static final String ANSI_RESET = "u001B[0m";
public static final String ANSI_BLACK = "u001B[30m";
public static final String ANSI_RED = "u001B[31m";
public static final String ANSI_GREEN = "u001B[32m";
public static final String ANSI_YELLOW = "u001B[33m";
public static final String ANSI_BLUE = "u001B[34m";
public static final String ANSI_PURPLE = "u001B[35m";
public static final String ANSI_CYAN = "u001B[36m";
public static final String ANSI_WHITE = "u001B[37m";
// Here you can configure the format of the output and
// its color by using the ANSI escape codes defined above.
// format is called for every console log message
@Override
public String format(LogRecord record)
{
// This example will print date/time, class, and log level in yellow,
// followed by the log message and it's parameters in white .
StringBuilder builder = new StringBuilder();
builder.append(ANSI_YELLOW);
builder.append("[");
builder.append(calcDate(record.getMillis()));
builder.append("]");
builder.append(" [");
builder.append(record.getSourceClassName());
builder.append("]");
builder.append(" [");
builder.append(record.getLevel().getName());
builder.append("]");
builder.append(ANSI_WHITE);
builder.append(" - ");
builder.append(record.getMessage());
Object[] params = record.getParameters();
if (params != null)
{
builder.append(" ");
for (int i = 0; i < params.length; i++)
{
builder.append(params[i]);
if (i < params.length - 1)
builder.append(", ");
}
}
builder.append(ANSI_RESET);
builder.append("
");
return builder.toString();
}
private String calcDate(long millisecs) {
SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date resultdate = new Date(millisecs);
return date_format.format(resultdate);
}
}
您可以像这样将自定义格式化程序绑定到您的记录器:
You can bind the custom formatter to your logger like this:
Logger logger = Logger.getLogger("logfile.txt");
logger.setUseParentHandlers(false);
ConsoleHandler handler = new ConsoleHandler();
Formatter formatter = new LogFormatter();
handler.setFormatter(formatter);
logger.addHandler(handler);
可以使用 这个插件
来源:
http://www.vogella.com/tutorials/Logging/article.html
如何在控制台打印颜色使用 System.out.println?
https://mihai-nita.net/2013/06/03/eclipse-plugin-ansi-in-console/
https://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html#setLevel-java.util.logging.Level-
https://docs.oracle.com/javase/8/docs/api/java/util/logging/Level.html
一个尊重 ANSI 颜色的 Eclipse 控制台视图代码?
这篇关于在 Eclipse 中更改 java.util.logging.Logger 输出的颜色和格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Eclipse 中更改 java.util.logging.Logger 输出的颜色和格式
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01