KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > servlet > LoggerServlet


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.servlet;
22
23 import java.io.FileInputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import java.util.Properties JavaDoc;
28
29 import javax.servlet.ServletContext JavaDoc;
30
31 import javax.servlet.http.HttpServlet JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.apache.log4j.BasicConfigurator;
36 import org.apache.log4j.Level;
37 import org.apache.log4j.PropertyConfigurator;
38
39 /**
40  * <p>
41  * A servlet to initialize the <a
42  * HREF="http://jakarta.apache.org/log4j/docs/index.html">Log4j</a> logger.
43  * This servlet sets up a very simple configuration and accepts a few
44  * optional parameters. Three init-params may be specified:
45  * </p>
46  * <ul>
47  * <li>
48  * <strong>logFile</strong>: the path of the log file. If the path begins
49  * with a forward slash it is assumed to be an absolute path, otherwise it
50  * is assumed to be a path relative to the context's root. For example:
51  * <tt>WEB-INF/log.txt</tt> or <tt>/var/log/log.txt</tt>. If this
52  * parameter is not specified, it defaults to <tt>WEB-INF/log.txt</tt>
53  * </li>
54  * <li>
55  * <strong>level</strong>: the log level. Legal values are <tt>ALL</tt>,
56  * <tt>DEBUG</tt>, <tt>ERROR</tt>, <tt>FATAL</tt>, <tt>INFO</tt>,
57  * <tt>OFF</tt>, <tt>WARN</tt>. Note that these level correspond to the
58  * log levels defined by Log4j. If this parameter is not specified, it
59  * defaults to <tt>ERROR</tt>.
60  * </li>
61  * <li>
62  * <strong>configFile</strong>: the path of a properties file containing
63  * Log4j configuration information. Like <tt>logFile</tt>, if the path
64  * begins with a forward slash it is assumed to be an absolute path,
65  * otherwise it is assumed to be a path relative to the context's root.
66  * This file should follow the conventions described in the Log4j
67  * documentation.
68  * </li>
69  * </ul>
70  * <p>
71  * If no init-params are specified, the following configuration is created:
72  * </p>
73  * <pre>
74  * log4j.appender.default=org.apache.log4j.FileAppender
75  * log4j.appender.default.file=WEB-INF/log.txt # logFile
76  * log4j.appender.default.layout=org.apache.log4j.PatternLayout
77  * log4j.appender.default.layout.ConversionPattern=org.apache.log4j.PatternLayout=%d %-5p %c - %m%n
78  * log4j.rootLogger=ERROR,default # level</pre>
79  * <p>
80  * Specifying <tt>logFile</tt> and <tt>level</tt> overrides the properties
81  * indicated above. If <tt>configFile</tt> is specified, its properties will
82  * be combined with those above, overriding any conflicting properties.
83  * </p>
84  * <p>
85  * So, as an example, if you wanted to enable DEBUG logging for this class
86  * only and you wanted to override the output format, you could add the
87  * following lines to your <tt>configFile</tt>:
88  * </p>
89  * <pre>
90  * log4j.rootLogger=OFF
91  * log4j.logger.com.methodhead.servlet.LoggerServlet=DEBUG
92  * log4j.appender.default.layout=org.apache.log4j.PatternLayout
93  * log4j.appender.default.layout.ConversionPattern=%d [%t] %-5p %c - %m%n</pre>
94  */

95 public class LoggerServlet extends HttpServlet JavaDoc {
96
97   // constructors /////////////////////////////////////////////////////////////
98

99   // constants ////////////////////////////////////////////////////////////////
100

101   // classes //////////////////////////////////////////////////////////////////
102

103   // methods //////////////////////////////////////////////////////////////////
104

105   public void init() {
106     //
107
// get init-params
108
//
109
String JavaDoc configFileParam = getInitParameter( "configFile" );
110     String JavaDoc logFileParam = getInitParameter( "logFile" );
111     String JavaDoc levelParam = getInitParameter( "level" );
112
113     //
114
// reset configuration
115
//
116
BasicConfigurator.resetConfiguration();
117
118     Properties JavaDoc props = new Properties JavaDoc();
119     props.put(
120       "log4j.appender.default",
121       "org.apache.log4j.FileAppender" );
122     props.put(
123       "log4j.appender.default.layout",
124       "org.apache.log4j.PatternLayout" );
125     props.put(
126       "log4j.appender.default.layout.ConversionPattern",
127       "%d %-5p %c - %m%n" );
128
129     //
130
// determine log file
131
//
132
String JavaDoc file = logFileParam;
133
134     if ( logFileParam == null )
135       file = getServletContext().getRealPath( "WEB-INF/log.txt" );
136
137     else if ( !logFileParam.startsWith( "/" ) ) {
138       file = getServletContext().getRealPath( logFileParam );
139
140       if ( file == null ) {
141         getServletContext().log(
142           "LoggerServlet: Couldn't get real path for " + logFileParam +
143           "; defaulting to WEB-INF/log.txt." );
144         file = getServletContext().getRealPath( "WEB-INF/log.txt" );
145       }
146     }
147
148     props.put( "log4j.appender.default.file", file );
149
150     //
151
// determine level
152
//
153
Level level = null;
154     if ( "ALL".equals( levelParam ) )
155       level = Level.ALL;
156     else if ( "DEBUG".equals( levelParam ) )
157       level = Level.DEBUG;
158     else if ( "ERROR".equals( levelParam ) )
159       level = Level.ERROR;
160     else if ( "FATAL".equals( levelParam ) )
161       level = Level.FATAL;
162     else if ( "INFO".equals( levelParam ) )
163       level = Level.INFO;
164     else if ( "OFF".equals( levelParam ) )
165       level = Level.OFF;
166     else if ( "WARN".equals( levelParam ) )
167       level = Level.WARN;
168     else {
169       getServletContext().log(
170         "LoggerServlet: Unexpected level " + levelParam +
171         "; defaulting to ERROR." );
172       level = Level.ERROR;
173     }
174
175     props.put( "log4j.rootLogger", level.toString() + ",default" );
176
177     //
178
// load config properties
179
//
180
if ( configFileParam != null ) {
181       String JavaDoc configFile = configFileParam;
182       if ( !configFileParam.startsWith( "/" ) ) {
183         configFile = getServletContext().getRealPath( configFileParam );
184
185         if ( configFile == null ) {
186           getServletContext().log(
187             "LoggerServlet: Couldn't get real path for " + configFileParam +
188             "." );
189         }
190       }
191
192       if ( configFile != null ) {
193         try {
194           InputStream JavaDoc in = new FileInputStream JavaDoc( configFile );
195           props.load( in );
196           in.close();
197         }
198         catch ( IOException JavaDoc e ) {
199           getServletContext().log(
200             "LoggerServlet: Unexpected IOException " + e );
201         }
202       }
203     }
204
205     //
206
// configure the logger
207
//
208
PropertyConfigurator.configure( props );
209   }
210
211   public void doGet(
212     HttpServletRequest JavaDoc req,
213     HttpServletResponse JavaDoc res) {
214   }
215
216   // properties ///////////////////////////////////////////////////////////////
217

218   // attributes ///////////////////////////////////////////////////////////////
219
}
220
Popular Tags