Exception("参数空。");
}
logger.debug("EXIT");
}
}
Java的开源项目:简单介绍Log4J的使用(4)
时间:2010-12-24
MyServlet.java
package com.webage.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.webage.model.MyModel;
public class MyServlet extends HttpServlet {
Logger logger = Logger.getLogger(MyServlet.class);
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
logger.debug("ENTRY");
MyModel model = new MyModel();
resp.getWriter().println("Log4J测试");
try {
model.checkValid("firstName", req.getParameter("firstName"));
} catch (Exception e) {
logger.error("doGet出现错误.", e);
}
logger.debug("EXIT");
}
public void init() throws ServletException {
super.init();
logger.info("Servlet初始化...");
}
}
需要两个jar包:servlet.jar和log4j-1.2.9.jar。
准备就绪,运行之于tomcat或者weblogic,笔者暂用weblogic代之。每当运行Logger logger = Logger.getLogger(MyServlet.class);时即生成E:\wangzj\myapplication.log文件。 |