Java.util.logging.Logger prints message twice to console

I run the jar file in the console and I put a log so I can track if there are any errors. However, the message information is printed twice to 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

      

+3


source to share


4 answers


Prepare for facepalm. This question is basically a duplicate of this SO question , but I give the answer anyway.

What's going on, your class Logger

already has a default handler that prints to the console System.out

. I expect only the following code to generate output in the console:



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

      

But you went above and beyond that by adding a second handler that also points to the console. Remove this second handler and it should watch for duplicate messages.

+8


source


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 is enough to print your log once using the default log handler. When you add handlers, it will only print to the console using the default handler, and the logging is routed to all your print handlers as well. Since you are adding another console handler to the console, veerything will be printed (logged to the console) twice.

+2


source


I faced 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);

      

Pay attention to the last line that calls setUseParentHandlers

. Without this line, I get duplicate logs. I checked at runtime that in my case the number of handlers returned logger.getHandlers()

is 1 after adding a 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 am not getting duplicate logs, but I am not getting anything more subtle than the INFO level.

+1


source


Just remove all handlers before adding them to your log, for example:

for (Handler handler : logger.getHandlers()) {  logger.removeHandler(handler);}

      

+1


source







All Articles