简体   繁体   English

有没有办法禁用java.util.logging并在以后将其启用?

[英]Is there a way to disable java.util.logging and enable it back later?

I'm using a library that logs a bunch of stuff that I don't want to log. 我正在使用一个库,该库记录了许多我不想记录的内容。 It's using java.util.logging . 它使用java.util.logging

I also don't want to completely disable its logging since some of its logs are useful. 我也不想完全禁用其日志记录,因为其某些日志很有用。 Since the unwanted lines are logged only when I run a certain task, if there was a global way of disabling and enabling logging it would solve my problem. 由于不想要的行仅在我运行特定任务时才记录,因此,如果有全局禁用和启用记录的方式,它将解决我的问题。

Ideally something like: 理想情况是:

disableLogging();
taskThatMakesUnnecessaryLogs();
enableLogging();

For a global impact you can use LogManager.reset() as your call to disable logging and to re-enable logging you can LogManager.readConfiguration() . 对于全局影响,您可以使用LogManager.reset()作为调用来禁用日志记录并重新启用日志记录,可以使用LogManager.readConfiguration() However, there are some major flaws in what actually gets reloaded . 但是, 实际重新加载的内容存在一些重大缺陷 The newer JDK9 method LogManager.updateConfiguration should be used instead. 应该使用更新的JDK9方法LogManager.updateConfiguration

If you know the logger name you can edit your logging.properties to modify the level of that offending logger or create a filter . 如果知道记录器的名称,则可以编辑logging.properties来修改该记录器的级别或创建过滤器

From code you can locate the logger and change it's level to OFF or something higher than the log messages you are seeing. 通过代码,您可以找到记录器并将其级别更改为OFF或比您看到的日志消息更高的级别。

final Logger chatty = Logger.getLogger("somelogger");
final Level oldLevel = chatty.getLevel();
chatty.setLevel(Level.OFF);
try {
  taskThatMakesUnnecessaryLogs();
} finally {
  chatty.setLevel(oldLevel);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM