jul-to-slf4j for specific classes only

只愿长相守 提交于 2019-12-02 03:59:11

I found a solution using java.util.logging.Filter, the filter just checks, if the name of the logger starts with org.primefaces :

For this Solution, one has to set the SLF4JBridgeHandler and the Filter programmatically, setting it with the logging.properties file wont work.

Also, one hast to create the SLF4JBridgeHandler himself, due what is afaik a Bug, the SLF4JBridgeHandler dosn't respect the Filter out of the box.

SLF4JBridgeHandler slf4jBridgeHandler = new SLF4JBridgeHandler(){
    @Override
    public void publish(LogRecord record) {
         if (getFilter() != null && !getFilter().isLoggable(record)) {
                return;
            }
        super.publish(record);
    }
};

Filter filter = new Filter() {

    @Override
    public boolean isLoggable(LogRecord record) {
        String loggerName = record.getLoggerName();
        boolean loggable = loggerName != null && (loggerName.startsWith("org.primefaces"));
        return loggable;
    }
}; 
slf4jBridgeHandler.setFilter(filter);
java.util.logging.LogManager.getLogManager().getLogger("").addHandler(slf4jBridgeHandler);

Do not call SLF4JBridgeHandler.removeHandlersForRootLogger() since that will remove every other Handler that was set by Websphere!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!