1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveutils.web.logging; |
16 | |
17 | import java.net.MalformedURLException; |
18 | import java.util.regex.Pattern; |
19 | |
20 | import javax.servlet.ServletContext; |
21 | import javax.servlet.ServletContextEvent; |
22 | import javax.servlet.ServletContextListener; |
23 | |
24 | import org.apache.log4j.LogManager; |
25 | import org.apache.log4j.xml.DOMConfigurator; |
26 | |
27 | /** |
28 | * ServletContextListener that will initialize log4j at web appalication start. |
29 | * <p> |
30 | * It must be setup in web.xml as <listener>. |
31 | * Following parameters are given as <context-param> in web.xml: |
32 | * <ul> |
33 | * <li><code>config.logging</code> defines the path to log4j configuration |
34 | * file</li> |
35 | * <li><code>config.logging.dir</code> defines the path to where log files |
36 | * should be written. That path will set the <code>logdir</code> property |
37 | * that can be used in log4j configuration file</li> |
38 | * </ul> |
39 | * Please note that currently only xml configuration files are supported. |
40 | * |
41 | * @author Jean-Francois Poilpret |
42 | */ |
43 | public class LoggingInitListener implements ServletContextListener |
44 | { |
45 | static final private String LOGDIR_KEY = "logdir"; |
46 | |
47 | public void contextInitialized(ServletContextEvent evt) |
48 | { |
49 | // Get information from init parameters for log4j |
50 | ServletContext context = evt.getServletContext(); |
51 | String config = context.getInitParameter("config.logging"); |
52 | String logdir = context.getInitParameter("config.logging.dir"); |
53 | // Initialize Log4j |
54 | if (config != null && config.length() > 0) |
55 | { |
56 | LogManager.resetConfiguration(); |
57 | // Set System properties |
58 | System.setProperty(LOGDIR_KEY, logdir); |
59 | |
60 | // Check if relative path or not /^([a-zA-Z]:)?\// |
61 | // if (!Pattern.matches("^([a-zA-Z]:)?/.*", config)) |
62 | if (!Pattern.matches("^(([a-zA-Z]:(/|\\\\))|/).*", config)) |
63 | { |
64 | try |
65 | { |
66 | DOMConfigurator.configure(context.getResource("/" + config)); |
67 | } |
68 | catch (MalformedURLException e) |
69 | { |
70 | throw new RuntimeException(e); |
71 | } |
72 | } |
73 | else |
74 | { |
75 | DOMConfigurator.configure(config); |
76 | } |
77 | _logStarted = true; |
78 | } |
79 | } |
80 | |
81 | public void contextDestroyed(ServletContextEvent evt) |
82 | { |
83 | if (_logStarted) |
84 | { |
85 | LogManager.shutdown(); |
86 | _logStarted = false; |
87 | } |
88 | } |
89 | |
90 | private boolean _logStarted = false; |
91 | } |