jul-to-slf4j for specific classes only

a 夏天 提交于 2019-12-20 05:33:25

问题


I'm working on a JSF-Project with Primefaces on Websphere Application Server. Since Primefaces uses java.util.logging, I'm using the jul-to-slf4j Bridge to capture the Primefacs Logs into my Log4J Logfile.

But using the jul-to-slf4j Bridge, all java.util.logging Messages will be in my Logfile, including general Websphere Messages like "Application started" or "Server started".

Can the jul-to-slf4j be configured, so it only redirects specific Messages (e.g. everything from org.primefaces) to SLF4j and leave the rest as it is?


回答1:


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!



来源:https://stackoverflow.com/questions/33867514/jul-to-slf4j-for-specific-classes-only

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