log4j2.xml和log4j.properties的指定配置路徑方法
阿新 • • 發佈:2019-01-30
對於預設直接把配置檔案放任classpath下面,這種henjiandan
首先說下log4j的配置,有兩種方法:
1、在web.xml中配置(推薦):
<!-- 配置log4j日誌載入檔案 log4j.properties --> <context-param> <param-name>webAppRootKey</param-name> <param-value>webapp.devportal</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>file:/opt/config/open_portal/opendev/appconfig/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
2、程式碼配置(耦合性強):
package com.cmcc.open.devportal.omae.util; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import com.cmcc.open.base.utils.ConfigurationUtil; public class Log4jConfig { private boolean reload = true; private int interval = 60000; private static Logger log = Logger.getLogger(ConfigurationUtil.class); public Log4jConfig(boolean reload, int interval) { this.reload = reload; this.interval = interval; this.loadConfig(); } public void loadConfig() { try { String log4jPath = "/opt/config/open_portal/opendev/appconfig/log4j.properties"+""; //InputStream logis =new BufferedInputStream(new FileInputStream("D:/home/jk/platform/dev/webconfig/log4j.properties")); // 間隔特定時間,檢測檔案是否修改,自動重新讀取配置 //PropertyConfigurator.configure(logis); PropertyConfigurator.configureAndWatch(log4jPath, this.interval); log.debug("log4j file path: " + log4jPath); } catch (Exception e) { e.printStackTrace(); } } }
配置spring檔案:
<!--配置log4j自動載入日誌-->
<bean class="com.cmcc.open.utils.Log4jConfig">
<constructor-arg name="reload" value="true"/>
<constructor-arg name="interval" value="60000"/>
</bean>
再說下log4j2.xml的配置方法,這是log4j的2版本的日誌,與1.x不同,也有兩種方法:
1、可執行的jar中指定(未嘗試):
import
org.apache.logging.log4j.LogManager;
import
org.apache.logging.log4j.core.LoggerContext;
..........
//省略
LoggerContext
logContext = (LoggerContext) LogManager.getContext(
false
);
File
conFile =
new
File(
"conf/logs/log4j2.xml"
);
logContext.setConfigLocation(conFile.toURI());
logContext.reconfigure();
logger.debug(
"hello
world...{}"
,
"How
are you"
);
........
//省略
2、在web.xml中指定:
<!-- 系統日誌配置監聽器 -->
<context-param>
<description>日誌配置檔案的路徑</description>
<param-name>log4jConfigLocation</param-name>
<param-value>file:/opt/config/open_portal/openapi/appconfig/log4j2.xml</param-value>
</context-param>
<listener>
<listener-class>com.cmcc.open.common.Log4j2ConfigListener</listener-class>
</listener>
package com.cmcc.open.common;
import java.util.Enumeration;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.logging.log4j.core.config.Configurator;
public class Log4j2ConfigListener implements ServletContextListener{
private static final String KEY = "log4jConfigLocation";
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
String fileName = getContextParam(arg0);
Configurator.initialize("Log4j2", fileName);
}
private String getContextParam(ServletContextEvent event) {
Enumeration<String> names = event.getServletContext().getInitParameterNames();
while (names.hasMoreElements()){
String name = names.nextElement();
String value = event.getServletContext().getInitParameter(name);
if(name.trim().equals(KEY)){
return value;
}
}
return null;
}
}