Java Code Examples for org.apache.logging.log4j.util.LoaderUtil#loadClass()

The following examples show how to use org.apache.logging.log4j.util.LoaderUtil#loadClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeConverters.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Override
public Class<?> convert(final String s) throws ClassNotFoundException {
    switch (s.toLowerCase()) {
        case "boolean":
            return boolean.class;
        case "byte":
            return byte.class;
        case "char":
            return char.class;
        case "double":
            return double.class;
        case "float":
            return float.class;
        case "int":
            return int.class;
        case "long":
            return long.class;
        case "short":
            return short.class;
        case "void":
            return void.class;
        default:
            return LoaderUtil.loadClass(s);
    }

}
 
Example 2
Source File: JacksonMixIn.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
private Class loadClass(String className, String argName) {

            if (className == null) {
                throw new ConfigurationException(String.format("No %s provided for %s", argName, JacksonMixIn.PLUGIN_NAME));
            }

            try {
                return LoaderUtil.loadClass(className);
            } catch (ClassNotFoundException e) {
                throw new ConfigurationException(String.format("Cannot load %s: %s for %s", argName, className, JacksonMixIn.PLUGIN_NAME));
            }

        }
 
Example 3
Source File: XmlConfiguration.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Used internally to parse a level  element.
 */
private void parseLevel(Element element, LoggerConfig logger, boolean isRoot) {
    String catName = logger.getName();
    if (isRoot) {
        catName = "root";
    }

    String priStr = subst(element.getAttribute(VALUE_ATTR));
    LOGGER.debug("Level value for {} is [{}}].", catName, priStr);

    if (INHERITED.equalsIgnoreCase(priStr) || NULL.equalsIgnoreCase(priStr)) {
        if (isRoot) {
            LOGGER.error("Root level cannot be inherited. Ignoring directive.");
        } else {
            logger.setLevel(null);
        }
    } else {
        String className = subst(element.getAttribute(CLASS_ATTR));
        if (EMPTY_STR.equals(className)) {
            logger.setLevel(OptionConverter.convertLevel(priStr, org.apache.logging.log4j.Level.DEBUG));
        } else {
            LOGGER.debug("Desired Level sub-class: [{}]", className);
            try {
                Class<?> clazz = LoaderUtil.loadClass(className);
                Method toLevelMethod = clazz.getMethod("toLevel", ONE_STRING_PARAM);
                Level pri = (Level) toLevelMethod.invoke(null, new Object[]{priStr});
                logger.setLevel(OptionConverter.convertLevel(pri));
            } catch (Exception oops) {
                if (oops instanceof InterruptedException || oops instanceof InterruptedIOException) {
                    Thread.currentThread().interrupt();
                }
                LOGGER.error("Could not create level [" + priStr +
                        "]. Reported error follows.", oops);
                return;
            }
        }
    }
    LOGGER.debug("{} level set to {}", catName,  logger.getLevel());
}
 
Example 4
Source File: XmlConfigurationFactory.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Used internally to parse a level  element.
 */
private void parseLevel(Element element, LoggerConfig logger, boolean isRoot) {
    String catName = logger.getName();
    if (isRoot) {
        catName = "root";
    }

    String priStr = subst(element.getAttribute(VALUE_ATTR));
    LOGGER.debug("Level value for {} is [{}}].", catName, priStr);

    if (INHERITED.equalsIgnoreCase(priStr) || NULL.equalsIgnoreCase(priStr)) {
        if (isRoot) {
            LOGGER.error("Root level cannot be inherited. Ignoring directive.");
        } else {
            logger.setLevel(null);
        }
    } else {
        String className = subst(element.getAttribute(CLASS_ATTR));
        if (EMPTY_STR.equals(className)) {
            logger.setLevel(convertLevel(OptionConverter.toLevel(priStr, Level.DEBUG)));
        } else {
            LOGGER.debug("Desired Level sub-class: [{}]", className);
            try {
                Class<?> clazz = LoaderUtil.loadClass(className);
                Method toLevelMethod = clazz.getMethod("toLevel", ONE_STRING_PARAM);
                Level pri = (Level) toLevelMethod.invoke(null, new Object[]{priStr});
                logger.setLevel(convertLevel(pri));
            } catch (Exception oops) {
                if (oops instanceof InterruptedException || oops instanceof InterruptedIOException) {
                    Thread.currentThread().interrupt();
                }
                LOGGER.error("Could not create level [" + priStr +
                        "]. Reported error follows.", oops);
                return;
            }
        }
    }
    LOGGER.debug("{} level set to {}", catName,  logger.getLevel());
}
 
Example 5
Source File: Log4jLogger.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private static EventDataConverter createConverter() {
    try {
        LoaderUtil.loadClass("org.slf4j.ext.EventData");
        return new EventDataConverter();
    } catch (final ClassNotFoundException cnfe) {
        return null;
    }
}
 
Example 6
Source File: SLF4JLoggerContextFactory.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public SLF4JLoggerContextFactory() {
    // LOG4J2-230, LOG4J2-204 (improve error reporting when misconfigured)
    boolean misconfigured = false;
    try {
        LoaderUtil.loadClass("org.slf4j.helpers.Log4jLoggerFactory");
        misconfigured = true;
    } catch (final ClassNotFoundException classNotFoundIsGood) {
        LOGGER.debug("org.slf4j.helpers.Log4jLoggerFactory is not on classpath. Good!");
    }
    if (misconfigured) {
        throw new IllegalStateException("slf4j-impl jar is mutually exclusive with log4j-to-slf4j jar "
                + "(the first routes calls from SLF4J to Log4j, the second from Log4j to SLF4J)");
    }
}
 
Example 7
Source File: ThrowableAttributeConverter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private Throwable getThrowable(final String throwableClassName, final String message, final Throwable cause,
                               final StackTraceElement[] stackTrace) {
    try {
        @SuppressWarnings("unchecked")
        final Class<Throwable> throwableClass = (Class<Throwable>) LoaderUtil.loadClass(throwableClassName);

        if (!Throwable.class.isAssignableFrom(throwableClass)) {
            return null;
        }

        Throwable throwable;
        if (message != null && cause != null) {
            throwable = this.getThrowable(throwableClass, message, cause);
            if (throwable == null) {
                throwable = this.getThrowable(throwableClass, cause);
                if (throwable == null) {
                    throwable = this.getThrowable(throwableClass, message);
                    if (throwable == null) {
                        throwable = this.getThrowable(throwableClass);
                        if (throwable != null) {
                            THROWABLE_MESSAGE.set(throwable, message);
                            THROWABLE_CAUSE.set(throwable, cause);
                        }
                    } else {
                        THROWABLE_CAUSE.set(throwable, cause);
                    }
                } else {
                    THROWABLE_MESSAGE.set(throwable, message);
                }
            }
        } else if (cause != null) {
            throwable = this.getThrowable(throwableClass, cause);
            if (throwable == null) {
                throwable = this.getThrowable(throwableClass);
                if (throwable != null) {
                    THROWABLE_CAUSE.set(throwable, cause);
                }
            }
        } else if (message != null) {
            throwable = this.getThrowable(throwableClass, message);
            if (throwable == null) {
                throwable = this.getThrowable(throwableClass);
                if (throwable != null) {
                    THROWABLE_MESSAGE.set(throwable, cause);
                }
            }
        } else {
            throwable = this.getThrowable(throwableClass);
        }

        if (throwable == null) {
            return null;
        }
        throwable.setStackTrace(stackTrace);
        return throwable;
    } catch (final Exception e) {
        return null;
    }
}