0

The application I'm working on provides logging based on the current user. To achieve this I use the log4j AbstractFilter which checks if there is a custom log level for the current user and returns the appropriate result for methods such as Log.isDebugEnabled() etc.

Some third party libraries used by my application use java.util.logging. I would like to have those log the same way my own logging does. But the java.util.logging.Logger.isLoggable() method returns different results than my own logging. Is there some way to overwrite this function with my own logic?

Edit: I use slf4j in my application with log4j2 as the underlying logging framework. I tried to use the java.util.logging ~> slf4j bridge as described here, but it seems only to affect the Log function itself, not the isLoggable().

Jdv
  • 962
  • 10
  • 34

1 Answers1

0

In java.util.logging you can modify the isLoggable behavior by implementing a java.util.logging.Filter and setting the appropriate level.

From the source code of the SLF4JBridgeHandler it appears that the publish method is not invoking the isLoggable on the handler. Because of that, you can't simply install a java.util.logging.Filter to the SLF4JBridgeHandler.

One option would be to wrap the SLF4JBridgeHandler in a proxy handler such as MemoryHandler. This can be setup in a logging.properties file or via code:

MemoryHandler h = new MemoryHandler(new SLF4JBridgeHandler(), 1, Level.ALL);
h.setLevel(Level.ALL);
h.setFilter(new CurrentUserFilter()); //Your custom code.
java.util.logging.Logger.getLogger("").addHandler(h); //Install the bridge manually.

Another option is to install log4 filter on all the loggers. That can be a lot of work if all of the loggers are based on class name an not subsystems.

jmehrens
  • 10,580
  • 1
  • 38
  • 47