java 日志颜色,在Eclipse中更改java.util.logging.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.

解决方案

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("\t");

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("\n");

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);

ANSI escape codes can be enabled for Eclipse console with this plugin

Sources:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值