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/10 21:31:55 UTC
[logging-log4j2] 01/03: Sort members.
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 0665d35f9fe6cb66469eaf6eef4f1780f91535fb
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jan 10 16:29:22 2022 -0500
Sort members.
---
.../org/apache/log4j/helpers/OptionConverter.java | 510 ++++++++++-----------
1 file changed, 255 insertions(+), 255 deletions(-)
diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/helpers/OptionConverter.java b/log4j-1.2-api/src/main/java/org/apache/log4j/helpers/OptionConverter.java
index 62f1f69..b55a015 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/helpers/OptionConverter.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/helpers/OptionConverter.java
@@ -41,11 +41,21 @@ import java.util.Properties;
*/
public class OptionConverter {
+ private static class CharMap {
+ final char key;
+ final char replacement;
+
+ public CharMap(char key, char replacement) {
+ this.key = key;
+ this.replacement = replacement;
+ }
+ }
static String DELIM_START = "${";
static char DELIM_STOP = '}';
static int DELIM_START_LEN = 2;
static int DELIM_STOP_LEN = 1;
private static final Logger LOGGER = LogManager.getLogger(OptionConverter.class);
+
private static final CharMap[] charMap = new CharMap[] {
new CharMap('n', '\n'),
new CharMap('r', '\r'),
@@ -57,12 +67,6 @@ public class OptionConverter {
new CharMap('\\', '\\')
};
- /**
- * OptionConverter is a static class.
- */
- private OptionConverter() {
- }
-
public static String[] concatanateArrays(String[] l, String[] r) {
int len = l.length + r.length;
String[] a = new String[len];
@@ -73,6 +77,57 @@ public class OptionConverter {
return a;
}
+ public static org.apache.logging.log4j.Level convertLevel(Level level) {
+ if (level == null) {
+ return org.apache.logging.log4j.Level.ERROR;
+ }
+ if (level.isGreaterOrEqual(Level.FATAL)) {
+ return org.apache.logging.log4j.Level.FATAL;
+ } else if (level.isGreaterOrEqual(Level.ERROR)) {
+ return org.apache.logging.log4j.Level.ERROR;
+ } else if (level.isGreaterOrEqual(Level.WARN)) {
+ return org.apache.logging.log4j.Level.WARN;
+ } else if (level.isGreaterOrEqual(Level.INFO)) {
+ return org.apache.logging.log4j.Level.INFO;
+ } else if (level.isGreaterOrEqual(Level.DEBUG)) {
+ return org.apache.logging.log4j.Level.DEBUG;
+ } else if (level.isGreaterOrEqual(Level.TRACE)) {
+ return org.apache.logging.log4j.Level.TRACE;
+ }
+ return org.apache.logging.log4j.Level.ALL;
+ }
+
+
+ public static Level convertLevel(org.apache.logging.log4j.Level level) {
+ if (level == null) {
+ return Level.ERROR;
+ }
+ switch (level.getStandardLevel()) {
+ case FATAL:
+ return Level.FATAL;
+ case WARN:
+ return Level.WARN;
+ case INFO:
+ return Level.INFO;
+ case DEBUG:
+ return Level.DEBUG;
+ case TRACE:
+ return Level.TRACE;
+ case ALL:
+ return Level.ALL;
+ case OFF:
+ return Level.OFF;
+ default:
+ return Level.ERROR;
+ }
+ }
+
+ public static org.apache.logging.log4j.Level convertLevel(String level,
+ org.apache.logging.log4j.Level defaultLevel) {
+ Level l = toLevel(level, null);
+ return l != null ? convertLevel(l) : defaultLevel;
+ }
+
public static String convertSpecialChars(String s) {
char c;
int len = s.length();
@@ -94,6 +149,28 @@ public class OptionConverter {
return sbuf.toString();
}
+ /**
+ * Find the value corresponding to <code>key</code> in
+ * <code>props</code>. Then perform variable substitution on the
+ * found value.
+ * @param key The key used to locate the substitution string.
+ * @param props The properties to use in the substitution.
+ * @return The substituted string.
+ */
+ public static String findAndSubst(String key, Properties props) {
+ String value = props.getProperty(key);
+ if (value == null) {
+ return null;
+ }
+
+ try {
+ return substVars(value, props);
+ } catch (IllegalArgumentException e) {
+ LOGGER.error("Bad option value [{}].", value, e);
+ return value;
+ }
+ }
+
/**
* Very similar to <code>System.getProperty</code> except
@@ -115,123 +192,6 @@ public class OptionConverter {
}
/**
- * If <code>value</code> is "true", then <code>true</code> is
- * returned. If <code>value</code> is "false", then
- * <code>true</code> is returned. Otherwise, <code>default</code> is
- * returned.
- *
- * <p>Case of value is unimportant.
- * @param value The value to convert.
- * @param dEfault The default value.
- * @return the value of the result.
- */
- public static boolean toBoolean(String value, boolean dEfault) {
- if (value == null) {
- return dEfault;
- }
- String trimmedVal = value.trim();
- if ("true".equalsIgnoreCase(trimmedVal)) {
- return true;
- }
- if ("false".equalsIgnoreCase(trimmedVal)) {
- return false;
- }
- return dEfault;
- }
-
- /**
- * Converts a standard or custom priority level to a Level
- * object. <p> If <code>value</code> is of form
- * "level#classname", then the specified class' toLevel method
- * is called to process the specified level string; if no '#'
- * character is present, then the default {@link org.apache.log4j.Level}
- * class is used to process the level value.
- *
- * <p>As a special case, if the <code>value</code> parameter is
- * equal to the string "NULL", then the value <code>null</code> will
- * be returned.
- *
- * <p> If any error occurs while converting the value to a level,
- * the <code>defaultValue</code> parameter, which may be
- * <code>null</code>, is returned.
- *
- * <p> Case of <code>value</code> is insignificant for the level level, but is
- * significant for the class name part, if present.
- * @param value The value to convert.
- * @param defaultValue The default value.
- * @return the value of the result.
- *
- * @since 1.1
- */
- public static Level toLevel(String value, Level defaultValue) {
- if (value == null) {
- return defaultValue;
- }
-
- value = value.trim();
-
- int hashIndex = value.indexOf('#');
- if (hashIndex == -1) {
- if ("NULL".equalsIgnoreCase(value)) {
- return null;
- }
- // no class name specified : use standard Level class
- return Level.toLevel(value, defaultValue);
- }
-
- Level result = defaultValue;
-
- String clazz = value.substring(hashIndex + 1);
- String levelName = value.substring(0, hashIndex);
-
- // This is degenerate case but you never know.
- if ("NULL".equalsIgnoreCase(levelName)) {
- return null;
- }
-
- LOGGER.debug("toLevel" + ":class=[" + clazz + "]"
- + ":pri=[" + levelName + "]");
-
- try {
- Class<?> customLevel = LoaderUtil.loadClass(clazz);
-
- // get a ref to the specified class' static method
- // toLevel(String, org.apache.log4j.Level)
- Class<?>[] paramTypes = new Class[] { String.class, org.apache.log4j.Level.class };
- java.lang.reflect.Method toLevelMethod =
- customLevel.getMethod("toLevel", paramTypes);
-
- // now call the toLevel method, passing level string + default
- Object[] params = new Object[]{levelName, defaultValue};
- Object o = toLevelMethod.invoke(null, params);
-
- result = (Level) o;
- } catch (ClassNotFoundException e) {
- LOGGER.warn("custom level class [" + clazz + "] not found.");
- } catch (NoSuchMethodException e) {
- LOGGER.warn("custom level class [" + clazz + "]"
- + " does not have a class function toLevel(String, Level)", e);
- } catch (java.lang.reflect.InvocationTargetException e) {
- if (e.getTargetException() instanceof InterruptedException
- || e.getTargetException() instanceof InterruptedIOException) {
- Thread.currentThread().interrupt();
- }
- LOGGER.warn("custom level class [" + clazz + "]"
- + " could not be instantiated", e);
- } catch (ClassCastException e) {
- LOGGER.warn("class [" + clazz
- + "] is not a subclass of org.apache.log4j.Level", e);
- } catch (IllegalAccessException e) {
- LOGGER.warn("class [" + clazz +
- "] cannot be instantiated due to access restrictions", e);
- } catch (RuntimeException e) {
- LOGGER.warn("class [" + clazz + "], level [" + levelName +
- "] conversion failed.", e);
- }
- return result;
- }
-
- /**
* Instantiate an object given a class name. Check that the
* <code>className</code> is a subclass of
* <code>superClass</code>. If that test fails or the object could
@@ -261,6 +221,78 @@ public class OptionConverter {
return defaultValue;
}
+ /**
+ * Configure log4j given an {@link InputStream}.
+ * <p>
+ * The InputStream will be interpreted by a new instance of a log4j configurator.
+ * </p>
+ * <p>
+ * All configurations steps are taken on the <code>hierarchy</code> passed as a parameter.
+ * </p>
+ *
+ * @param inputStream The configuration input stream.
+ * @param clazz The class name, of the log4j configurator which will parse the <code>inputStream</code>. This must be a
+ * subclass of {@link Configurator}, or null. If this value is null then a default configurator of
+ * {@link PropertyConfigurator} is used.
+ * @param hierarchy The {@link LoggerRepository} to act on.
+ * @since 1.2.17
+ */
+ static public void selectAndConfigure(InputStream inputStream, String clazz, LoggerRepository hierarchy) {
+ Configurator configurator = null;
+
+ if (clazz != null) {
+ LOGGER.debug("Preferred configurator class: " + clazz);
+ configurator = (Configurator) instantiateByClassName(clazz, Configurator.class, null);
+ if (configurator == null) {
+ LOGGER.error("Could not instantiate configurator [" + clazz + "].");
+ return;
+ }
+ } else {
+ configurator = new PropertyConfigurator();
+ }
+
+ configurator.doConfigure(inputStream, hierarchy);
+ }
+
+ /**
+ * Configure log4j given a URL.
+ * <p>
+ * The url must point to a file or resource which will be interpreted by a new instance of a log4j configurator.
+ * </p>
+ * <p>
+ * All configurations steps are taken on the <code>hierarchy</code> passed as a parameter.
+ * </p>
+ *
+ * @param url The location of the configuration file or resource.
+ * @param clazz The classname, of the log4j configurator which will parse the file or resource at <code>url</code>. This
+ * must be a subclass of {@link Configurator}, or null. If this value is null then a default configurator of
+ * {@link PropertyConfigurator} is used, unless the filename pointed to by <code>url</code> ends in '.xml', in
+ * which case {@link org.apache.log4j.xml.DOMConfigurator} is used.
+ * @param hierarchy The {@link LoggerRepository} to act on.
+ *
+ * @since 1.1.4
+ */
+ static public void selectAndConfigure(URL url, String clazz, LoggerRepository hierarchy) {
+ Configurator configurator = null;
+ String filename = url.getFile();
+
+ if (clazz == null && filename != null && filename.endsWith(".xml")) {
+ clazz = "org.apache.log4j.xml.DOMConfigurator";
+ }
+
+ if (clazz != null) {
+ LOGGER.debug("Preferred configurator class: " + clazz);
+ configurator = (Configurator) instantiateByClassName(clazz, Configurator.class, null);
+ if (configurator == null) {
+ LOGGER.error("Could not instantiate configurator [" + clazz + "].");
+ return;
+ }
+ } else {
+ configurator = new PropertyConfigurator();
+ }
+
+ configurator.doConfigure(url, hierarchy);
+ }
/**
* Perform variable substitution in string <code>val</code> from the
@@ -360,158 +392,126 @@ public class OptionConverter {
}
}
- public static org.apache.logging.log4j.Level convertLevel(String level,
- org.apache.logging.log4j.Level defaultLevel) {
- Level l = toLevel(level, null);
- return l != null ? convertLevel(l) : defaultLevel;
- }
-
- public static org.apache.logging.log4j.Level convertLevel(Level level) {
- if (level == null) {
- return org.apache.logging.log4j.Level.ERROR;
- }
- if (level.isGreaterOrEqual(Level.FATAL)) {
- return org.apache.logging.log4j.Level.FATAL;
- } else if (level.isGreaterOrEqual(Level.ERROR)) {
- return org.apache.logging.log4j.Level.ERROR;
- } else if (level.isGreaterOrEqual(Level.WARN)) {
- return org.apache.logging.log4j.Level.WARN;
- } else if (level.isGreaterOrEqual(Level.INFO)) {
- return org.apache.logging.log4j.Level.INFO;
- } else if (level.isGreaterOrEqual(Level.DEBUG)) {
- return org.apache.logging.log4j.Level.DEBUG;
- } else if (level.isGreaterOrEqual(Level.TRACE)) {
- return org.apache.logging.log4j.Level.TRACE;
+ /**
+ * If <code>value</code> is "true", then <code>true</code> is
+ * returned. If <code>value</code> is "false", then
+ * <code>true</code> is returned. Otherwise, <code>default</code> is
+ * returned.
+ *
+ * <p>Case of value is unimportant.
+ * @param value The value to convert.
+ * @param dEfault The default value.
+ * @return the value of the result.
+ */
+ public static boolean toBoolean(String value, boolean dEfault) {
+ if (value == null) {
+ return dEfault;
}
- return org.apache.logging.log4j.Level.ALL;
- }
-
- public static Level convertLevel(org.apache.logging.log4j.Level level) {
- if (level == null) {
- return Level.ERROR;
+ String trimmedVal = value.trim();
+ if ("true".equalsIgnoreCase(trimmedVal)) {
+ return true;
}
- switch (level.getStandardLevel()) {
- case FATAL:
- return Level.FATAL;
- case WARN:
- return Level.WARN;
- case INFO:
- return Level.INFO;
- case DEBUG:
- return Level.DEBUG;
- case TRACE:
- return Level.TRACE;
- case ALL:
- return Level.ALL;
- case OFF:
- return Level.OFF;
- default:
- return Level.ERROR;
+ if ("false".equalsIgnoreCase(trimmedVal)) {
+ return false;
}
+ return dEfault;
}
/**
- * Configure log4j given an {@link InputStream}.
- * <p>
- * The InputStream will be interpreted by a new instance of a log4j configurator.
- * </p>
- * <p>
- * All configurations steps are taken on the <code>hierarchy</code> passed as a parameter.
- * </p>
- *
- * @param inputStream The configuration input stream.
- * @param clazz The class name, of the log4j configurator which will parse the <code>inputStream</code>. This must be a
- * subclass of {@link Configurator}, or null. If this value is null then a default configurator of
- * {@link PropertyConfigurator} is used.
- * @param hierarchy The {@link LoggerRepository} to act on.
- * @since 1.2.17
+ * Converts a standard or custom priority level to a Level
+ * object. <p> If <code>value</code> is of form
+ * "level#classname", then the specified class' toLevel method
+ * is called to process the specified level string; if no '#'
+ * character is present, then the default {@link org.apache.log4j.Level}
+ * class is used to process the level value.
+ *
+ * <p>As a special case, if the <code>value</code> parameter is
+ * equal to the string "NULL", then the value <code>null</code> will
+ * be returned.
+ *
+ * <p> If any error occurs while converting the value to a level,
+ * the <code>defaultValue</code> parameter, which may be
+ * <code>null</code>, is returned.
+ *
+ * <p> Case of <code>value</code> is insignificant for the level level, but is
+ * significant for the class name part, if present.
+ * @param value The value to convert.
+ * @param defaultValue The default value.
+ * @return the value of the result.
+ *
+ * @since 1.1
*/
- static public void selectAndConfigure(InputStream inputStream, String clazz, LoggerRepository hierarchy) {
- Configurator configurator = null;
-
- if (clazz != null) {
- LOGGER.debug("Preferred configurator class: " + clazz);
- configurator = (Configurator) instantiateByClassName(clazz, Configurator.class, null);
- if (configurator == null) {
- LOGGER.error("Could not instantiate configurator [" + clazz + "].");
- return;
- }
- } else {
- configurator = new PropertyConfigurator();
+ public static Level toLevel(String value, Level defaultValue) {
+ if (value == null) {
+ return defaultValue;
}
- configurator.doConfigure(inputStream, hierarchy);
- }
-
- /**
- * Configure log4j given a URL.
- * <p>
- * The url must point to a file or resource which will be interpreted by a new instance of a log4j configurator.
- * </p>
- * <p>
- * All configurations steps are taken on the <code>hierarchy</code> passed as a parameter.
- * </p>
- *
- * @param url The location of the configuration file or resource.
- * @param clazz The classname, of the log4j configurator which will parse the file or resource at <code>url</code>. This
- * must be a subclass of {@link Configurator}, or null. If this value is null then a default configurator of
- * {@link PropertyConfigurator} is used, unless the filename pointed to by <code>url</code> ends in '.xml', in
- * which case {@link org.apache.log4j.xml.DOMConfigurator} is used.
- * @param hierarchy The {@link LoggerRepository} to act on.
- *
- * @since 1.1.4
- */
- static public void selectAndConfigure(URL url, String clazz, LoggerRepository hierarchy) {
- Configurator configurator = null;
- String filename = url.getFile();
-
- if (clazz == null && filename != null && filename.endsWith(".xml")) {
- clazz = "org.apache.log4j.xml.DOMConfigurator";
- }
+ value = value.trim();
- if (clazz != null) {
- LOGGER.debug("Preferred configurator class: " + clazz);
- configurator = (Configurator) instantiateByClassName(clazz, Configurator.class, null);
- if (configurator == null) {
- LOGGER.error("Could not instantiate configurator [" + clazz + "].");
- return;
+ int hashIndex = value.indexOf('#');
+ if (hashIndex == -1) {
+ if ("NULL".equalsIgnoreCase(value)) {
+ return null;
}
- } else {
- configurator = new PropertyConfigurator();
+ // no class name specified : use standard Level class
+ return Level.toLevel(value, defaultValue);
}
- configurator.doConfigure(url, hierarchy);
- }
+ Level result = defaultValue;
- /**
- * Find the value corresponding to <code>key</code> in
- * <code>props</code>. Then perform variable substitution on the
- * found value.
- * @param key The key used to locate the substitution string.
- * @param props The properties to use in the substitution.
- * @return The substituted string.
- */
- public static String findAndSubst(String key, Properties props) {
- String value = props.getProperty(key);
- if (value == null) {
+ String clazz = value.substring(hashIndex + 1);
+ String levelName = value.substring(0, hashIndex);
+
+ // This is degenerate case but you never know.
+ if ("NULL".equalsIgnoreCase(levelName)) {
return null;
}
+ LOGGER.debug("toLevel" + ":class=[" + clazz + "]"
+ + ":pri=[" + levelName + "]");
+
try {
- return substVars(value, props);
- } catch (IllegalArgumentException e) {
- LOGGER.error("Bad option value [{}].", value, e);
- return value;
- }
- }
+ Class<?> customLevel = LoaderUtil.loadClass(clazz);
- private static class CharMap {
- final char key;
- final char replacement;
+ // get a ref to the specified class' static method
+ // toLevel(String, org.apache.log4j.Level)
+ Class<?>[] paramTypes = new Class[] { String.class, org.apache.log4j.Level.class };
+ java.lang.reflect.Method toLevelMethod =
+ customLevel.getMethod("toLevel", paramTypes);
- public CharMap(char key, char replacement) {
- this.key = key;
- this.replacement = replacement;
+ // now call the toLevel method, passing level string + default
+ Object[] params = new Object[]{levelName, defaultValue};
+ Object o = toLevelMethod.invoke(null, params);
+
+ result = (Level) o;
+ } catch (ClassNotFoundException e) {
+ LOGGER.warn("custom level class [" + clazz + "] not found.");
+ } catch (NoSuchMethodException e) {
+ LOGGER.warn("custom level class [" + clazz + "]"
+ + " does not have a class function toLevel(String, Level)", e);
+ } catch (java.lang.reflect.InvocationTargetException e) {
+ if (e.getTargetException() instanceof InterruptedException
+ || e.getTargetException() instanceof InterruptedIOException) {
+ Thread.currentThread().interrupt();
+ }
+ LOGGER.warn("custom level class [" + clazz + "]"
+ + " could not be instantiated", e);
+ } catch (ClassCastException e) {
+ LOGGER.warn("class [" + clazz
+ + "] is not a subclass of org.apache.log4j.Level", e);
+ } catch (IllegalAccessException e) {
+ LOGGER.warn("class [" + clazz +
+ "] cannot be instantiated due to access restrictions", e);
+ } catch (RuntimeException e) {
+ LOGGER.warn("class [" + clazz + "], level [" + levelName +
+ "] conversion failed.", e);
}
+ return result;
+ }
+
+ /**
+ * OptionConverter is a static class.
+ */
+ private OptionConverter() {
}
}