编辑servlet:
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

1

2

3

4

5

6

7

8

9

10

还有加上log4j.properties文件在WEB-INF目录下
log4j.rootLogger=INFO, A1 , R
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%nlog4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=log4j.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n下面写了两个测试的程序:
servlet Test.java中:
1public void doGet(HttpServletRequest request, HttpServletResponse response)
2throws ServletException, IOException {
3
4response.setContentType("text/html");
5PrintWriter out = response.getWriter();
6Logger logger = Logger.getLogger(Test.class);
7logger.info("Test use Logging for Servlet");
8out.println("<h1>Success<h1>");
9out.flush();
10out.close();
11}
12
13JSP temp.jsp中:1<%
2response.setContentType("text/html");
3Logger logger = Logger.getLogger("temp.jsp");
4logger.info("Test use Logging for JSP");
5out.println("<h1>Success<h1>");
6%>
7这样在%TOMCAT_HOME%\bin目录下会产生log4j.log的文件,这就是日志文件。