source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/log/LogLevel.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 5.1 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.apache.hadoop.log;
19
20import java.io.*;
21import java.net.*;
22import java.util.regex.Pattern;
23
24import javax.servlet.*;
25import javax.servlet.http.*;
26
27import org.apache.commons.logging.*;
28import org.apache.commons.logging.impl.*;
29import org.apache.hadoop.util.ServletUtil;
30
31/**
32 * Change log level in runtime.
33 */
34public class LogLevel {
35  public static final String USAGES = "\nUSAGES:\n"
36    + "java " + LogLevel.class.getName()
37    + " -getlevel <host:port> <name>\n"
38    + "java " + LogLevel.class.getName()
39    + " -setlevel <host:port> <name> <level>\n";
40
41  /**
42   * A command line implementation
43   */
44  public static void main(String[] args) {
45    if (args.length == 3 && "-getlevel".equals(args[0])) {
46      process("http://" + args[1] + "/logLevel?log=" + args[2]);
47      return;
48    }
49    else if (args.length == 4 && "-setlevel".equals(args[0])) {
50      process("http://" + args[1] + "/logLevel?log=" + args[2]
51              + "&level=" + args[3]);
52      return;
53    }
54
55    System.err.println(USAGES);
56    System.exit(-1);
57  }
58
59  private static void process(String urlstring) {
60    try {
61      URL url = new URL(urlstring);
62      System.out.println("Connecting to " + url);
63      URLConnection connection = url.openConnection();
64      connection.connect();
65
66      BufferedReader in = new BufferedReader(new InputStreamReader(
67          connection.getInputStream()));
68      for(String line; (line = in.readLine()) != null; )
69        if (line.startsWith(MARKER)) {
70          System.out.println(TAG.matcher(line).replaceAll(""));
71        }
72      in.close();
73    } catch (IOException ioe) {
74      System.err.println("" + ioe);
75    }
76  }
77
78  static final String MARKER = "<!-- OUTPUT -->";
79  static final Pattern TAG = Pattern.compile("<[^>]*>");
80
81  /**
82   * A servlet implementation
83   */
84  public static class Servlet extends HttpServlet {
85    private static final long serialVersionUID = 1L;
86
87    public void doGet(HttpServletRequest request, HttpServletResponse response
88        ) throws ServletException, IOException {
89      PrintWriter out = ServletUtil.initHTML(response, "Log Level");
90      String logName = ServletUtil.getParameter(request, "log");
91      String level = ServletUtil.getParameter(request, "level");
92
93      if (logName != null) {
94        out.println("<br /><hr /><h3>Results</h3>");
95        out.println(MARKER
96            + "Submitted Log Name: <b>" + logName + "</b><br />");
97
98        Log log = LogFactory.getLog(logName);
99        out.println(MARKER
100            + "Log Class: <b>" + log.getClass().getName() +"</b><br />");
101        if (level != null) {
102          out.println(MARKER + "Submitted Level: <b>" + level + "</b><br />");
103        }
104
105        if (log instanceof Log4JLogger) {
106          process(((Log4JLogger)log).getLogger(), level, out);
107        }
108        else if (log instanceof Jdk14Logger) {
109          process(((Jdk14Logger)log).getLogger(), level, out);
110        }
111        else {
112          out.println("Sorry, " + log.getClass() + " not supported.<br />");
113        }
114      }
115
116      out.println(FORMS);
117      out.println(ServletUtil.HTML_TAIL);
118    }
119
120    static final String FORMS = "\n<br /><hr /><h3>Get / Set</h3>"
121        + "\n<form>Log: <input type='text' size='50' name='log' /> "
122        + "<input type='submit' value='Get Log Level' />"
123        + "</form>"
124        + "\n<form>Log: <input type='text' size='50' name='log' /> "
125        + "Level: <input type='text' name='level' /> "
126        + "<input type='submit' value='Set Log Level' />"
127        + "</form>";
128
129    private static void process(org.apache.log4j.Logger log, String level,
130        PrintWriter out) throws IOException {
131      if (level != null) {
132        log.setLevel(org.apache.log4j.Level.toLevel(level));
133        out.println(MARKER + "Setting Level to " + level + " ...<br />");
134      }
135      out.println(MARKER
136          + "Effective level: <b>" + log.getEffectiveLevel() + "</b><br />");
137    }
138
139    private static void process(java.util.logging.Logger log, String level,
140        PrintWriter out) throws IOException {
141      if (level != null) {
142        log.setLevel(java.util.logging.Level.parse(level));
143        out.println(MARKER + "Setting Level to " + level + " ...<br />");
144      }
145
146      java.util.logging.Level lev;
147      for(; (lev = log.getLevel()) == null; log = log.getParent());
148      out.println(MARKER + "Effective level: <b>" + lev + "</b><br />");
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.