You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2022/01/11 20:19:34 UTC
[logging-log4j2] 01/03: Use final and better local variable names.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 70dc7ac6e30c3d271966744824abdcdf50f1462d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jan 11 15:04:22 2022 -0500
Use final and better local variable names.
---
.../log4j/config/PropertiesConfiguration.java | 101 ++++++++++-----------
1 file changed, 50 insertions(+), 51 deletions(-)
diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/config/PropertiesConfiguration.java b/log4j-1.2-api/src/main/java/org/apache/log4j/config/PropertiesConfiguration.java
index e927e7d..1ef5ab6 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/config/PropertiesConfiguration.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/config/PropertiesConfiguration.java
@@ -75,22 +75,22 @@ public class PropertiesConfiguration extends Log4j1Configuration {
* @param monitorIntervalSeconds The monitoring interval in seconds.
*/
public PropertiesConfiguration(final LoggerContext loggerContext, final ConfigurationSource source,
- int monitorIntervalSeconds) {
+ final int monitorIntervalSeconds) {
super(loggerContext, source, monitorIntervalSeconds);
}
@Override
public void doConfigure() {
- InputStream is = getConfigurationSource().getInputStream();
- Properties props = new Properties();
+ final InputStream inputStream = getConfigurationSource().getInputStream();
+ final Properties properties = new Properties();
try {
- props.load(is);
- } catch (Exception e) {
+ properties.load(inputStream);
+ } catch (final Exception e) {
LOGGER.error("Could not read configuration file [{}].", getConfigurationSource().toString(), e);
return;
}
// If we reach here, then the config file is alright.
- doConfigure(props);
+ doConfigure(properties);
}
@Override
@@ -101,8 +101,7 @@ public class PropertiesConfiguration extends Log4j1Configuration {
return null;
}
final PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory();
- final PropertiesConfiguration config =
- (PropertiesConfiguration) factory.getConfiguration(getLoggerContext(), source);
+ final PropertiesConfiguration config = (PropertiesConfiguration) factory.getConfiguration(getLoggerContext(), source);
return config == null || config.getState() != State.INITIALIZING ? null : config;
} catch (final IOException ex) {
LOGGER.error("Cannot locate file {}: {}", getConfigurationSource(), ex);
@@ -283,7 +282,7 @@ public class PropertiesConfiguration extends Log4j1Configuration {
*
* <p>
*/
- private void doConfigure(Properties properties) {
+ private void doConfigure(final Properties properties) {
String status = "error";
String value = properties.getProperty(DEBUG_KEY);
if (value == null) {
@@ -310,7 +309,7 @@ public class PropertiesConfiguration extends Log4j1Configuration {
// Internal stuff
// --------------------------------------------------------------------------
- private void configureRoot(Properties props) {
+ private void configureRoot(final Properties props) {
String effectiveFrefix = ROOT_LOGGER_PREFIX;
String value = OptionConverter.findAndSubst(ROOT_LOGGER_PREFIX, props);
@@ -322,7 +321,7 @@ public class PropertiesConfiguration extends Log4j1Configuration {
if (value == null) {
LOGGER.debug("Could not find root logger information. Is this OK?");
} else {
- LoggerConfig root = getRootLogger();
+ final LoggerConfig root = getRootLogger();
parseLogger(props, root, effectiveFrefix, INTERNAL_ROOT_NAME, value);
}
}
@@ -330,10 +329,10 @@ public class PropertiesConfiguration extends Log4j1Configuration {
/**
* Parses non-root elements, such non-root categories and renderers.
*/
- private void parseLoggers(Properties props) {
- Enumeration<?> enumeration = props.propertyNames();
+ private void parseLoggers(final Properties props) {
+ final Enumeration<?> enumeration = props.propertyNames();
while (enumeration.hasMoreElements()) {
- String key = Objects.toString(enumeration.nextElement(), null);
+ final String key = Objects.toString(enumeration.nextElement(), null);
if (key.startsWith(CATEGORY_PREFIX) || key.startsWith(LOGGER_PREFIX)) {
String loggerName = null;
if (key.startsWith(CATEGORY_PREFIX)) {
@@ -341,10 +340,10 @@ public class PropertiesConfiguration extends Log4j1Configuration {
} else if (key.startsWith(LOGGER_PREFIX)) {
loggerName = key.substring(LOGGER_PREFIX.length());
}
- String value = OptionConverter.findAndSubst(key, props);
+ final String value = OptionConverter.findAndSubst(key, props);
LoggerConfig loggerConfig = getLogger(loggerName);
if (loggerConfig == null) {
- boolean additivity = getAdditivityForLogger(props, loggerName);
+ final boolean additivity = getAdditivityForLogger(props, loggerName);
loggerConfig = new LoggerConfig(loggerName, org.apache.logging.log4j.Level.ERROR, additivity);
addLogger(loggerName, loggerConfig);
}
@@ -356,10 +355,10 @@ public class PropertiesConfiguration extends Log4j1Configuration {
/**
* Parses the additivity option for a non-root category.
*/
- private boolean getAdditivityForLogger(Properties props, String loggerName) {
+ private boolean getAdditivityForLogger(final Properties props, final String loggerName) {
boolean additivity = true;
- String key = ADDITIVITY_PREFIX + loggerName;
- String value = OptionConverter.findAndSubst(key, props);
+ final String key = ADDITIVITY_PREFIX + loggerName;
+ final String value = OptionConverter.findAndSubst(key, props);
LOGGER.debug("Handling {}=[{}]", key, value);
// touch additivity only if necessary
if ((value != null) && (!value.equals(""))) {
@@ -371,11 +370,11 @@ public class PropertiesConfiguration extends Log4j1Configuration {
/**
* This method must work for the root category as well.
*/
- private void parseLogger(Properties props, LoggerConfig logger, String optionKey, String loggerName, String value) {
+ private void parseLogger(final Properties props, final LoggerConfig logger, final String optionKey, final String loggerName, final String value) {
LOGGER.debug("Parsing for [{}] with value=[{}].", loggerName, value);
// We must skip over ',' but not white space
- StringTokenizer st = new StringTokenizer(value, ",");
+ final StringTokenizer st = new StringTokenizer(value, ",");
// If value is not in the form ", appender.." or "", then we should set the level of the logger.
@@ -386,10 +385,10 @@ public class PropertiesConfiguration extends Log4j1Configuration {
return;
}
- String levelStr = st.nextToken();
+ final String levelStr = st.nextToken();
LOGGER.debug("Level token is [{}].", levelStr);
- org.apache.logging.log4j.Level level = levelStr == null ? org.apache.logging.log4j.Level.ERROR :
+ final org.apache.logging.log4j.Level level = levelStr == null ? org.apache.logging.log4j.Level.ERROR :
OptionConverter.convertLevel(levelStr, org.apache.logging.log4j.Level.DEBUG);
logger.setLevel(level);
LOGGER.debug("Logger {} level set to {}", loggerName, level);
@@ -414,7 +413,7 @@ public class PropertiesConfiguration extends Log4j1Configuration {
}
}
- public Appender parseAppender(Properties props, String appenderName) {
+ public Appender parseAppender(final Properties props, final String appenderName) {
Appender appender = registry.get(appenderName);
if ((appender != null)) {
LOGGER.debug("Appender \"" + appenderName + "\" was already parsed.");
@@ -424,7 +423,7 @@ public class PropertiesConfiguration extends Log4j1Configuration {
final String prefix = APPENDER_PREFIX + appenderName;
final String layoutPrefix = prefix + ".layout";
final String filterPrefix = APPENDER_PREFIX + appenderName + ".filter.";
- String className = OptionConverter.findAndSubst(prefix, props);
+ final String className = OptionConverter.findAndSubst(prefix, props);
appender = manager.parseAppender(appenderName, className, prefix, layoutPrefix, filterPrefix, props, this);
if (appender == null) {
appender = buildAppender(appenderName, className, prefix, layoutPrefix, filterPrefix, props);
@@ -441,22 +440,22 @@ public class PropertiesConfiguration extends Log4j1Configuration {
private Appender buildAppender(final String appenderName, final String className, final String prefix,
final String layoutPrefix, final String filterPrefix, final Properties props) {
- Appender appender = newInstanceOf(className, "Appender");
+ final Appender appender = newInstanceOf(className, "Appender");
if (appender == null) {
return null;
}
appender.setName(appenderName);
appender.setLayout(parseLayout(layoutPrefix, appenderName, props));
final String errorHandlerPrefix = prefix + ".errorhandler";
- String errorHandlerClass = OptionConverter.findAndSubst(errorHandlerPrefix, props);
+ final String errorHandlerClass = OptionConverter.findAndSubst(errorHandlerPrefix, props);
if (errorHandlerClass != null) {
- ErrorHandler eh = parseErrorHandler(props, errorHandlerPrefix, errorHandlerClass, appender);
+ final ErrorHandler eh = parseErrorHandler(props, errorHandlerPrefix, errorHandlerClass, appender);
if (eh != null) {
appender.setErrorHandler(eh);
}
}
appender.addFilter(parseAppenderFilters(props, filterPrefix, appenderName));
- String[] keys = new String[] { layoutPrefix };
+ final String[] keys = new String[] { layoutPrefix };
addProperties(appender, keys, props, prefix);
if (appender instanceof AppenderWrapper) {
addAppender(((AppenderWrapper) appender).getAppender());
@@ -467,8 +466,8 @@ public class PropertiesConfiguration extends Log4j1Configuration {
return appender;
}
- public Layout parseLayout(String layoutPrefix, String appenderName, Properties props) {
- String layoutClass = OptionConverter.findAndSubst(layoutPrefix, props);
+ public Layout parseLayout(final String layoutPrefix, final String appenderName, final Properties props) {
+ final String layoutClass = OptionConverter.findAndSubst(layoutPrefix, props);
if (layoutClass == null) {
return null;
}
@@ -479,8 +478,8 @@ public class PropertiesConfiguration extends Log4j1Configuration {
return layout;
}
- private Layout buildLayout(String layoutPrefix, String className, String appenderName, Properties props) {
- Layout layout = newInstanceOf(className, "Layout");
+ private Layout buildLayout(final String layoutPrefix, final String className, final String appenderName, final Properties props) {
+ final Layout layout = newInstanceOf(className, "Layout");
if (layout == null) {
return null;
}
@@ -492,7 +491,7 @@ public class PropertiesConfiguration extends Log4j1Configuration {
public ErrorHandler parseErrorHandler(final Properties props, final String errorHandlerPrefix,
final String errorHandlerClass, final Appender appender) {
- ErrorHandler eh = newInstanceOf(errorHandlerClass, "ErrorHandler");
+ final ErrorHandler eh = newInstanceOf(errorHandlerClass, "ErrorHandler");
final String[] keys = new String[] {
errorHandlerPrefix + "." + ROOT_REF,
errorHandlerPrefix + "." + LOGGER_REF,
@@ -506,7 +505,7 @@ public class PropertiesConfiguration extends Log4j1Configuration {
final Properties edited = new Properties();
props.stringPropertyNames().stream().filter(name -> {
if (name.startsWith(prefix)) {
- for (String key : keys) {
+ for (final String key : keys) {
if (name.equals(key)) {
return false;
}
@@ -519,26 +518,26 @@ public class PropertiesConfiguration extends Log4j1Configuration {
}
- public Filter parseAppenderFilters(Properties props, String filterPrefix, String appenderName) {
+ public Filter parseAppenderFilters(final Properties props, final String filterPrefix, final String appenderName) {
// extract filters and filter options from props into a hashtable mapping
// the property name defining the filter class to a list of pre-parsed
// name-value pairs associated to that filter
- int fIdx = filterPrefix.length();
- SortedMap<String, List<NameValue>> filters = new TreeMap<>();
- Enumeration<?> e = props.keys();
+ final int fIdx = filterPrefix.length();
+ final SortedMap<String, List<NameValue>> filters = new TreeMap<>();
+ final Enumeration<?> e = props.keys();
String name = "";
while (e.hasMoreElements()) {
- String key = (String) e.nextElement();
+ final String key = (String) e.nextElement();
if (key.startsWith(filterPrefix)) {
- int dotIdx = key.indexOf('.', fIdx);
+ final int dotIdx = key.indexOf('.', fIdx);
String filterKey = key;
if (dotIdx != -1) {
filterKey = key.substring(0, dotIdx);
name = key.substring(dotIdx + 1);
}
- List<NameValue> filterOpts = filters.computeIfAbsent(filterKey, k -> new ArrayList<>());
+ final List<NameValue> filterOpts = filters.computeIfAbsent(filterKey, k -> new ArrayList<>());
if (dotIdx != -1) {
- String value = OptionConverter.findAndSubst(key, props);
+ final String value = OptionConverter.findAndSubst(key, props);
filterOpts.add(new NameValue(name, value));
}
}
@@ -546,8 +545,8 @@ public class PropertiesConfiguration extends Log4j1Configuration {
Filter head = null;
Filter next = null;
- for (Map.Entry<String, List<NameValue>> entry : filters.entrySet()) {
- String clazz = props.getProperty(entry.getKey());
+ for (final Map.Entry<String, List<NameValue>> entry : filters.entrySet()) {
+ final String clazz = props.getProperty(entry.getKey());
Filter filter = null;
if (clazz != null) {
filter = manager.parseFilter(clazz, entry.getKey(), props, this);
@@ -568,11 +567,11 @@ public class PropertiesConfiguration extends Log4j1Configuration {
return head;
}
- private Filter buildFilter(String className, String appenderName, List<NameValue> props) {
- Filter filter = newInstanceOf(className, "Filter");
+ private Filter buildFilter(final String className, final String appenderName, final List<NameValue> props) {
+ final Filter filter = newInstanceOf(className, "Filter");
if (filter != null) {
- PropertySetter propSetter = new PropertySetter(filter);
- for (NameValue property : props) {
+ final PropertySetter propSetter = new PropertySetter(filter);
+ for (final NameValue property : props) {
propSetter.setProperty(property.key, property.value);
}
propSetter.activate();
@@ -581,7 +580,7 @@ public class PropertiesConfiguration extends Log4j1Configuration {
}
- private static <T> T newInstanceOf(String className, String type) {
+ private static <T> T newInstanceOf(final String className, final String type) {
try {
return LoaderUtil.newInstanceOf(className);
} catch (ClassNotFoundException | IllegalAccessException | NoSuchMethodException |
@@ -595,7 +594,7 @@ public class PropertiesConfiguration extends Log4j1Configuration {
private static class NameValue {
String key, value;
- NameValue(String key, String value) {
+ NameValue(final String key, final String value) {
this.key = key;
this.value = value;
}