配置log4j日誌熱加載

1.使用log4j提供的方法自定義類並引入到spring中.
    <!--配置log4j自動加載日誌-->  
    <bean class="com.roy.util.Log4jConfig">  
        <constructor-arg name="reload" value="true"/>  
        <constructor-arg name="interval" value="20000"/>  
    </bean>  spring

2.代碼以下:
    package com.roy.util;  
 
    import org.apache.log4j.PropertyConfigurator;  
    import org.slf4j.Logger;  
    import org.slf4j.LoggerFactory;  
      
    public class Log4jConfig {  
        private boolean reload = true;  
        private int interval = 10000;  
        private static final Logger logger = LoggerFactory.getLogger(Log4jConfig.class);  
      
        /**
         * log4j日誌自動加載
         *
         * @param reload   是否開啓自動加載
         * @param interval 自動加載時間(ms)
         */  
        public Log4jConfig(boolean reload, int interval) {  
            this.reload = reload;  
            this.interval = interval;  
            this.loadConfig();  
        }  
      
        public void loadConfig() {  
            String log4jPath = Log4jConfig.class.getClassLoader().getResource("log4j.properties").getPath();  
            logger.debug("log4j file path: " + log4jPath);  
      
            // 間隔特定時間,檢測文件是否修改,自動從新讀取配置  
            PropertyConfigurator.configureAndWatch(log4jPath, this.interval);  
        }  
    }apache

相關文章
相關標籤/搜索