对于默认直接把配置文件放任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"
);
........
//省略
<!-- 系统日志配置监听器 -->
<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;
}
}