View Javadoc

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   */
18  package org.apache.hadoop.hbase.http.log;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.InputStreamReader;
23  import java.io.PrintWriter;
24  import java.net.URL;
25  import java.net.URLConnection;
26  import java.util.regex.Pattern;
27  
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServlet;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.commons.logging.impl.Jdk14Logger;
36  import org.apache.commons.logging.impl.Log4JLogger;
37  import org.apache.hadoop.hbase.classification.InterfaceAudience;
38  import org.apache.hadoop.hbase.classification.InterfaceStability;
39  import org.apache.hadoop.hbase.http.HttpServer;
40  import org.apache.hadoop.util.ServletUtil;
41  
42  /**
43   * Change log level in runtime.
44   */
45  @InterfaceStability.Evolving
46  public class LogLevel {
47    public static final String USAGES = "\nUsage: General options are:\n"
48        + "\t[-getlevel <host:httpPort> <name>]\n"
49        + "\t[-setlevel <host:httpPort> <name> <level>]\n";
50  
51    /**
52     * A command line implementation
53     */
54    public static void main(String[] args) {
55      if (args.length == 3 && "-getlevel".equals(args[0])) {
56        process("http://" + args[1] + "/logLevel?log=" + args[2]);
57        return;
58      }
59      else if (args.length == 4 && "-setlevel".equals(args[0])) {
60        process("http://" + args[1] + "/logLevel?log=" + args[2]
61                + "&level=" + args[3]);
62        return;
63      }
64  
65      System.err.println(USAGES);
66      System.exit(-1);
67    }
68  
69    private static void process(String urlstring) {
70      try {
71        URL url = new URL(urlstring);
72        System.out.println("Connecting to " + url);
73        URLConnection connection = url.openConnection();
74        connection.connect();
75  
76        BufferedReader in = new BufferedReader(new InputStreamReader(
77            connection.getInputStream()));
78        for(String line; (line = in.readLine()) != null; )
79          if (line.startsWith(MARKER)) {
80            System.out.println(TAG.matcher(line).replaceAll(""));
81          }
82        in.close();
83      } catch (IOException ioe) {
84        System.err.println("" + ioe);
85      }
86    }
87  
88    static final String MARKER = "<!-- OUTPUT -->";
89    static final Pattern TAG = Pattern.compile("<[^>]*>");
90  
91    /**
92     * A servlet implementation
93     */
94    @InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})
95    @InterfaceStability.Unstable
96    public static class Servlet extends HttpServlet {
97      private static final long serialVersionUID = 1L;
98  
99      @Override
100     public void doGet(HttpServletRequest request, HttpServletResponse response
101         ) throws ServletException, IOException {
102 
103       // Do the authorization
104       if (!HttpServer.hasAdministratorAccess(getServletContext(), request,
105           response)) {
106         return;
107       }
108 
109       PrintWriter out = ServletUtil.initHTML(response, "Log Level");
110       String logName = ServletUtil.getParameter(request, "log");
111       String level = ServletUtil.getParameter(request, "level");
112 
113       if (logName != null) {
114         out.println("<br /><hr /><h3>Results</h3>");
115         out.println(MARKER
116             + "Submitted Log Name: <b>" + logName + "</b><br />");
117 
118         Log log = LogFactory.getLog(logName);
119         out.println(MARKER
120             + "Log Class: <b>" + log.getClass().getName() +"</b><br />");
121         if (level != null) {
122           out.println(MARKER + "Submitted Level: <b>" + level + "</b><br />");
123         }
124 
125         if (log instanceof Log4JLogger) {
126           process(((Log4JLogger)log).getLogger(), level, out);
127         }
128         else if (log instanceof Jdk14Logger) {
129           process(((Jdk14Logger)log).getLogger(), level, out);
130         }
131         else {
132           out.println("Sorry, " + log.getClass() + " not supported.<br />");
133         }
134       }
135 
136       out.println(FORMS);
137       out.println(ServletUtil.HTML_TAIL);
138     }
139 
140     static final String FORMS = "\n<br /><hr /><h3>Get / Set</h3>"
141         + "\n<form>Log: <input type='text' size='50' name='log' /> "
142         + "<input type='submit' value='Get Log Level' />"
143         + "</form>"
144         + "\n<form>Log: <input type='text' size='50' name='log' /> "
145         + "Level: <input type='text' name='level' /> "
146         + "<input type='submit' value='Set Log Level' />"
147         + "</form>";
148 
149     private static void process(org.apache.log4j.Logger log, String level,
150         PrintWriter out) throws IOException {
151       if (level != null) {
152         if (!level.equals(org.apache.log4j.Level.toLevel(level).toString())) {
153           out.println(MARKER + "Bad level : <b>" + level + "</b><br />");
154         } else {
155           log.setLevel(org.apache.log4j.Level.toLevel(level));
156           out.println(MARKER + "Setting Level to " + level + " ...<br />");
157         }
158       }
159       out.println(MARKER
160           + "Effective level: <b>" + log.getEffectiveLevel() + "</b><br />");
161     }
162 
163     private static void process(java.util.logging.Logger log, String level,
164         PrintWriter out) throws IOException {
165       if (level != null) {
166         log.setLevel(java.util.logging.Level.parse(level));
167         out.println(MARKER + "Setting Level to " + level + " ...<br />");
168       }
169 
170       java.util.logging.Level lev;
171       for(; (lev = log.getLevel()) == null; log = log.getParent());
172       out.println(MARKER + "Effective level: <b>" + lev + "</b><br />");
173     }
174   }
175 }