日誌框架實現實時修改,實時生效,具體框架思路(7)

在第一章中有提到編寫一個監聽器,實現定時刷新log4j.propertes文件,實現修改之後日誌文件生效。


該listener必須在Spring的listener後面啓動,否則無法啓動。

具體的web.xml配置 如下:

<listener>
                <listener-class>com.work.log.listener.LogStartListener</listener-class>
</listener>


監聽類的具體實現如下:

package com.work.log.listener;


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


import org.apache.log4j.PropertyConfigurator;


/**
 * 日誌啓動Listener 該listener必須在Spring的listener後面啓動,否則無法啓動
 * @version 
 * @see 
 * @since 
 */
public class LogStartListener implements ServletContextListener
{


    private static final int LOG4J_SCAN_TIME = 10;


    /**
     * 系統停止
     * @param event ServletContextEvent
     */
    public void contextDestroyed(ServletContextEvent event)
    {
    }


    /**
     * 系統啓動
     * @param event ServletContextEvent
     */
    public void contextInitialized(ServletContextEvent event)
    {
        // 啓動Log4j配置文件監聽
        configureAndWatch();
        // 啓動數據庫日誌
    }
    /**
     * 設置Log4j定時讀取配置文件
     * @see [類、類#方法、類#成員]
     */
    private void configureAndWatch()
    {
        // 定時掃描
        PropertyConfigurator.configureAndWatch("log4j.propertes", LOG4J_SCAN_TIME);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章