Question

Given the following main:

public static void main(String[] args) {
    System.out.println("System.out.println");
    Logger.getGlobal().setLevel(Level.INFO);
    Logger.getGlobal().log(Level.INFO, "Logger.INFO");
}

I get the following output: Logger output

If there a way to get a better output for Logger? I would much prefer a single line output and a coherent color highlighting (SEVERE=red, INFO=green, ... like logcat for Android basically).

Thanks

Was it helpful?

Solution

I use the eclipse plugin grepConsole

With smple regex you can change the (foreground|background)color of the whole line or just use different color for each group of the line.

OTHER TIPS

The java system implements only two output streams, the System.err and the System.out stream. The latter is always black, and the output of the former is always displayed red in most IDE's. So basically, there is no way for you to set the color of this output easily.

In order to make changes to the colors... I guess you'll have to run it either in the terminal/windows console and use the (native) color-codes encoded in your text, or you could create a very new plugin for eclipse (or maybe search for other existing ones?) that renders your output differently. I'm at least pretty sure there is no easy solution for this.

You are not going to get color in a logfile because it is plain text. A log viewer (as mentioned by @Cqnqrd) can add color.

Take a look at slf4j and/or logback. Both appear to be better than your technique.

Example code:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Blam
{
    private static final Logger = LoggerFactory.getLogger(Blam.class);

    public void hooty()
    {
         logger.info("some {}", "example");
         logger.info("logging {} with {} parameters",
             "statements",
             2);
    }
}

output:

some example
logging statements with 2 parameters

The value of the {} is that the work to subsitute the parameter only happens if info (in this example) level logging is enabled.

public class LogHandler extends ConsoleHandler {

    public LogHandler() {
        setOutputStream(System.out);
    }

}

LogHandler handler = new LogHandler();
handler.setFormatter(new LogFormatter());

Logger logger = Logger.getAnonymousLogger();
logger.setUseParentHandlers(false);
logger.addHandler(handler);

LogRecord record = new LogRecord(Level.INFO, "This is just a test");
logger.log(record);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top