日志框架实现实时修改,实时生效,具体框架思路(7)

来源:互联网 发布:mac 登陆apple store 编辑:程序博客网 时间:2024/05/18 00:49

在第一章中有提到编写一个监听器,实现定时刷新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);
    }
}

0 0