AgostinoX
AgostinoX

Reputation: 7683

java logging API, disable logging to standard output

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

Upvotes: 12

Views: 19088

Answers (2)

Michal Krasny
Michal Krasny

Reputation: 5916

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

Upvotes: 12

AgostinoX
AgostinoX

Reputation: 7683

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]);

Upvotes: 12

Related Questions