Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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();
like image 464
Sarp Avatar asked May 25 '18 16:05

Sarp


People also ask

Does Java Util logging use log4j?

The JDK Logging Adapter is a custom implementation of java. util. logging. LogManager that uses Log4j.


1 Answers

For a global impact you can use LogManager.reset() as your call to disable logging and to re-enable logging you can LogManager.readConfiguration(). However, there are some major flaws in what actually gets reloaded. The newer JDK9 method LogManager.updateConfiguration should be used instead.

If you know the logger name you can edit your logging.properties to modify the level of that offending logger or create a filter.

From code you can locate the logger and change it's level to OFF or something higher than the log messages you are seeing.

final Logger chatty = Logger.getLogger("somelogger");
final Level oldLevel = chatty.getLevel();
chatty.setLevel(Level.OFF);
try {
  taskThatMakesUnnecessaryLogs();
} finally {
  chatty.setLevel(oldLevel);
}
like image 117
jmehrens Avatar answered Sep 25 '22 22:09

jmehrens