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 static void setLogLevel(String loggerName, String levelName) { 040 org.apache.logging.log4j.Level level = 041 org.apache.logging.log4j.Level.toLevel(levelName.toUpperCase()); 042 if (!level.toString().equalsIgnoreCase(levelName)) { 043 throw new IllegalArgumentException("Unsupported log level " + levelName); 044 } 045 org.apache.logging.log4j.core.config.Configurator.setLevel(loggerName, level); 046 } 047 048 static String getEffectiveLevel(String loggerName) { 049 org.apache.logging.log4j.Logger logger = 050 org.apache.logging.log4j.LogManager.getLogger(loggerName); 051 return logger.getLevel().name(); 052 } 053 054 static Set<File> getActiveLogFiles() throws IOException { 055 Set<File> ret = new HashSet<>(); 056 org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getRootLogger(); 057 if (!(logger instanceof org.apache.logging.log4j.core.Logger)) { 058 return ret; 059 } 060 org.apache.logging.log4j.core.Logger coreLogger = (org.apache.logging.log4j.core.Logger) logger; 061 for (org.apache.logging.log4j.core.Appender appender : coreLogger.getAppenders().values()) { 062 if (appender instanceof org.apache.logging.log4j.core.appender.FileAppender) { 063 String fileName = 064 ((org.apache.logging.log4j.core.appender.FileAppender) appender).getFileName(); 065 ret.add(new File(fileName)); 066 } else if (appender instanceof org.apache.logging.log4j.core.appender.AbstractFileAppender) { 067 String fileName = 068 ((org.apache.logging.log4j.core.appender.AbstractFileAppender<?>) appender).getFileName(); 069 ret.add(new File(fileName)); 070 } else if (appender instanceof org.apache.logging.log4j.core.appender.RollingFileAppender) { 071 String fileName = 072 ((org.apache.logging.log4j.core.appender.RollingFileAppender) appender).getFileName(); 073 ret.add(new File(fileName)); 074 } else 075 if (appender instanceof org.apache.logging.log4j.core.appender.RandomAccessFileAppender) { 076 String fileName = 077 ((org.apache.logging.log4j.core.appender.RandomAccessFileAppender) appender) 078 .getFileName(); 079 ret.add(new File(fileName)); 080 } else 081 if (appender instanceof org.apache.logging.log4j.core.appender.MemoryMappedFileAppender) { 082 String fileName = 083 ((org.apache.logging.log4j.core.appender.MemoryMappedFileAppender) appender) 084 .getFileName(); 085 ret.add(new File(fileName)); 086 } 087 } 088 return ret; 089 } 090}