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     */
018    package org.apache.hadoop.log;
019    
020    import java.io.*;
021    import java.net.*;
022    import java.util.regex.Pattern;
023    
024    import javax.servlet.*;
025    import javax.servlet.http.*;
026    
027    import org.apache.commons.logging.*;
028    import org.apache.commons.logging.impl.*;
029    import org.apache.hadoop.classification.InterfaceAudience;
030    import org.apache.hadoop.classification.InterfaceStability;
031    import org.apache.hadoop.http.HttpServer;
032    import org.apache.hadoop.util.ServletUtil;
033    
034    /**
035     * Change log level in runtime.
036     */
037    @InterfaceStability.Evolving
038    public class LogLevel {
039      public static final String USAGES = "\nUSAGES:\n"
040        + "java " + LogLevel.class.getName()
041        + " -getlevel <host:port> <name>\n"
042        + "java " + LogLevel.class.getName()
043        + " -setlevel <host:port> <name> <level>\n";
044    
045      /**
046       * A command line implementation
047       */
048      public static void main(String[] args) {
049        if (args.length == 3 && "-getlevel".equals(args[0])) {
050          process("http://" + args[1] + "/logLevel?log=" + args[2]);
051          return;
052        }
053        else if (args.length == 4 && "-setlevel".equals(args[0])) {
054          process("http://" + args[1] + "/logLevel?log=" + args[2]
055                  + "&level=" + args[3]);
056          return;
057        }
058    
059        System.err.println(USAGES);
060        System.exit(-1);
061      }
062    
063      private static void process(String urlstring) {
064        try {
065          URL url = new URL(urlstring);
066          System.out.println("Connecting to " + url);
067          URLConnection connection = url.openConnection();
068          connection.connect();
069    
070          BufferedReader in = new BufferedReader(new InputStreamReader(
071              connection.getInputStream()));
072          for(String line; (line = in.readLine()) != null; )
073            if (line.startsWith(MARKER)) {
074              System.out.println(TAG.matcher(line).replaceAll(""));
075            }
076          in.close();
077        } catch (IOException ioe) {
078          System.err.println("" + ioe);
079        }
080      }
081    
082      static final String MARKER = "<!-- OUTPUT -->";
083      static final Pattern TAG = Pattern.compile("<[^>]*>");
084    
085      /**
086       * A servlet implementation
087       */
088      @InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})
089      @InterfaceStability.Unstable
090      public static class Servlet extends HttpServlet {
091        private static final long serialVersionUID = 1L;
092    
093        public void doGet(HttpServletRequest request, HttpServletResponse response
094            ) throws ServletException, IOException {
095    
096          // Do the authorization
097          if (!HttpServer.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    }