EMMA Coverage Report (generated Tue Feb 12 22:23:49 ICT 2008)
[all classes][net.sourceforge.hiveutils.web.logging]

COVERAGE SUMMARY FOR SOURCE FILE [LoggingInitListener.java]

nameclass, %method, %block, %line, %
LoggingInitListener.java0%   (0/1)0%   (0/3)0%   (0/63)0%   (0/20)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class LoggingInitListener0%   (0/1)0%   (0/3)0%   (0/63)0%   (0/20)
LoggingInitListener (): void 0%   (0/1)0%   (0/6)0%   (0/2)
contextDestroyed (ServletContextEvent): void 0%   (0/1)0%   (0/8)0%   (0/4)
contextInitialized (ServletContextEvent): void 0%   (0/1)0%   (0/49)0%   (0/14)

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 
15package net.sourceforge.hiveutils.web.logging;
16 
17import java.net.MalformedURLException;
18import java.util.regex.Pattern;
19 
20import javax.servlet.ServletContext;
21import javax.servlet.ServletContextEvent;
22import javax.servlet.ServletContextListener;
23 
24import org.apache.log4j.LogManager;
25import 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 &lt;listener&gt;.
31 * Following parameters are given as &lt;context-param&gt; 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 */
43public 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}

[all classes][net.sourceforge.hiveutils.web.logging]
EMMA 2.0.5312 (C) Vladimir Roubtsov