How to disable default console handler using java logging API?

Hi, I am trying to implement a Java entry in my application. I want to use two handlers. File handler and my own console handler. Both of my handlers work fine. My log is sent to a file and to the console. My log is also sent to the default console handler, which I do not want. If you run my code, you will see an additional two lines sent to the console. I do not want to use the default console handler. Does anyone know how to disable the default console handler. I want to use only two handlers that I created.

Handler fh = new FileHandler("test.txt"); fh.setFormatter(formatter); logger.addHandler(fh); 



 Handler ch = new ConsoleHandler(); ch.setFormatter(formatter); logger.addHandler(ch); 



 import java.util.Date; import java.util.logging.ConsoleHandler; import java.util.logging.FileHandler; import java.util.logging.Formatter; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.LogRecord; import java.util.logging.Logger; public class LoggingExample { private static Logger logger = Logger.getLogger("test"); static { try { logger.setLevel(Level.INFO); Formatter formatter = new Formatter() { @Override public String format(LogRecord arg0) { StringBuilder b = new StringBuilder(); b.append(new Date()); b.append(" "); b.append(arg0.getSourceClassName()); b.append(" "); b.append(arg0.getSourceMethodName()); b.append(" "); b.append(arg0.getLevel()); b.append(" "); b.append(arg0.getMessage()); b.append(System.getProperty("line.separator")); return b.toString(); } }; Handler fh = new FileHandler("test.txt"); fh.setFormatter(formatter); logger.addHandler(fh); Handler ch = new ConsoleHandler(); ch.setFormatter(formatter); logger.addHandler(ch); LogManager lm = LogManager.getLogManager(); lm.addLogger(logger); } catch (Throwable e) { e.printStackTrace(); } } public static void main(String[] args) { logger.info("why does my test application use the standard console logger ?\n" + " I want only my console handler (Handler ch)\n " + "how can i turn the standard logger to the console off. ??"); } } 
+82
java java.util.logging
Mar 28 '10 at 14:25
source share
5 answers

By default, the console handler connects to the root log, which is the parent of all other registrars, including yours. Therefore, I see two ways to solve your problem:

If this only affects your specific class, the easiest solution would be to turn off log transfers to the parent logger:

 logger.setUseParentHandlers(false); 

If you want to change this behavior for your application, you can remove the default console handler from the root logger before adding your own handlers:

 Logger globalLogger = Logger.getLogger("global"); Handler[] handlers = globalLogger.getHandlers(); for(Handler handler : handlers) { globalLogger.removeHandler(handler); } 

Note. If you want to use the same log handlers in other classes, it is best to move the log configuration to the configuration file in the end.

+86
Mar 28 '10 at 14:34
source share

Just do

 LogManager.getLogManager().reset(); 
+87
Jul 29 '10 at 2:48
source share

This is strange, but Logger.getLogger("global") does not work in my setup (as well as Logger.getLogger(Logger.GLOBAL_LOGGER_NAME) ).

However, Logger.getLogger("") does the job well.

Hope this information also helps someone ...

+17
Jun 29 '10 at 14:34
source share

Reset the configuration and set the root level to OFF

 LogManager.getLogManager().reset(); Logger globalLogger = Logger.getLogger(java.util.logging.Logger.GLOBAL_LOGGER_NAME); globalLogger.setLevel(java.util.logging.Level.OFF); 
+9
Feb 15 '11 at 11:48
source share

You must tell your registrar not to send messages to the parent registrar:

 ... import java.util.logging.*; ... Logger logger = Logger.getLogger(this.getClass().getName()); logger.setUseParentHandlers(false); ... 

However, this must be done before adding more handlers to the registrar.

+4
Mar 17 2018-11-12T00:
source share



All Articles