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      // If user is a static user and auth Type is null, that means
097      // there is a non-security environment and no need authorization,
098      // otherwise, do the authorization.
099      final ServletContext servletContext = getServletContext();
100      if (!HttpServer2.isStaticUserAndNoneAuthType(servletContext, request) &&
101          !HttpServer2.hasAdministratorAccess(servletContext,
102              request, response)) {
103        return;
104      }
105
106      PrintWriter out = ServletUtil.initHTML(response, "Log Level");
107      String logName = ServletUtil.getParameter(request, "log");
108      String level = ServletUtil.getParameter(request, "level");
109
110      if (logName != null) {
111        out.println("<br /><hr /><h3>Results</h3>");
112        out.println(MARKER
113            + "Submitted Log Name: <b>" + logName + "</b><br />");
114
115        Log log = LogFactory.getLog(logName);
116        out.println(MARKER
117            + "Log Class: <b>" + log.getClass().getName() +"</b><br />");
118        if (level != null) {
119          out.println(MARKER + "Submitted Level: <b>" + level + "</b><br />");
120        }
121
122        if (log instanceof Log4JLogger) {
123          process(((Log4JLogger)log).getLogger(), level, out);
124        }
125        else if (log instanceof Jdk14Logger) {
126          process(((Jdk14Logger)log).getLogger(), level, out);
127        }
128        else {
129          out.println("Sorry, " + log.getClass() + " not supported.<br />");
130        }
131      }
132
133      out.println(FORMS);
134      out.println(ServletUtil.HTML_TAIL);
135    }
136
137    static final String FORMS = "\n<br /><hr /><h3>Get / Set</h3>"
138        + "\n<form>Log: <input type='text' size='50' name='log' /> "
139        + "<input type='submit' value='Get Log Level' />"
140        + "</form>"
141        + "\n<form>Log: <input type='text' size='50' name='log' /> "
142        + "Level: <input type='text' name='level' /> "
143        + "<input type='submit' value='Set Log Level' />"
144        + "</form>";
145
146    private static void process(org.apache.log4j.Logger log, String level,
147        PrintWriter out) throws IOException {
148      if (level != null) {
149        if (!level.equalsIgnoreCase(org.apache.log4j.Level.toLevel(level)
150            .toString())) {
151          out.println(MARKER + "Bad Level : <b>" + level + "</b><br />");
152        } else {
153          log.setLevel(org.apache.log4j.Level.toLevel(level));
154          out.println(MARKER + "Setting Level to " + level + " ...<br />");
155        }
156      }
157      out.println(MARKER
158          + "Effective Level: <b>" + log.getEffectiveLevel() + "</b><br />");
159    }
160
161    private static void process(java.util.logging.Logger log, String level,
162        PrintWriter out) throws IOException {
163      if (level != null) {
164        String levelToUpperCase = level.toUpperCase();
165        try {
166          log.setLevel(java.util.logging.Level.parse(levelToUpperCase));
167        } catch (IllegalArgumentException e) {
168          out.println(MARKER + "Bad Level : <b>" + level + "</b><br />");
169        }
170        out.println(MARKER + "Setting Level to " + level + " ...<br />");
171      }
172
173      java.util.logging.Level lev;
174      for(; (lev = log.getLevel()) == null; log = log.getParent());
175      out.println(MARKER + "Effective Level: <b>" + lev + "</b><br />");
176    }
177  }
178}