java logging API,禁用日志记录到标准输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6077267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java logging API, disable logging to standard output
提问by AgostinoX
Using the standard java logging API (import java.util.logging.Logger), after the construction:
使用标准的java日志API(import java.util.logging.Logger),构建后:
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.
我已经能够记录一些东西了。由于它没有 FileHandler,它不会向磁盘写入任何内容。
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
它将(部分而非全部)日志消息写入输出。如何禁用此功能?预先感谢阿戈斯蒂诺
采纳答案by AgostinoX
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
如果您不知道 java util logging 的默认配置,就会出现问题。架构事实:0)每个记录器,无论其名称如何,都将根记录器作为父记录器。默认事实:1) 记录器属性 useParentHandlers 默认为 true 2) 根记录器默认有一个 ConsoleHandler
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).
所以。一个新的记录器,默认情况下也会将其日志记录发送到他的父记录器(点 1),即根记录器(点 0),默认情况下,将它们记录到控制台(点 2)。
Remove console logging is easy as:
删除控制台日志很容易,因为:
Logger l0 = Logger.getLogger("");
l0.removeHandler(l0.getHandlers()[0]);
回答by Michal
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:
Java 中的标准 Logger 是分层结构的,子 Logger 默认继承其父级的 Handler。试试这个来禁止使用父处理程序:
l.setUseParentHandlers(false);
回答by OscarRyz
By disable this featureyou 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
通过禁用此功能,你的意思是你不希望记录了什么呢?如果是这种情况,您必须将该记录器的级别设置为 NONE
For instance, if you're using a logging properties file you can set:
例如,如果您使用的是日志属性文件,您可以设置:
mylogger.level=NONE