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

参考博客:https://blog.csdn.net/mathlpz126/article/details/79130977

方法一:代码配置(耦合性强

在main函数中添加如下代码

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();
        }  
    }  
}

方法二:在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>

 

方法三:

将log4j.properties文件放在工程src/main/resources目录下

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值