[英]How to set relative path for log4j.properties file in Websphere
[英]set an absolute path for a “log4j.properties” file
我正在為我的Web應用程序使用apache commons + log4j。
通常,log4j在類路徑中需要一個配置文件; 但是我需要將日志記錄配置委派給外部文件(我需要在環境中部署.war,但是日志配置(最大大小,位置等)取決於第二小組。
我的類路徑中有commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
# log4j.configuration=/absolute/path/where/external/logs/are/log4j.properties
不幸的是,注釋行不起作用。
有沒有辦法使用外部配置文件設置log4j?
您可以將其設置為系統屬性log4j.configuration
屬性..例如在J2SE應用程序中
java -Dlog4j.configuration=file:/path/to/log4j.properties myApp
請注意,該屬性值必須是URL。
有關更多信息,請閱讀Log4j手冊中的 “默認初始化過程”部分。
也可以讓ServletContextListener設置System屬性:
import java.util.Enumeration;
import javax.servlet.*;
public class SystemPropertiesHelper implements
javax.servlet.ServletContextListener {
private ServletContext context = null;
public void contextInitialized(ServletContextEvent event) {
context = event.getServletContext();
Enumeration<String> params = context.getInitParameterNames();
while (params.hasMoreElements()) {
String param = (String) params.nextElement();
String value =
context.getInitParameter(param);
if (param.startsWith("customPrefix.")) {
System.setProperty(param, value);
}
}
}
public void contextDestroyed(ServletContextEvent event) {
}
}
然后將其放入您的web.xml(context.xml也應該是可能的)
<context-param>
<param-name>customPrefix.property</param-name>
<param-value>value</param-value>
<param-type>java.lang.String</param-type>
</context-param>
<listener>
<listener-class>servletUtils.SystemPropertiesHelper</listener-class>
</listener>
我是從answer的此偵聽器代碼中獲得的。
希望對您有所幫助!
您可以使用jvm參數來指示配置文件路徑:
-Dlog4j.configuration=absolute path
絕對路徑的示例:
java -Dlog4j.configuration="file:/dir1/log4j.properties"
如果您使用的是Spring Framework ,則可以使用org.springframework.web.util.Log4jConfigListener
。
您只需要指定參數log4jConfigLocation
和文件位置(使用File的URL )。 例如:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>file:/absolute/path/where/external/logs/are/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>1000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
您可以指定log4jRefreshInterval
(配置文件刷新檢查之間的間隔),以毫秒為單位。
請參閱org.springframework.web.util.Log4jWebConfigurer
的javadoc中的更多內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.