public void init() throws ServletException { String prefix = getServletContext().getRealPath("/"); prefix = prefix.replace("\\", "/"); String file = getInitParameter("log4j-init-file"); // if the log4j-init-file is not set, then no point in trying if (file != null) { PropertyConfigurator.configure(prefix + file); } } } package com.dheaven.mip.web;
public void init() throws ServletException { String prefix = getServletContext().getRealPath("/"); prefix = prefix.replace("\\", "/"); String file = getInitParameter("log4j-init-file"); // if the log4j-init-file is not set, then no point in trying if (file != null) { PropertyConfigurator.configure(prefix + file); } } }
2.2.3 在Web.xml中定义初始化类
view plaincopy to clipboardprint? log4j-init chb.test.web.InitLog4j
log4j-init-file WEB-INF/log4j.properties
1
log4j-init chb.test.web.InitLog4j
log4j-init-file WEB-INF/log4j.properties
1
2.2.4 测试类
view plaincopy to clipboardprint? package chb.test.web;