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;
19  
20  import java.util.HashMap;
21  
22  import org.apache.commons.logging.impl.Log4JLogger;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogConfigurationException;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.log4j.Appender;
27  import org.apache.log4j.Logger;
28  import org.mortbay.jetty.NCSARequestLog;
29  import org.mortbay.jetty.RequestLog;
30  
31  /**
32   * RequestLog object for use with Http
33   */
34  public class HttpRequestLog {
35  
36    private static final Log LOG = LogFactory.getLog(HttpRequestLog.class);
37    private static final HashMap<String, String> serverToComponent;
38  
39    static {
40      serverToComponent = new HashMap<String, String>();
41      serverToComponent.put("master", "master");
42      serverToComponent.put("region", "regionserver");
43    }
44  
45    public static RequestLog getRequestLog(String name) {
46  
47      String lookup = serverToComponent.get(name);
48      if (lookup != null) {
49        name = lookup;
50      }
51      String loggerName = "http.requests." + name;
52      String appenderName = name + "requestlog";
53      Log logger = LogFactory.getLog(loggerName);
54  
55      if (logger instanceof Log4JLogger) {
56        Log4JLogger httpLog4JLog = (Log4JLogger)logger;
57        Logger httpLogger = httpLog4JLog.getLogger();
58        Appender appender = null;
59  
60        try {
61          appender = httpLogger.getAppender(appenderName);
62        } catch (LogConfigurationException e) {
63          LOG.warn("Http request log for " + loggerName
64              + " could not be created");
65          throw e;
66        }
67  
68        if (appender == null) {
69          LOG.info("Http request log for " + loggerName
70              + " is not defined");
71          return null;
72        }
73  
74        if (appender instanceof HttpRequestLogAppender) {
75          HttpRequestLogAppender requestLogAppender
76            = (HttpRequestLogAppender)appender;
77          NCSARequestLog requestLog = new NCSARequestLog();
78          requestLog.setFilename(requestLogAppender.getFilename());
79          requestLog.setRetainDays(requestLogAppender.getRetainDays());
80          return requestLog;
81        } else {
82          LOG.warn("Jetty request log for " + loggerName
83              + " was of the wrong class");
84          return null;
85        }
86      }
87      else {
88        LOG.warn("Jetty request log can only be enabled using Log4j");
89        return null;
90      }
91    }
92  }