Details
-
Bug
-
Resolution: Unresolved
-
Minor
-
None
-
1.7.12
-
None
-
Classpath:
logback-core-1.1.3.jar
logback-classic-1.1.3.jar
slf4j-api-1.7.12.jar
jul-to-slf4j-1.7.12.jar
Description
I'm trying to use the SLF4JBridgeHandler to only redirect specific messages to SLF4j.
I tried to set a java.util.logging.Filter, but the SLF4JBridgeHandler dosn't respect Filters.
If you look at the call hierarchy of java.util.logging.Filter.isLoggable(LogRecord), it seems every handler is itself responsible to call the Filter in his publish method.
Example:
SLF4JBridgeHandler.removeHandlersForRootLogger(); //Handler handler = new ConsoleHandler(); Handler handler = new SLF4JBridgeHandler(); handler.setFilter(new Filter() { @Override public boolean isLoggable(LogRecord record) { return record.getMessage().startsWith("ShouldBeShown"); } }); Logger julLogger = Logger.getLogger("com.myLogger"); julLogger.addHandler(handler); julLogger.setLevel(Level.FINEST); julLogger.info("ShouldNotBeShown info message"); julLogger.info("ShouldBeShown info message");
Output:
09:39:01.604 [main] INFO com.myLogger - ShouldNotBeShown info message 09:39:01.607 [main] INFO com.myLogger - ShouldBeShown info message
If you use the ConsoleHandler instead, the output will be as expected:
Nov 25, 2015 9:39:37 AM de.harrassowitz.test.dho.jul.JulFilterTest main INFO: ShouldBeShown info message
Workaround:
Call java.util.logging.Filter.isLoggable(LogRecord) yourself:
Handler handler = new SLF4JBridgeHandler() { @Override public void publish(LogRecord record) { if (getFilter() != null && !getFilter().isLoggable(record)) { return; } super.publish(record); } };
I didn't use java.util.logging.Handler.isLoggable(LogRecord) like the other Handlers since that method also checks the Log Level.