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.hbase.http;
019
020import java.util.HashMap;
021import org.apache.commons.logging.LogConfigurationException;
022import org.apache.commons.logging.impl.Log4JLogger;
023import org.apache.log4j.Appender;
024import org.apache.log4j.LogManager;
025import org.apache.yetus.audience.InterfaceAudience;
026import org.eclipse.jetty.server.NCSARequestLog;
027import org.eclipse.jetty.server.RequestLog;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030import org.slf4j.impl.Log4jLoggerAdapter;
031
032/**
033 * RequestLog object for use with Http
034 */
035@InterfaceAudience.Private
036public final class HttpRequestLog {
037
038  private static final Logger LOG = LoggerFactory.getLogger(HttpRequestLog.class);
039  private static final HashMap<String, String> serverToComponent;
040
041  static {
042    serverToComponent = new HashMap<>();
043    serverToComponent.put("master", "master");
044    serverToComponent.put("region", "regionserver");
045  }
046
047  private static org.apache.log4j.Logger getLog4jLogger(String loggerName) {
048    Logger logger = LoggerFactory.getLogger(loggerName);
049
050    if (logger instanceof Log4JLogger) {
051      Log4JLogger httpLog4JLog = (Log4JLogger)logger;
052      return httpLog4JLog.getLogger();
053    } else if (logger instanceof Log4jLoggerAdapter) {
054      return LogManager.getLogger(loggerName);
055    } else {
056      return null;
057    }
058  }
059
060  public static RequestLog getRequestLog(String name) {
061
062    String lookup = serverToComponent.get(name);
063    if (lookup != null) {
064      name = lookup;
065    }
066    String loggerName = "http.requests." + name;
067    String appenderName = name + "requestlog";
068
069    org.apache.log4j.Logger httpLogger = getLog4jLogger(loggerName);
070
071    if (httpLogger == null) {
072      LOG.warn("Jetty request log can only be enabled using Log4j");
073      return null;
074    }
075
076    Appender appender = null;
077
078    try {
079      appender = httpLogger.getAppender(appenderName);
080    } catch (LogConfigurationException e) {
081      LOG.warn("Http request log for " + loggerName
082          + " could not be created");
083      throw e;
084    }
085
086    if (appender == null) {
087      LOG.info("Http request log for " + loggerName
088          + " is not defined");
089      return null;
090    }
091
092    if (appender instanceof HttpRequestLogAppender) {
093      HttpRequestLogAppender requestLogAppender
094        = (HttpRequestLogAppender)appender;
095      NCSARequestLog requestLog = new NCSARequestLog();
096      requestLog.setFilename(requestLogAppender.getFilename());
097      requestLog.setRetainDays(requestLogAppender.getRetainDays());
098      return requestLog;
099    } else {
100      LOG.warn("Jetty request log for " + loggerName
101          + " was of the wrong class");
102      return null;
103    }
104  }
105
106  private HttpRequestLog() {}
107}