java logging API, disable logging to standard output

你离开我真会死。 提交于 2019-12-04 19:31:01

问题


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:


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



回答2:


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



回答3:


By disable this feature you mean you don't want to log anything at all? If that's the case you have to set the level of that logger to NONE

For instance, if you're using a logging properties file you can set:

mylogger.level=NONE


来源:https://stackoverflow.com/questions/6077267/java-logging-api-disable-logging-to-standard-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!