001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.log;
019
020import java.io.*;
021import java.net.*;
022import java.util.regex.Pattern;
023
024import javax.servlet.*;
025import javax.servlet.http.*;
026
027import com.google.common.base.Charsets;
028import org.apache.commons.logging.*;
029import org.apache.commons.logging.impl.*;
030import org.apache.hadoop.classification.InterfaceAudience;
031import org.apache.hadoop.classification.InterfaceStability;
032import org.apache.hadoop.http.HttpServer2;
033import org.apache.hadoop.util.ServletUtil;
034
035/**
036 * Change log level in runtime.
037 */
038@InterfaceStability.Evolving
039public class LogLevel {
040  public static final String USAGES = "\nUsage: General options are:\n"
041      + "\t[-getlevel <host:httpPort> <name>]\n"
042      + "\t[-setlevel <host:httpPort> <name> <level>]\n";
043
044  /**
045   * A command line implementation
046   */
047  public static void main(String[] args) {
048    if (args.length == 3 && "-getlevel".equals(args[0])) {
049      process("http://" + args[1] + "/logLevel?log=" + args[2]);
050      return;
051    }
052    else if (args.length == 4 && "-setlevel".equals(args[0])) {
053      process("http://" + args[1] + "/logLevel?log=" + args[2]
054              + "&level=" + args[3]);
055      return;
056    }
057
058    System.err.println(USAGES);
059    System.exit(-1);
060  }
061
062  private static void process(String urlstring) {
063    try {
064      URL url = new URL(urlstring);
065      System.out.println("Connecting to " + url);
066      URLConnection connection = url.openConnection();
067      connection.connect();
068
069      BufferedReader in = new BufferedReader(new InputStreamReader(
070          connection.getInputStream(), Charsets.UTF_8));
071      for(String line; (line = in.readLine()) != null; )
072        if (line.startsWith(MARKER)) {
073          System.out.println(TAG.matcher(line).replaceAll(""));
074        }
075      in.close();
076    } catch (IOException ioe) {
077      System.err.println("" + ioe);
078    }
079  }
080
081  static final String MARKER = "<!-- OUTPUT -->";
082  static final Pattern TAG = Pattern.compile("<[^>]*>");
083
084  /**
085   * A servlet implementation
086   */
087  @InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})
088  @InterfaceStability.Unstable
089  public static class Servlet extends HttpServlet {
090    private static final long serialVersionUID = 1L;
091
092    @Override
093    public void doGet(HttpServletRequest request, HttpServletResponse response
094        ) throws ServletException, IOException {
095
096      // Do the authorization
097      if (!HttpServer2.hasAdministratorAccess(getServletContext(), request,
098          response)) {
099        return;
100      }
101
102      PrintWriter out = ServletUtil.initHTML(response, "Log Level");
103      String logName = ServletUtil.getParameter(request, "log");
104      String level = ServletUtil.getParameter(request, "level");
105
106      if (logName != null) {
107        out.println("<br /><hr /><h3>Results</h3>");
108        out.println(MARKER
109            + "Submitted Log Name: <b>" + logName + "</b><br />");
110
111        Log log = LogFactory.getLog(logName);
112        out.println(MARKER
113            + "Log Class: <b>" + log.getClass().getName() +"</b><br />");
114        if (level != null) {
115          out.println(MARKER + "Submitted Level: <b>" + level + "</b><br />");
116        }
117
118        if (log instanceof Log4JLogger) {
119          process(((Log4JLogger)log).getLogger(), level, out);
120        }
121        else if (log instanceof Jdk14Logger) {
122          process(((Jdk14Logger)log).getLogger(), level, out);
123        }
124        else {
125          out.println("Sorry, " + log.getClass() + " not supported.<br />");
126        }
127      }
128
129      out.println(FORMS);
130      out.println(ServletUtil.HTML_TAIL);
131    }
132
133    static final String FORMS = "\n<br /><hr /><h3>Get / Set</h3>"
134        + "\n<form>Log: <input type='text' size='50' name='log' /> "
135        + "<input type='submit' value='Get Log Level' />"
136        + "</form>"
137        + "\n<form>Log: <input type='text' size='50' name='log' /> "
138        + "Level: <input type='text' name='level' /> "
139        + "<input type='submit' value='Set Log Level' />"
140        + "</form>";
141
142    private static void process(org.apache.log4j.Logger log, String level,
143        PrintWriter out) throws IOException {
144      if (level != null) {
145        if (!level.equals(org.apache.log4j.Level.toLevel(level).toString())) {
146          out.println(MARKER + "Bad level : <b>" + level + "</b><br />");
147        } else {
148          log.setLevel(org.apache.log4j.Level.toLevel(level));
149          out.println(MARKER + "Setting Level to " + level + " ...<br />");
150        }
151      }
152      out.println(MARKER
153          + "Effective level: <b>" + log.getEffectiveLevel() + "</b><br />");
154    }
155
156    private static void process(java.util.logging.Logger log, String level,
157        PrintWriter out) throws IOException {
158      if (level != null) {
159        log.setLevel(java.util.logging.Level.parse(level));
160        out.println(MARKER + "Setting Level to " + level + " ...<br />");
161      }
162
163      java.util.logging.Level lev;
164      for(; (lev = log.getLevel()) == null; log = log.getParent());
165      out.println(MARKER + "Effective level: <b>" + lev + "</b><br />");
166    }
167  }
168}