View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2toolkit library.
13   *
14   *  The Initial Owner of the Original Code is
15   *  Power Of Two S.R.L. (www.pow2.com)
16   *
17   *  Portions created by Power Of Two S.R.L. are
18   *  Copyright (C) Power Of Two S.R.L.
19   *  All Rights Reserved.
20   *
21   * Contributor(s):
22   */
23  
24  package  com.pow2.resources;
25  
26  
27  import javax.servlet.ServletException;
28  
29  import org.apache.log4j.PropertyConfigurator;
30  
31  import com.pow2.util.Util;
32  
33  
34  /***
35   *  Log4J init servlet.
36   *  <br>
37   *  Configure and initialize Log4j defining the following servlet
38   *  into the web.xml file of your web-application.
39   *
40   *  <code>
41   *  <pre>
42   *  &lt;servlet&gt;
43   *    &lt;servlet-name&gt;log4j-init&lt;/servlet-name&gt;
44   *    &lt;servlet-class&gt;com.pow2.resources.Log4jInit&lt;/servlet-class&gt;
45   *
46   *    &lt;init-param&gt;
47   *      &lt;param-name&gt;log4j-init-file&lt;/param-name&gt;
48   *      &lt;param-value&gt;WEB-INF/log4j.properties&lt;/param-value&gt;
49   *    &lt;/init-param&gt;
50   *
51   *    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
52   *  &lt;/servlet&gt;
53   *  </pre>
54   *  </code>
55   *
56   *  See
57   *  <ul>
58   *    <li><a href="http://jakarta.apache.org/log4j/docs/index.html">http://jakarta.apache.org/log4j/docs/index.html</a></li>
59   *    <li><a href="http://jakarta.apache.org/log4j/docs/manual.html">http://jakarta.apache.org/log4j/docs/manual.html</a></li>
60   *  </ul>
61   *  for further details.
62   */
63  public class Log4jInit extends InitServlet
64  {
65    /***
66     *  Configure Log4J using the configuration file whose path is set
67     *  by the <code>log4j-init-file</code> parameter
68     *  of the <code>web.xml</code> file.
69     */
70    public void init() throws ServletException
71    {
72      super.init();
73      String file = getInitParameterFilePath("log4j-init-file");
74  
75      if (Util.isNull(file))
76      {
77        String err = "cannot configure log4j logging package. Log4J config file is null";
78        throw new ServletException(err);
79      }
80  
81      PropertyConfigurator.configure(file);
82      cat.info("::init - log4j successfully configured.");
83    }
84  }