0

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

2
  • 1
    What is the type of Logger? Commented Feb 11, 2014 at 14:00
  • Logger is a java util class (java.util.logging.Logger). Commented Feb 11, 2014 at 14:15

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually this is what I was looking for, so thanks StephaneM!
0

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.

Comments

0

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.