利用【监听器】动态加载Log4j配置文件

1 创建监听器:
public class LogListener implements ServletContextListener
{
	private Log log = LogFactoryImpl.getFactory().getLog(LogListener.class);
	@Override
	public void contextDestroyed(ServletContextEvent arg0)
	{
		if (log.isDebugEnable())
		{
			log.debug("销毁LogListener....");
		}
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0)
	{	
	    // 配置log4j
        String filePath = "src/conf/log4j.xml";
		DOMConfigurator.configure(filePath);
		if (log.isDebugEnable())
		{
			log.debug("初始化Web日志成功....");
		}
	}



2 修改web.xml 注册监听器
	<listener>
		<listener-class>com.log.LogListener</listener-class>
	</listener>	



你可能感兴趣的:(Web,log4j)