001: /*
002: * <copyright>
003: *
004: * Copyright 2002-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.logging;
028:
029: import java.io.IOException;
030: import java.io.PrintWriter;
031:
032: import javax.servlet.Servlet;
033: import javax.servlet.http.HttpServlet;
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036:
037: //import org.apache.log4j.Logger;
038: import org.apache.log4j.Level;
039: import org.cougaar.core.servlet.BaseServletComponent;
040: import org.cougaar.util.log.Logger;
041: import org.cougaar.util.log.log4j.DetailPriority;
042: import org.cougaar.util.log.log4j.ShoutPriority;
043:
044: /**
045: * This component is a {@link Servlet} that allows clients to view
046: * and modify the {@link org.cougaar.core.service.LoggingService}
047: * configuration.
048: */
049: public class LoggingConfigServlet extends BaseServletComponent {
050:
051: protected String getPath() {
052: return "/log";
053: }
054:
055: protected Servlet createServlet() {
056: return new MyServlet();
057: }
058:
059: // servlet:
060:
061: private class MyServlet extends HttpServlet {
062:
063: public void doGet(HttpServletRequest req,
064: HttpServletResponse res) throws IOException {
065:
066: String action = req.getParameter("action");
067: String getlog = req.getParameter("getlog");
068: String setlog = req.getParameter("setlog");
069: String setlevel = req.getParameter("level");
070: if (getlog != null)
071: getlog = getlog.trim();
072: if (setlog != null)
073: setlog = setlog.trim();
074: if (setlevel != null)
075: setlevel = setlevel.trim();
076: if ("".equals(getlog))
077: getlog = null;
078: if ("".equals(setlog))
079: setlog = null;
080: int isetlevel = (setlevel != null ? convertStringToInt(setlevel)
081: : -1);
082:
083: res.setContentType("text/html");
084: PrintWriter out = res.getWriter();
085:
086: out.print("<html><head><title>" + "Logger Configuration"
087: + "</title></head><body>\n"
088: + "<h2>Logger Configuration</h2><p>");
089:
090: if (getlog != null && "Get".equalsIgnoreCase(action)) {
091: // get the level
092: Exception e = null;
093: int l = -1;
094: try {
095: l = getLevel(getlog);
096: } catch (Exception e2) {
097: e = e2;
098: }
099: if (l >= 0 && e == null) {
100: out.print("Level for \"" + getlog + "\" is "
101: + convertIntToString(l));
102: } else {
103: out.print("Unable to get logging level of \""
104: + getlog + "\": ");
105: if (e == null) {
106: out.print("invalid name");
107: } else {
108: e.printStackTrace(out);
109: }
110: }
111: } else if (setlog != null && setlevel != null
112: && "Set".equalsIgnoreCase(action)) {
113: // set the level
114: Exception e = null;
115: try {
116: setLevel(setlog, isetlevel);
117: } catch (Exception e2) {
118: e = e2;
119: }
120: if (e == null) {
121: out.print("Set \"" + setlog + "\" to "
122: + convertIntToString(isetlevel));
123: } else {
124: out.print("Unable to change logging level of \""
125: + setlog + "\": ");
126: e.printStackTrace(out);
127: }
128: // } else {
129: // // Neither get or set invoked. Print a Usage message?
130: }
131:
132: out
133: .print("<br><hr>\n"
134: + "<form method=\"GET\" action=\""
135: + req.getRequestURI()
136: + "\">\n"
137: + "<input type=\"RESET\" name=\"Clear\" value=\"Clear\">"
138: + "<p>\n"
139: + "Get the level for "
140: + "<input type=\"text\" name=\"getlog\" size=\"50\""
141: + (getlog != null ? (" value=\"" + getlog + "\"")
142: : "")
143: + "/>"
144: + "<input type=\"submit\" name=\"action\" value=\"Get\">"
145: + "<p>\n"
146: + "Set the level for "
147: + "<input type=\"text\" name=\"setlog\" size=\"50\""
148: + (setlog != null ? (" value=\"" + setlog + "\"")
149: : "")
150: + "/> to "
151: + "<select name=\"level\">"
152: + "<option"
153: + (isetlevel == Logger.DETAIL ? " selected"
154: : "")
155: + ">DETAIL</option>"
156: + "<option"
157: + (isetlevel == Logger.DEBUG ? " selected"
158: : "")
159: + ">DEBUG</option>"
160: + "<option "
161: + (isetlevel == Logger.INFO ? " selected"
162: : "")
163: + ">INFO</option>"
164: + "<option"
165: + (isetlevel == Logger.WARN ? " selected"
166: : "")
167: + ">WARN</option>"
168: + "<option"
169: + (isetlevel == Logger.ERROR ? " selected"
170: : "")
171: + ">ERROR</option>"
172: + "<option"
173: + (isetlevel == Logger.SHOUT ? " selected"
174: : "")
175: + ">SHOUT</option>"
176: + "<option"
177: + (isetlevel == Logger.FATAL ? " selected"
178: : "")
179: + ">FATAL</option>"
180: + "</select>"
181: + "<input type=\"submit\" name=\"action\" value=\"Set\">\n"
182: + "</form></body></html>\n");
183: out.close();
184: }
185: }
186:
187: // logger utilities:
188:
189: private String convertIntToString(int level) {
190: switch (level) {
191: case Logger.DETAIL:
192: return "DETAIL";
193: case Logger.DEBUG:
194: return "DEBUG";
195: case Logger.INFO:
196: return "INFO";
197: case Logger.WARN:
198: return "WARN";
199: case Logger.ERROR:
200: return "ERROR";
201: case Logger.SHOUT:
202: return "SHOUT";
203: case Logger.FATAL:
204: return "FATAL";
205: default:
206: return null;
207: }
208: }
209:
210: private int convertStringToInt(String s) {
211: if (s == null) {
212: return -1;
213: } else if (s.equalsIgnoreCase("DETAIL")) {
214: return Logger.DETAIL;
215: } else if (s.equalsIgnoreCase("DEBUG")) {
216: return Logger.DEBUG;
217: } else if (s.equalsIgnoreCase("INFO")) {
218: return Logger.INFO;
219: } else if (s.equalsIgnoreCase("WARN")) {
220: return Logger.WARN;
221: } else if (s.equalsIgnoreCase("ERROR")) {
222: return Logger.ERROR;
223: } else if (s.equalsIgnoreCase("SHOUT")) {
224: return Logger.SHOUT;
225: } else if (s.equalsIgnoreCase("FATAL")) {
226: return Logger.FATAL;
227: } else {
228: return -1;
229: }
230: }
231:
232: // log4j utilities
233: //
234: // these should be moved to "org.cougaar.util.log"!
235:
236: // okay public api:
237: private int getLevel(String name) {
238: org.apache.log4j.Logger cat = getLogger(name);
239: return getLevel(cat);
240: }
241:
242: // okay public api:
243: private void setLevel(String name, int level) {
244: org.apache.log4j.Logger cat = getLogger(name);
245: setLevel(cat, level);
246: }
247:
248: // hack:
249: static final Level SHOUT = ShoutPriority.toLevel("SHOUT", null);
250:
251: // hack:
252: static final Level DETAIL = DetailPriority.toLevel("DETAIL", null);
253:
254: // log4j private
255: private org.apache.log4j.Logger getLogger(String name) {
256: return (name != null ? ((name.equals("root") || name.equals("")) ? org.apache.log4j.Logger
257: .getRootLogger()
258: : org.apache.log4j.Logger.getLogger(name))
259: : null);
260: }
261:
262: // log4j private
263: private int getLevel(org.apache.log4j.Logger cat) {
264: if (cat != null) {
265: Level p = cat.getEffectiveLevel();
266: return convertLevelToInt(p);
267: } else {
268: return -1;
269: }
270: }
271:
272: // log4j private
273: private void setLevel(org.apache.log4j.Logger cat, int level) {
274: if (cat != null) {
275: Level p = convertIntToLevel(level);
276: cat.setLevel(p);
277: } else {
278: throw new RuntimeException("null category");
279: }
280: }
281:
282: // log4j private
283: private Level convertIntToLevel(int level) {
284: switch (level) {
285: case Logger.DETAIL:
286: return DETAIL;
287: case Logger.DEBUG:
288: return Level.DEBUG;
289: case Logger.INFO:
290: return Level.INFO;
291: case Logger.WARN:
292: return Level.WARN;
293: case Logger.ERROR:
294: return Level.ERROR;
295: case Logger.SHOUT:
296: return SHOUT;
297: case Logger.FATAL:
298: return Level.FATAL;
299: default:
300: return null;
301: }
302: }
303:
304: // log4j private
305: private int convertLevelToInt(Level level) {
306: switch (level.toInt()) {
307: case Level.DEBUG_INT:
308: return Logger.DEBUG;
309: case Level.INFO_INT:
310: return Logger.INFO;
311: case Level.WARN_INT:
312: return Logger.WARN;
313: case Level.ERROR_INT:
314: return Logger.ERROR;
315: case Level.FATAL_INT:
316: return Logger.FATAL;
317: default:
318: if (level.equals(SHOUT)) {
319: return Logger.SHOUT;
320: } else if (level.equals(DETAIL)) {
321: return Logger.DETAIL;
322: }
323: return 0;
324: }
325: }
326:
327: }
|