Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to remove the information line from java.util.logging.Logger output?

Tags:

java

logging

Using java.util.logging.Logger to output some log to the console just like this:

public static void main(String[] args) {
    Logger logger = Logger.getLogger("test");
    logger.info("Hello Wolrd!");
}

The output is:

FEB 16, 2012 10:17:43 AM com.abc.HelloWorld main
INFO: Hello World.

This seems to be OK, however...

We are using java.util.logging.Logger in all our Ant tasks (an internal standard) and we have a large ant project. The console output of a full cycle can be larger than 300KB, in which our own logger output taks at least 50.

Now I don't want to see the information about time, class name and method of Level.INFO outputs. Also the information line makes it hard to focus on the custom messages.

So is there any way to remove the first line (information of timestamp, class and method) from each output (or just from each Level.INFO output)?

like image 297
Dante WWWW Avatar asked Feb 16 '12 02:02

Dante WWWW


People also ask

What does logger info do in Java?

A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.

Is Java logger thread safe?

All methods on Logger are multi-thread safe.


1 Answers

See How do I get java logging output to appear on a single line?. However, the only difference is removing the first line, instead of putting it on one line.

To only do this for INFO levels, extend the formatter you want to conditionally modify (e.g. SimpleFormatter), and override the format method. You could do something like this:

public String format(LogRecord record){
  if(record.getLevel() == Level.INFO){
    return record.getMessage() + "\r\n";
  }else{
    return super.format(record);
  }
}

If you're doing this only for INFO, you probably don't need the "INFO: " prefix - but you could add it back on if you'd like.

like image 73
ziesemer Avatar answered Sep 19 '22 17:09

ziesemer