Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.logging.Logger prints the Message twice in console

Tags:

java

logging

I am running a jar file in a console and I put logger so I can trace the if there are any errors. However, the message info prints twice in the console. Can I prevent this?

My code:

public static void main(String[] args) {

     Logger log = Logger.getLogger("Logger");
     log.setLevel(Level.ALL);

     ConsoleHandler handler = new ConsoleHandler();
     handler.setFormatter(new SimpleFormatter());
     handler.setLevel(Level.ALL);
     log.addHandler(handler);

     log.log(Level.INFO, "Reading Configuration File");

 }

Console:

Jul 22, 2015 9:30:33 PM com.ouc.mv90.conversion.CSVtoMV90Converter main
INFO: Reading Configuration File
Jul 22, 2015 9:30:33 PM com.ouc.mv90.conversion.CSVtoMV90Converter main
INFO: Reading Configuration File
like image 544
Ti J Avatar asked Jul 23 '15 01:07

Ti J


4 Answers

Get ready for a facepalm. This question is basically a duplicate of this SO question, but I am giving an answer anyway.

What is happening is that your Logger class already has a default handler which prints to the System.out console. I expect that just the following code will generate output in your console:

Logger log = Logger.getLogger("Logger");
log.setLevel(Level.ALL);
log.log(Level.INFO, "Reading Configuration File");

But you have gone above and beyond this by adding a second handler which also is directed towards the console. Remove this second handler and that should take care of duplicate messages.

like image 150
Tim Biegeleisen Avatar answered Nov 16 '22 04:11

Tim Biegeleisen


I have run into this problem and in my case the solution was:

Logger logger = Logger.getLogger("Logger");
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.ALL);
logger.addHandler(consoleHandler);
logger.setLevel(Level.ALL);
logger.setUseParentHandlers(false);

Notice the last line that calls to setUseParentHandlers. Without this line I get duplicate logs. I have checked at run time that in my case the number of handlers returned by logger.getHandlers() is just 1 after adding my console handler.

If I try

Logger logger = Logger.getLogger("Logger");
logger.setLevel(Level.ALL);
logger.log(Level.INFO, "Reading Configuration File");
logger.log(Level.FINE, "another message");

in this case I don't get duplicate logs but I don't get anything finer than INFO level either.

like image 41
Fede Garcia Avatar answered Nov 16 '22 02:11

Fede Garcia


Just remove all handlers before add it to your logger, like :

for (Handler handler : logger.getHandlers()) {  logger.removeHandler(handler);}
like image 40
ignacio lucatero Avatar answered Nov 16 '22 04:11

ignacio lucatero


public static void main(String[] args) {

     Logger log = Logger.getLogger("Logger");
     log.setLevel(Level.ALL);
     log.log(Level.INFO, "Reading Configuration File");

 }

The above code itself is sufficient to print your log once with it's default log handler. When you add handlers, it will only print in the console using it's default handler, the logging is also directed to your all added handlers to print. Since you are adding another console handler to your logger, veerything will get printed (logged on the console) twice.

like image 32
sakthisundar Avatar answered Nov 16 '22 04:11

sakthisundar