01 import org.apache.log4j.Logger;
02 import org.apache.log4j.BasicConfigurator;
03
04 import javax.servlet.*;
05 import javax.servlet.http.*;
06 import java.io.PrintWriter;
07
08 public class LoggerNotConfig extends HttpServlet {
09
10 private Logger log = null;
11
12 public void init(){
13 //použijeme root logger
14 log = Logger.getRootLogger();
15 //logovanie pôjde na konzolu
16 BasicConfigurator.configure();
17 }
18
19 public void doGet(HttpServletRequest request,HttpServletResponse response)
20 throws ServletException, java.io.IOException {
21
22 //úroveň DEBUG
23 log.debug("Posielam DEBUG message ...");
24
25 //úroveň INFO
26 log.info("Posielam INFO message ...");
27
28 //zobrazíme aj nejaké HTML
29 response.setContentType("text/html");
30 PrintWriter out = response.getWriter();
31 out.println("<html><head></head><body>");
32 out.println("<h2>Logger without config file!</h2>");
33 out.println("Logger name is: " + log.getName()+"<br>");
34 out.println("</body></html>");
35 }
36
37 public void doPost(HttpServletRequest request,HttpServletResponse response)
38 throws ServletException, java.io.IOException {
39 doGet(request,response);
40 }
41 }
|