12

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

1
  • 1
    l.setLevel(Level.OFF); Commented Nov 14, 2017 at 13:56

2 Answers 2

12

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]);
Sign up to request clarification or add additional context in comments.

Comments

12

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

2 Comments

Please explain why your answer would work instead of just giving an answer.
i get ` error: <identifier> expected`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.