public class Log4jInit extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* 初始化Log4j的配置
* @param
* Date: 2012-5-15上午10:40:49
*/
public void init() {
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j-init-file");
if (file != null) {
DOMConfigurator.configure(prefix + file);
//this.listCofig(); // 调试用
}
}
private void listCofig() {
LoggerRepository logRp = org.apache.log4j.LogManager
.getLoggerRepository();
Logger root = logRp.getRootLogger();
showLogger(root);
Enumeration<?> enumeration = logRp.getCurrentLoggers();
if (enumeration != null) {
while (enumeration.hasMoreElements()) {
Logger log = (Logger) enumeration.nextElement();
showLogger(log);
}
}
}
private void showLogger(Logger log) {
Enumeration<?> enumeration = log.getAllAppenders();
if (enumeration != null && enumeration.hasMoreElements()) {
System.out.println(">>>log name():" + log.getName());
while (enumeration.hasMoreElements()) {
Appender append = (Appender) enumeration.nextElement();
System.out.println(" append ref.getName():" + append.getName());
}
System.out.println(">>>log name end");
}
}
}