Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable the default console handler, while using the java logging API?

Hi I am trying to implement the java logging in my application. I want to use two handlers. A file handler and my own console handler. Both of my handlers work fine. My logging is send to a file and to the console . My logging is also sent to the default console handler, which i do not want. If you run my code you will see extra two line sent to the console. I don't want to use the default console handler. Does anyone know how to disable the default console handler. I only want to use the two handlers I have 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. ??");     } } 
like image 611
loudiyimo Avatar asked Mar 28 '10 14:03

loudiyimo


People also ask

What is Java logging used for?

In Java, Logging is an API that provides the ability to trace out the errors of the applications. When an application generates the logging call, the Logger records the event in the LogRecord. After that, it sends to the corresponding handlers or appenders.

What is Java logging package?

Logging in Java requires using one or more logging frameworks. These frameworks provide the objects, methods, and configuration necessary to create and send log messages. Java provides a built-in framework in the java. util. logging package.


2 Answers

Just do

LogManager.getLogManager().reset(); 
like image 81
Dominique Avatar answered Sep 18 '22 15:09

Dominique


The default console handler is attached to the root logger, which is a parent of all other loggers including yours. So I see two ways to solve your problem:

If this is only affects this particular class of yours, the simplest solution would be to disable passing the logs up to the parent logger:

logger.setUseParentHandlers(false); 

If you want to change this behaviour for your whole app, you could remove the default console handler from the root logger altogether 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 too, the best way is to move the log configuration into a config file in the long run.

like image 33
Péter Török Avatar answered Sep 19 '22 15:09

Péter Török