View Javadoc

1   /*   Copyright (C) 2004 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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           // get config-file from classpath
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           // ServerConstants().getClass().getResourceAsStream("cgcp.properties)
76  
77           // PropertyConfigurator.configure(url);
78           BasicConfigurator.configure();
79           // load config
80           // DOMConfigurator.configure(url);
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