1
votes

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

1 Answers

1
votes

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!