log4j2.xml和log4j.properties的指定配置路径方法

对于默认直接把配置文件放任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;
    }
}








  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值