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.HashSet; 023import java.util.Set; 024import org.apache.yetus.audience.InterfaceAudience; 025 026/** 027 * The actual class for operating on log4j2. 028 * <p/> 029 * This class will depend on log4j directly, so callers should not use this class directly to avoid 030 * introducing log4j2 dependencies to downstream users. Please call the methods in 031 * {@link Log4jUtils}, as they will call the methods here through reflection. 032 */ 033@InterfaceAudience.Private 034final class InternalLog4jUtils { 035 036 private InternalLog4jUtils() { 037 } 038 039 private static org.apache.logging.log4j.Level getLevel(String levelName) 040 throws IllegalArgumentException { 041 org.apache.logging.log4j.Level level = 042 org.apache.logging.log4j.Level.toLevel(levelName.toUpperCase()); 043 if (!level.toString().equalsIgnoreCase(levelName)) { 044 throw new IllegalArgumentException("Unsupported log level " + levelName); 045 } 046 return level; 047 } 048 049 static void setAllLevels(String loggerName, String levelName) { 050 org.apache.logging.log4j.core.config.Configurator.setAllLevels(loggerName, getLevel(levelName)); 051 } 052 053 static void setLogLevel(String loggerName, String levelName) { 054 org.apache.logging.log4j.core.config.Configurator.setLevel(loggerName, getLevel(levelName)); 055 } 056 057 static void setRootLevel(String levelName) { 058 String loggerName = org.apache.logging.log4j.LogManager.getRootLogger().getName(); 059 setLogLevel(loggerName, levelName); 060 } 061 062 static String getEffectiveLevel(String loggerName) { 063 org.apache.logging.log4j.Logger logger = 064 org.apache.logging.log4j.LogManager.getLogger(loggerName); 065 return logger.getLevel().name(); 066 } 067 068 static Set<File> getActiveLogFiles() throws IOException { 069 Set<File> ret = new HashSet<>(); 070 org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getRootLogger(); 071 if (!(logger instanceof org.apache.logging.log4j.core.Logger)) { 072 return ret; 073 } 074 org.apache.logging.log4j.core.Logger coreLogger = (org.apache.logging.log4j.core.Logger) logger; 075 for (org.apache.logging.log4j.core.Appender appender : coreLogger.getAppenders().values()) { 076 if (appender instanceof org.apache.logging.log4j.core.appender.FileAppender) { 077 String fileName = 078 ((org.apache.logging.log4j.core.appender.FileAppender) appender).getFileName(); 079 ret.add(new File(fileName)); 080 } else if (appender instanceof org.apache.logging.log4j.core.appender.AbstractFileAppender) { 081 String fileName = 082 ((org.apache.logging.log4j.core.appender.AbstractFileAppender<?>) appender).getFileName(); 083 ret.add(new File(fileName)); 084 } else if (appender instanceof org.apache.logging.log4j.core.appender.RollingFileAppender) { 085 String fileName = 086 ((org.apache.logging.log4j.core.appender.RollingFileAppender) appender).getFileName(); 087 ret.add(new File(fileName)); 088 } else 089 if (appender instanceof org.apache.logging.log4j.core.appender.RandomAccessFileAppender) { 090 String fileName = 091 ((org.apache.logging.log4j.core.appender.RandomAccessFileAppender) appender) 092 .getFileName(); 093 ret.add(new File(fileName)); 094 } else 095 if (appender instanceof org.apache.logging.log4j.core.appender.MemoryMappedFileAppender) { 096 String fileName = 097 ((org.apache.logging.log4j.core.appender.MemoryMappedFileAppender) appender) 098 .getFileName(); 099 ret.add(new File(fileName)); 100 } 101 } 102 return ret; 103 } 104}