1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.finalist.util.log;
19
20 import org.apache.log4j.*;
21
22 import javax.servlet.http.HttpServlet;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25 import java.net.URL;
26
27 public class InitLoggerServlet extends HttpServlet {
28
29
30 public static final String LOG4_FILENAME = "log4j";
31
32
33 public InitLoggerServlet() {
34 }
35
36
37 public void init() {
38 System.out.println("Trying to initialize the logger from the servlet.");
39 initLog4j();
40 Category log = Category.getInstance(InitLoggerServlet.class);
41 log.info("Init servlet initialized the logger.");
42 }
43
44
45 /***
46 * Initializes log4j.
47 */
48 private void initLog4j() {
49 try {
50 System.out.println("Using system classloader now.");
51
52 ClassLoader cl = this.getClass().getClassLoader().getSystemClassLoader();
53 if (cl == null) {
54 System.err.println("Error: Can not get classloader to init log4j.");
55 return;
56 }
57 URL url = cl.getResource(LOG4_FILENAME);
58 if (url == null) {
59 System.out.println("Error: Can not find config-file for log4j in classpath: " + LOG4_FILENAME);
60 System.out.println("Doing a Basicconfigure.");
61 }
62
63 url = cl.getResource(LOG4_FILENAME + ".properties");
64 if (url == null) {
65 System.out.println("Error: Can not find config-file for log4j in classpath: " + LOG4_FILENAME + ".properties");
66 System.out.println("Doing a Basicconfigure.");
67 }
68 url = new InitLoggerServlet().getClass().getClassLoader().getResource(LOG4_FILENAME + ".properties");
69 if (url == null) {
70 System.out.println("Error: Can not find config-file for log4j in classpath using the constructor: " + LOG4_FILENAME + ".properties");
71 System.out.println("Doing a Basicconfigure.");
72 }
73
74
75
76
77
78 BasicConfigurator.configure();
79
80
81
82 }
83 catch (Throwable e) {
84 System.err.println("Error: Can not init logging (log4j).");
85 e.printStackTrace();
86 }
87 }
88
89
90 public
91 void doGet(HttpServletRequest req, HttpServletResponse res) {
92 }
93 }
94