in log4j-1.2-api/src/main/java/org/apache/log4j/helpers/OptionConverter.java [626:696]
public static Level toLevel(final String clazz, final String levelName, final Level defaultValue) {
// This is degenerate case but you never know.
if ("NULL".equalsIgnoreCase(levelName)) {
return null;
}
LOGGER.debug("toLevel:class=[{}]:pri=[{}]", clazz, levelName);
// Support for levels defined in Log4j2.
if (LOG4J2_LEVEL_CLASS.equals(clazz)) {
final org.apache.logging.log4j.Level v2Level =
org.apache.logging.log4j.Level.getLevel(toRootUpperCase(levelName));
if (v2Level != null) {
switch (v2Level.name()) {
case "ALL":
return Level.ALL;
case "DEBUG":
return Level.DEBUG;
case "ERROR":
return Level.ERROR;
case "FATAL":
return Level.FATAL;
case "INFO":
return Level.INFO;
case "OFF":
return Level.OFF;
case "WARN":
return Level.WARN;
case "TRACE":
return Level.TRACE;
default:
return new LevelWrapper(v2Level);
}
} else {
return defaultValue;
}
}
try {
final Class<?> customLevel = LoaderUtil.loadClass(clazz);
// get a ref to the specified class' static method
// toLevel(String, org.apache.log4j.Level)
final Class<?>[] paramTypes = new Class[] {String.class, org.apache.log4j.Level.class};
final java.lang.reflect.Method toLevelMethod = customLevel.getMethod("toLevel", paramTypes);
// now call the toLevel method, passing level string + default
final Object[] params = new Object[] {levelName, defaultValue};
final Object o = toLevelMethod.invoke(null, params);
return (Level) o;
} catch (final ClassNotFoundException e) {
LOGGER.warn("custom level class [" + clazz + "] not found.");
} catch (final NoSuchMethodException e) {
LOGGER.warn(
"custom level class [" + clazz + "]" + " does not have a class function toLevel(String, Level)", e);
} catch (final 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 (final ClassCastException e) {
LOGGER.warn("class [" + clazz + "] is not a subclass of org.apache.log4j.Level", e);
} catch (final IllegalAccessException e) {
LOGGER.warn("class [" + clazz + "] cannot be instantiated due to access restrictions", e);
} catch (final RuntimeException e) {
LOGGER.warn("class [" + clazz + "], level [" + levelName + "] conversion failed.", e);
}
return defaultValue;
}