Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java logging API, disable logging to standard output

Tags:

java

logging

Using the standard java logging API (import java.util.logging.Logger), after the construction:

Logger l = Logger.getLogger("mylogger");

I am already able to log something. Since it has not a FileHandler, it doesn't write anything to disk.

l.severe("test with no handler");

It writes (some, not all) the log messages to output. How can I disable this feature? thanks in advance Agostino

like image 809
AgostinoX Avatar asked May 20 '11 20:05

AgostinoX


People also ask

Why logger is use instead of system out Println?

If you are running a Java program in Linux or any UNIX-based system, Log4j or SLF4j or any other logging framework offers a lot more features, flexibility, and improvement on message quality, which is not possible using the System. out. println() statement.


2 Answers

The question arises if you don't know the default configuration of java util logging. Architectural fact: 0)Every logger whatever its name is has the root logger as parent. Default facts: 1) the logger property useParentHandlers is true by default 2) the root logger has a ConsoleHandler by default

So. A new logger, by default sends its log records also to his parent(point 1) that is the root logger(point 0) wich, by default, logs them to console(point 2).

Remove console logging is easy as:

Logger l0 = Logger.getLogger("");
l0.removeHandler(l0.getHandlers()[0]);
like image 197
AgostinoX Avatar answered Oct 02 '22 03:10

AgostinoX


Standard Loggers in Java are in a hierarchical structure and child Loggers by default inherit the Handlers of their parents. Try this to suppress parent Handlers from being used:

l.setUseParentHandlers(false);
like image 32
Michal Krasny Avatar answered Oct 02 '22 04:10

Michal Krasny