Why is some output in Eclipse console - red?
If the console preferences settings are standard (in other words, you haven't made any changes), then red is for error
Black is Standard Out Text Color
This preference controls the color of text written to the standard output stream by an application.
Red is Standard Error Text Color
This preference controls the color of text written to the standard error stream by an application.
Docs
The difference is System.out v. System.err
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:
- Save the code above as
CustomFormatter.java
- Compile it with
javac CustomFormatter.java
- 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, inC:\java\CustomFormatter
)
- Save the code above as
Instructions
- Go to
Window --> Preferences --> Ansi Console
and checkTry using the standard error color setting for stderr output
, thenApply and Close
. Edit
logging.properties
file, which is located in thelib
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:- Open
logging.properties
with a text editor. In my case, I'll editC:\Program Files\Java\jre1.8.0_231\lib\logging.properties
. - You should change
java.util.logging.ConsoleHandler.formatter
value (in my case it's the 44th line) so it's equal toCustomFormatter
, exactly like this:java.util.logging.ConsoleHandler.formatter = CustomFormatter
. Make sure you save the changes.
- Open
Add
CustomFormatter.jar
as a JRE system library in Eclipse.- Go to
Window --> Preferences --> Java --> Installed JREs
- Select the JRE that you're using, click
Edit
andAdd External JARs...
- Go to the folder in which you saved
CustomFormatter.jar
and select it. - Make sure it's on the list of system libraries and click
Finish
andApply and Close
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:
- Go to
NOTE: If normal stdout text appears blue, red or yellow try to disable Limit console output
in Window --> Preferences --> Run/Debug --> Console