BX21
BX21

Reputation: 461

websphere liberty - application specific logging configuration

I am using SLF4J in my application which is deployed on websphere liberty profile. I want to configure it so that 1- logging configuration (such as log4j properties if I use log4j with slf4j) resides outside the application war file, 2 - The logging configuration applies only to my application and not to other applications deployed on the same profile.

(1) can be achieved by using slf4j with java.util.logging since websphere liberty is integrated with that, however the logging configuration applies to the entire profile

However, what would be the best way to get both (1) and (2) ?

Upvotes: 1

Views: 704

Answers (1)

BX21
BX21

Reputation: 461

I ended up using the org.apache.log4j.PropertyConfigurator for this along with the java.nio.file.WatchService.

The watchservice will watch a log4j.properties file at a predefined location for changes. If the file is changed it will call this method

/**
 * Update configuration.
 */
public void updateConfiguration() {
    Path path = configPath.resolve(LOG_FILE_NAME);
    if (Files.exists(path)){
        System.out.println(MessageFormat.format("Reloading logging configuration from {0}", path.toAbsolutePath()));
        PropertyConfigurator.configure(path.toAbsolutePath().toString());
    } else {
        System.err.println(MessageFormat.format("log4j.properties file was expected at {0} but was not present. Please create this file and configure it for logging to work.", path.toAbsolutePath()));
    }
}

This will allow for a log configuration file outside the application which will also be updateable at runtime.

Upvotes: 2

Related Questions