Why is some output in Eclipse console - red?

后端 未结 3 1203
礼貌的吻别
礼貌的吻别 2020-12-20 21:22

I have console output of different color in Eclipse: red and black.

What does the color mean in this context?

3条回答
  •  囚心锁ツ
    2020-12-20 21:48

    This is an old question but I solved it, so here's my answer.

    The problem is that Eclipse always shows stderr output in the same color (in this case red). It's configurable in settings but it will always be the same for all stderr stream.

    My solution is to use a custom formatter for the console handler and inject ANSI color codes before each message depending on its level. So I made that, based on the original SimpleFormatter.java in JDK8. Here's the code:

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

    Prerequisites

    • ANSI Escape in Console plugin for Eclipse. You'll need this so the Eclipse console can interpret ANSI Escape codes. Install it and restart Eclipse.

    • The code above compiled and zipped into a .jar file:

      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)

    Instructions

    • Go to Window --> Preferences --> Ansi Console and check Try using the standard error color setting for stderr output, then Apply and Close.
    • Edit logging.properties file, which is located in the lib folder of your JRE. You may have multiple ones in your computer, you should edit the one corresponding to the JRE version that Eclipse is using. You can know which one is using by reading below the "Console" tab in Eclipse:

      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.
    • Add CustomFormatter.jar as a JRE system library in Eclipse.

      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. It's working for me with java 8 and Eclipse 2019-12:

    NOTE: If normal stdout text appears blue, red or yellow try to disable Limit console output in Window --> Preferences --> Run/Debug --> Console

提交回复
热议问题