首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Util.Logging.Logger中改变logging.properties打印颜色

如何在Util.Logging.Logger中改变logging.properties打印颜色
EN

Stack Overflow用户
提问于 2019-02-27 16:09:00
回答 3查看 4.5K关注 0票数 0

目前,我所有的信息,严重和良好的原木打印红色。我想改变信息和细日志为白色。

我发现许多东西通过制造一个新的Fommater来改变颜色。我想知道如何通过修改logging.properties来改变印刷颜色?

我使用的是Java默认日志库Util.Logging.Logger

环境:

  • 2018-12年(4.10.0)
  • Windows 10
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-02-28 14:35:55

目前,我所有的信息,严重和良好的原木打印红色。

这取决于使用System.err的进程是什么。由于输出是红色的,所以我假设您使用的是IDE。你用的是哪种IDE?也许它有一些设置来修改控制台输出的呈现方式。对于eclipse,您可以:

  1. 更改标准输出/标准错误文本颜色,使标准输出为白色,标准错误为红色。
  2. 创建一个设置为级别警告的常规ConsoleHandler。这将指向所有警告和上面的错误流,这将是红色的。
  3. 创建一个特殊的要打印到标准输出的处理程序并将级别设置为ALL。
  4. 创建一个java.util.logging.Filter来过滤出比标准输出处理程序的信息更大的消息,并将其安装在该处理程序上。
  5. 将控制台处理程序(ERR)和OUT处理程序附加到根记录器。

我想知道如何通过修改logging.properties来改变印刷颜色?

您只能从logging.properties中安装一种新类型的格式化程序或第三方处理程序,这些处理程序可以更改输出颜色,但这取决于如何呈现消耗的控制台输出流(E.HTMLvs BASH终端)。

票数 1
EN

Stack Overflow用户

发布于 2020-02-27 13:36:57

问题是Eclipse总是以相同的颜色(在本例中为红色)显示stderr输出。它在设置中是可配置的,但对于所有stderr流来说都是一样的。

我的解决方案是为控制台处理程序使用自定义格式化程序,并根据其级别在每条消息之前注入ANSI颜色代码。因此,我根据JDK8中的原始JDK8做出了这个结论。下面是代码:

代码语言:javascript
复制
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public class CustomFormatter extends Formatter {

    public static final String ANSI_RESET = "\u001B[0m";
    public static final String ANSI_RED = "\u001B[31m";
    public static final String ANSI_YELLOW = "\u001B[33m";
    public static final String ANSI_CYAN = "\u001B[36m";

    private final Date dat = new Date();
    private static final String format = "%1$s %2$tb %2$td, %2$tY %2$tl:%2$tM:%2$tS %2$Tp %3$s%n%5$s: %6$s%7$s%n";

    @Override
    public String format(LogRecord record) {
        dat.setTime(record.getMillis());
        String source;
        if (record.getSourceClassName() != null) {
            source = record.getSourceClassName();
            if (record.getSourceMethodName() != null) {
                source += " " + record.getSourceMethodName();
            }
        } else {
            source = record.getLoggerName();
        }
        String message = formatMessage(record);
        String throwable = "";
        if (record.getThrown() != null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            pw.println();
            record.getThrown().printStackTrace(pw);
            pw.close();
            throwable = sw.toString();
        }

        switch (record.getLevel().toString()) {
            case "INFO":
                return String.format(format, ANSI_CYAN, dat, source, record.getLoggerName(),
                        record.getLevel().getLocalizedName(), message + ANSI_RESET, throwable);
            case "WARNING":
                return String.format(format, ANSI_YELLOW, dat, source, record.getLoggerName(),
                        record.getLevel().getLocalizedName(), message + ANSI_RESET, throwable);
            case "SEVERE":
                return String.format(format, ANSI_RED, dat, source, record.getLoggerName(),
                        record.getLevel().getLocalizedName(), message + ANSI_RESET, throwable);
            default:
                return String.format(format, dat, source, record.getLoggerName(),
                        record.getLevel().getLocalizedName(), message, throwable);
        }
    }
}

先决条件

  • 用于Eclipse的控制台插件中的ANSI逃逸。您需要这样做,这样Eclipse控制台就可以解释ANSI代码。安装它并重新启动Eclipse
  • 上面的代码编译并压缩到一个.jar文件中。
代码语言:javascript
复制
1. Save the code above as `CustomFormatter.java`
2. Compile it with `javac CustomFormatter.java`
3. Create a JAR File Containing the Class File with `jar cfv CustomFormatter.jar CustomFormatter.class` and save it in whatever folder you want (for example, in `C:\java\CustomFormatter`)

使用说明

  • Window --> Preferences --> Ansi Console,检查Try using the standard error color setting for stderr output,然后是Apply and Close
  • logging.properties文件编辑,它位于JRE的lib文件夹中。您的计算机中可能有多个,您应该编辑与使用的JRE版本相对应的一个。通过阅读Eclipse中的"Console“选项卡,您可以知道使用的是哪一种:
