I have console output of different color in Eclipse: red and black.
What does the color mean in this context?
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
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);
}
}
}
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:
CustomFormatter.java
javac CustomFormatter.java
jar cfv CustomFormatter.jar CustomFormatter.class
and save it in whatever folder you want (for example, in C:\java\CustomFormatter
)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:
logging.properties
with a text editor. In my case, I'll edit C:\Program Files\Java\jre1.8.0_231\lib\logging.properties
. 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.
Window --> Preferences --> Java --> Installed JREs
Edit
and Add External JARs...
CustomFormatter.jar
and select it.Finish
and Apply 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:
NOTE: If normal stdout text appears blue, red or yellow try to disable Limit console output
in Window --> Preferences --> Run/Debug --> Console
The difference is System.out v. System.err