001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.servlet;
022:
023: import java.io.FileInputStream;
024: import java.io.InputStream;
025: import java.io.IOException;
026:
027: import java.util.Properties;
028:
029: import javax.servlet.ServletContext;
030:
031: import javax.servlet.http.HttpServlet;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: import org.apache.log4j.BasicConfigurator;
036: import org.apache.log4j.Level;
037: import org.apache.log4j.PropertyConfigurator;
038:
039: /**
040: * <p>
041: * A servlet to initialize the <a
042: * href="http://jakarta.apache.org/log4j/docs/index.html">Log4j</a> logger.
043: * This servlet sets up a very simple configuration and accepts a few
044: * optional parameters. Three init-params may be specified:
045: * </p>
046: * <ul>
047: * <li>
048: * <strong>logFile</strong>: the path of the log file. If the path begins
049: * with a forward slash it is assumed to be an absolute path, otherwise it
050: * is assumed to be a path relative to the context's root. For example:
051: * <tt>WEB-INF/log.txt</tt> or <tt>/var/log/log.txt</tt>. If this
052: * parameter is not specified, it defaults to <tt>WEB-INF/log.txt</tt>
053: * </li>
054: * <li>
055: * <strong>level</strong>: the log level. Legal values are <tt>ALL</tt>,
056: * <tt>DEBUG</tt>, <tt>ERROR</tt>, <tt>FATAL</tt>, <tt>INFO</tt>,
057: * <tt>OFF</tt>, <tt>WARN</tt>. Note that these level correspond to the
058: * log levels defined by Log4j. If this parameter is not specified, it
059: * defaults to <tt>ERROR</tt>.
060: * </li>
061: * <li>
062: * <strong>configFile</strong>: the path of a properties file containing
063: * Log4j configuration information. Like <tt>logFile</tt>, if the path
064: * begins with a forward slash it is assumed to be an absolute path,
065: * otherwise it is assumed to be a path relative to the context's root.
066: * This file should follow the conventions described in the Log4j
067: * documentation.
068: * </li>
069: * </ul>
070: * <p>
071: * If no init-params are specified, the following configuration is created:
072: * </p>
073: * <pre>
074: * log4j.appender.default=org.apache.log4j.FileAppender
075: * log4j.appender.default.file=WEB-INF/log.txt # logFile
076: * log4j.appender.default.layout=org.apache.log4j.PatternLayout
077: * log4j.appender.default.layout.ConversionPattern=org.apache.log4j.PatternLayout=%d %-5p %c - %m%n
078: * log4j.rootLogger=ERROR,default # level</pre>
079: * <p>
080: * Specifying <tt>logFile</tt> and <tt>level</tt> overrides the properties
081: * indicated above. If <tt>configFile</tt> is specified, its properties will
082: * be combined with those above, overriding any conflicting properties.
083: * </p>
084: * <p>
085: * So, as an example, if you wanted to enable DEBUG logging for this class
086: * only and you wanted to override the output format, you could add the
087: * following lines to your <tt>configFile</tt>:
088: * </p>
089: * <pre>
090: * log4j.rootLogger=OFF
091: * log4j.logger.com.methodhead.servlet.LoggerServlet=DEBUG
092: * log4j.appender.default.layout=org.apache.log4j.PatternLayout
093: * log4j.appender.default.layout.ConversionPattern=%d [%t] %-5p %c - %m%n</pre>
094: */
095: public class LoggerServlet extends HttpServlet {
096:
097: // constructors /////////////////////////////////////////////////////////////
098:
099: // constants ////////////////////////////////////////////////////////////////
100:
101: // classes //////////////////////////////////////////////////////////////////
102:
103: // methods //////////////////////////////////////////////////////////////////
104:
105: public void init() {
106: //
107: // get init-params
108: //
109: String configFileParam = getInitParameter("configFile");
110: String logFileParam = getInitParameter("logFile");
111: String levelParam = getInitParameter("level");
112:
113: //
114: // reset configuration
115: //
116: BasicConfigurator.resetConfiguration();
117:
118: Properties props = new Properties();
119: props.put("log4j.appender.default",
120: "org.apache.log4j.FileAppender");
121: props.put("log4j.appender.default.layout",
122: "org.apache.log4j.PatternLayout");
123: props.put("log4j.appender.default.layout.ConversionPattern",
124: "%d %-5p %c - %m%n");
125:
126: //
127: // determine log file
128: //
129: String file = logFileParam;
130:
131: if (logFileParam == null)
132: file = getServletContext().getRealPath("WEB-INF/log.txt");
133:
134: else if (!logFileParam.startsWith("/")) {
135: file = getServletContext().getRealPath(logFileParam);
136:
137: if (file == null) {
138: getServletContext().log(
139: "LoggerServlet: Couldn't get real path for "
140: + logFileParam
141: + "; defaulting to WEB-INF/log.txt.");
142: file = getServletContext().getRealPath(
143: "WEB-INF/log.txt");
144: }
145: }
146:
147: props.put("log4j.appender.default.file", file);
148:
149: //
150: // determine level
151: //
152: Level level = null;
153: if ("ALL".equals(levelParam))
154: level = Level.ALL;
155: else if ("DEBUG".equals(levelParam))
156: level = Level.DEBUG;
157: else if ("ERROR".equals(levelParam))
158: level = Level.ERROR;
159: else if ("FATAL".equals(levelParam))
160: level = Level.FATAL;
161: else if ("INFO".equals(levelParam))
162: level = Level.INFO;
163: else if ("OFF".equals(levelParam))
164: level = Level.OFF;
165: else if ("WARN".equals(levelParam))
166: level = Level.WARN;
167: else {
168: getServletContext().log(
169: "LoggerServlet: Unexpected level " + levelParam
170: + "; defaulting to ERROR.");
171: level = Level.ERROR;
172: }
173:
174: props.put("log4j.rootLogger", level.toString() + ",default");
175:
176: //
177: // load config properties
178: //
179: if (configFileParam != null) {
180: String configFile = configFileParam;
181: if (!configFileParam.startsWith("/")) {
182: configFile = getServletContext().getRealPath(
183: configFileParam);
184:
185: if (configFile == null) {
186: getServletContext().log(
187: "LoggerServlet: Couldn't get real path for "
188: + configFileParam + ".");
189: }
190: }
191:
192: if (configFile != null) {
193: try {
194: InputStream in = new FileInputStream(configFile);
195: props.load(in);
196: in.close();
197: } catch (IOException e) {
198: getServletContext().log(
199: "LoggerServlet: Unexpected IOException "
200: + e);
201: }
202: }
203: }
204:
205: //
206: // configure the logger
207: //
208: PropertyConfigurator.configure(props);
209: }
210:
211: public void doGet(HttpServletRequest req, HttpServletResponse res) {
212: }
213:
214: // properties ///////////////////////////////////////////////////////////////
215:
216: // attributes ///////////////////////////////////////////////////////////////
217: }
|