1

I'm working with java.util.logging and I was wondering how I can remove the date and class name from the console output. I read some similar posts but none seemed to solve the problem. In order to fix the issue, I tried creating a CustomFormatter and overriding some of the methods, but that didn't work either. Here's what my code looks like:

public class CustomFormatter extends Formatter {
    public CustomFormatter() {
        super();
    }

    @Override
    public String format(LogRecord record){
          return "testing";
    }
    @Override
    public String formatMessage(LogRecord record) {
        return "testing";
    }

}
public void logStuff() {
    logger.setLevel(Level.INFO);
    Handler consoleHandler = new ConsoleHandler();
    consoleHandler.setFormatter(new CustomFormatter());
    logger.addHandler(consoleHandler);
    logger.info("log stuff");
}

I'm getting the following output:

testingAug 13, 2015 9:37:12 PM className     <init>
INFO: log stuff

I want to get:

INFO: log stuff
shaydawg
  • 1,193
  • 1
  • 12
  • 17
  • That is a different question i guess. Here he wants a different output. – prab2112 Aug 14 '15 at 05:29
  • Check this: https://prab2112.wordpress.com/2015/08/14/implement-a-custom-logger-using-java-util-logging/ – prab2112 Aug 14 '15 at 05:41
  • 1
    @nginx No, same question, same answer, he is getting two sets of output, needs to remove the defualt console handler. – user207421 Aug 14 '15 at 05:48
  • 1
    Actually what he wants is while logging to Console "testingAug 13, 2015 9:37:12 PM className " this line shouldn't get printed. – prab2112 Aug 14 '15 at 05:54
  • Don't install addtional ConsoleHandlers and set the [format](http://docs.oracle.com/javase/7/docs/api/java/util/logging/SimpleFormatter.html#format(java.util.logging.LogRecord)) property of the [SimpleFormatter](http://docs.oracle.com/javase/7/docs/api/java/util/logging/SimpleFormatter.html). Remove the quotes from the pattern if you are going to set it up via a properties file. The pattern you are looking for is something like java.util.logging.SimpleFormatter.format="4$s: %5$s%6$s%n" – jmehrens Aug 14 '15 at 13:01
  • @jmehrens I tested this in Eclipse; the problem is that the parent console handler is getting added. Your proposed dupe doesn't even address that problem. – durron597 Aug 14 '15 at 13:27

0 Answers0