websphere liberty - 特定于应用程序的日志记录配置
websphere liberty - application specific logging configuration
我在部署在 websphere liberty profile 上的应用程序中使用 SLF4J。我想对其进行配置,以便 1- 日志记录配置(例如 log4j 属性,如果我将 log4j 与 slf4j 一起使用)驻留在应用程序 war 文件之外,2 - 日志记录配置仅适用于我的应用程序,而不适用于部署的其他应用程序在同一个人资料上。
(1) 可以通过将 slf4j 与 java.util.logging 一起使用来实现,因为 websphere liberty 与其集成,但是日志记录配置适用于整个配置文件
但是,获得 (1) 和 (2) 的最佳方法是什么?
我最终使用了 org.apache.log4j.PropertyConfigurator
和 java.nio.file.WatchService
。
watchservice 将在预定义位置监视 log4j.properties 文件的更改。如果文件被更改,它将调用此方法
/**
* 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()));
}
}
这将允许应用程序外部的日志配置文件,该文件也可在运行时更新。
我在部署在 websphere liberty profile 上的应用程序中使用 SLF4J。我想对其进行配置,以便 1- 日志记录配置(例如 log4j 属性,如果我将 log4j 与 slf4j 一起使用)驻留在应用程序 war 文件之外,2 - 日志记录配置仅适用于我的应用程序,而不适用于部署的其他应用程序在同一个人资料上。
(1) 可以通过将 slf4j 与 java.util.logging 一起使用来实现,因为 websphere liberty 与其集成,但是日志记录配置适用于整个配置文件
但是,获得 (1) 和 (2) 的最佳方法是什么?
我最终使用了 org.apache.log4j.PropertyConfigurator
和 java.nio.file.WatchService
。
watchservice 将在预定义位置监视 log4j.properties 文件的更改。如果文件被更改,它将调用此方法
/**
* 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()));
}
}
这将允许应用程序外部的日志配置文件,该文件也可在运行时更新。