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.logging;
019
020import java.io.File;
021import java.io.IOException;
022import java.util.Enumeration;
023import java.util.HashSet;
024import java.util.Set;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * The actual class for operating on log4j.
029 * <p/>
030 * This class will depend on log4j directly, so callers should not use this class directly to avoid
031 * introducing log4j dependencies to downstream users. Please call the methods in
032 * {@link Log4jUtils}, as they will call the methods here through reflection.
033 */
034@InterfaceAudience.Private
035final class InternalLog4jUtils {
036
037  private InternalLog4jUtils() {
038  }
039
040  static void setLogLevel(String loggerName, String levelName) {
041    org.apache.log4j.Logger logger = org.apache.log4j.LogManager.getLogger(loggerName);
042    org.apache.log4j.Level level = org.apache.log4j.Level.toLevel(levelName.toUpperCase());
043    if (!level.toString().equalsIgnoreCase(levelName)) {
044      throw new IllegalArgumentException("Unsupported log level " + levelName);
045    }
046    logger.setLevel(level);
047  }
048
049  static String getEffectiveLevel(String loggerName) {
050    org.apache.log4j.Logger logger = org.apache.log4j.LogManager.getLogger(loggerName);
051    return logger.getEffectiveLevel().toString();
052  }
053
054  static Set<File> getActiveLogFiles() throws IOException {
055    Set<File> ret = new HashSet<>();
056    org.apache.log4j.Appender a;
057    @SuppressWarnings("unchecked")
058    Enumeration<org.apache.log4j.Appender> e =
059      org.apache.log4j.Logger.getRootLogger().getAllAppenders();
060    while (e.hasMoreElements()) {
061      a = e.nextElement();
062      if (a instanceof org.apache.log4j.FileAppender) {
063        org.apache.log4j.FileAppender fa = (org.apache.log4j.FileAppender) a;
064        String filename = fa.getFile();
065        ret.add(new File(filename));
066      }
067    }
068    return ret;
069  }
070}