代码语言:javascript
复制
1. Open `logging.properties` with a text editor. In my case, I'll edit `C:\Program Files\Java\jre1.8.0_231\lib\logging.properties`. 
2. You should change `java.util.logging.ConsoleHandler.formatter` value (in my case it's the 44th line) so it's equal to `CustomFormatter`, **exactly like this:** **`java.util.logging.ConsoleHandler.formatter = CustomFormatter`**. Make sure you save the changes.

  • 将添加为Eclipse系统库。
代码语言:javascript
复制
1. Go to `Window --> Preferences --> Java --> Installed JREs`
2. Select the JRE that you're using, click `Edit` and `Add External JARs...`
3. Go to the folder in which you saved `CustomFormatter.jar` and select it.
4. Make sure it's on the list of system libraries and click `Finish` and `Apply and Close`
5. That's it. You should have different colors for each Logger level now. It's cyan for INFO, yellow for WARNING and red for SEVERE. You can change it to whatever color you want by modifying the code above with the [corresponding ANSI Color code](https://stackoverflow.com/a/5762502/7915606). It's working for me with java 8 and Eclipse 2019-12:

注意:如果普通的标准文本显示为蓝色、红色或黄色,请尝试在Window --> Preferences --> Run/Debug --> Console中禁用Limit console output

票数 3
EN

Stack Overflow用户

发布于 2021-01-05 07:29:05

贾朱贝的回答很棒。我使用netbeans 8.2。它的控制台有默认的红色文本,但处理ANSI代码。我把他的答案修改为下面的格式化程序

代码语言:javascript
复制
package net.sf.jaer.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

/**
 * Based on
 * https://stackoverflow.com/questions/54909752/how-to-change-the-util-logging-logger-printing-colour-in-logging-properties
 * with ANSI codes from
 * https://stackoverflow.com/questions/5762491/how-to-print-color-in-console-using-system-out-println
 *
 * @author tobid Jan 2021
 */
public class LoggingAnsiColorConsoleFormatter extends Formatter {

    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";
    public static final String ANSI_BLACK_BACKGROUND = "\u001B[40m";
    public static final String ANSI_RED_BACKGROUND = "\u001B[41m";
    public static final String ANSI_GREEN_BACKGROUND = "\u001B[42m";
    public static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m";
    public static final String ANSI_BLUE_BACKGROUND = "\u001B[44m";
    public static final String ANSI_PURPLE_BACKGROUND = "\u001B[45m";
    public static final String ANSI_CYAN_BACKGROUND = "\u001B[46m";
    public static final String ANSI_WHITE_BACKGROUND = "\u001B[47m";

    private final Date date = new Date();
    // FORMAT uses e.g. 2$ which refers to 2nd argument of String.format
    // It has two lines: Line 1 is the date and class/method. Line 2 is the LEVEL and message
    // Lines are separated by the format spec %n which makes newline
    // This format puts date and class/method in CYAN, followed by newline with level colored, followed by default message color
    private static final String FORMAT = ANSI_CYAN+"%2$tb %2$td, %2$tY %2$tl:%2$tM:%2$tS %2$Tp %3$s%n%1$s%5$s:" + ANSI_RESET + " %6$s%7$s%n";
    // args to String.format
    // 1 ansi code
    // 2 date 
    // 3 source (class/method) 
    // 4 logger name 
    // 5 level 
    // 6 message, 
    // 7 throwable
    // output example
    // Jan 05, 2021 7:09:55 AM net.sf.jaer.eventprocessing.filter.BackgroundActivityFilter resetFilter
    //INFO: resetting BackgroundActivityFilter

    @Override
    public String format(LogRecord record) {
        date.setTime(record.getMillis());
        String source;
        if (record.getSourceClassName() != null) {
            source = record.getSourceClassName();
            if (record.getSourceMethodName() != null) {
                source += " " + record.getSourceMethodName();
            }
        } else {
            source = record.getLoggerName();
        }
        String message = formatMessage(record);
        String throwable = "";
        if (record.getThrown() != null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            pw.println();
            record.getThrown().printStackTrace(pw);
            pw.close();
            throwable = sw.toString();
        }

        switch (record.getLevel().toString()) {
            case "INFO":
                return String.format(FORMAT, ANSI_GREEN_BACKGROUND+ANSI_BLACK, date, source, record.getLoggerName(),
                        record.getLevel().getLocalizedName(), message + ANSI_RESET, throwable);
            case "WARNING":
                return String.format(FORMAT, ANSI_YELLOW_BACKGROUND+ANSI_BLACK, date, source, record.getLoggerName(),
                        record.getLevel().getLocalizedName(), message + ANSI_RESET, throwable);
            case "SEVERE":
                return String.format(FORMAT, ANSI_RED_BACKGROUND + ANSI_WHITE, date, source, record.getLoggerName(),
                        record.getLevel().getLocalizedName(), message + ANSI_RESET, throwable);
            default:
                return String.format(FORMAT, date, source, record.getLoggerName(),
                        record.getLevel().getLocalizedName(), message, throwable);
        }
    }
}

它在我的Logging.properties中实例化,我在启动时指定了

代码语言:javascript
复制
-Djava.util.logging.config.file=conf/Logging.properties

Logging.properties有这一行

代码语言:javascript
复制
java.util.logging.ConsoleHandler.formatter = net.sf.jaer.util.LoggingAnsiColorConsoleFormatter

输出如下所示

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54909752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档