Search in sources :

Example 6 with ConfigurationException

use of org.apache.logging.log4j.core.config.ConfigurationException in project logging-log4j2 by apache.

the class PropertiesConfigurationBuilder method createAppenderRef.

private AppenderRefComponentBuilder createAppenderRef(final String key, final Properties properties) {
    final String ref = (String) properties.remove("ref");
    if (Strings.isEmpty(ref)) {
        throw new ConfigurationException("No ref attribute provided for AppenderRef " + key);
    }
    final AppenderRefComponentBuilder appenderRefBuilder = builder.newAppenderRef(ref);
    final String level = (String) properties.remove("level");
    if (!Strings.isEmpty(level)) {
        appenderRefBuilder.addAttribute("level", level);
    }
    return addFiltersToComponent(appenderRefBuilder, properties);
}
Also used : ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) AppenderRefComponentBuilder(org.apache.logging.log4j.core.config.builder.api.AppenderRefComponentBuilder)

Example 7 with ConfigurationException

use of org.apache.logging.log4j.core.config.ConfigurationException in project logging-log4j2 by apache.

the class PluginBuilder method generateParameters.

private Object[] generateParameters(final Method factory) {
    final StringBuilder log = new StringBuilder();
    final Class<?>[] types = factory.getParameterTypes();
    final Annotation[][] annotations = factory.getParameterAnnotations();
    final Object[] args = new Object[annotations.length];
    boolean invalid = false;
    for (int i = 0; i < annotations.length; i++) {
        log.append(log.length() == 0 ? factory.getName() + "(" : ", ");
        final String[] aliases = extractPluginAliases(annotations[i]);
        for (final Annotation a : annotations[i]) {
            if (a instanceof PluginAliases) {
                // already processed
                continue;
            }
            final PluginVisitor<? extends Annotation> visitor = PluginVisitors.findVisitor(a.annotationType());
            if (visitor != null) {
                final Object value = visitor.setAliases(aliases).setAnnotation(a).setConversionType(types[i]).setStrSubstitutor(configuration.getStrSubstitutor()).setMember(factory).visit(configuration, node, event, log);
                // don't overwrite existing values if the visitor gives us no value to inject
                if (value != null) {
                    args[i] = value;
                }
            }
        }
        final Collection<ConstraintValidator<?>> validators = ConstraintValidators.findValidators(annotations[i]);
        final Object value = args[i];
        final String argName = "arg[" + i + "](" + simpleName(value) + ")";
        for (final ConstraintValidator<?> validator : validators) {
            if (!validator.isValid(argName, value)) {
                invalid = true;
            }
        }
    }
    log.append(log.length() == 0 ? factory.getName() + "()" : ")");
    checkForRemainingAttributes();
    verifyNodeChildrenUsed();
    LOGGER.debug(log.toString());
    if (invalid) {
        throw new ConfigurationException("Arguments given for element " + node.getName() + " are invalid");
    }
    return args;
}
Also used : Annotation(java.lang.annotation.Annotation) ConstraintValidator(org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator) PluginAliases(org.apache.logging.log4j.core.config.plugins.PluginAliases) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) AccessibleObject(java.lang.reflect.AccessibleObject)

Example 8 with ConfigurationException

use of org.apache.logging.log4j.core.config.ConfigurationException in project logging-log4j2 by apache.

the class AsyncAppender method start.

@Override
public void start() {
    final Map<String, Appender> map = config.getAppenders();
    final List<AppenderControl> appenders = new ArrayList<>();
    for (final AppenderRef appenderRef : appenderRefs) {
        final Appender appender = map.get(appenderRef.getRef());
        if (appender != null) {
            appenders.add(new AppenderControl(appender, appenderRef.getLevel(), appenderRef.getFilter()));
        } else {
            LOGGER.error("No appender named {} was configured", appenderRef);
        }
    }
    if (errorRef != null) {
        final Appender appender = map.get(errorRef);
        if (appender != null) {
            errorAppender = new AppenderControl(appender, null, null);
        } else {
            LOGGER.error("Unable to set up error Appender. No appender named {} was configured", errorRef);
        }
    }
    if (appenders.size() > 0) {
        thread = new AsyncThread(appenders, queue);
        thread.setName("AsyncAppender-" + getName());
    } else if (errorRef == null) {
        throw new ConfigurationException("No appenders are available for AsyncAppender " + getName());
    }
    asyncQueueFullPolicy = AsyncQueueFullPolicyFactory.create();
    thread.start();
    super.start();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) ArrayList(java.util.ArrayList) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef)

Example 9 with ConfigurationException

use of org.apache.logging.log4j.core.config.ConfigurationException in project logging-log4j2 by apache.

the class PluginBuilder method injectFields.

private void injectFields(final Builder<?> builder) throws IllegalAccessException {
    final List<Field> fields = TypeUtil.getAllDeclaredFields(builder.getClass());
    AccessibleObject.setAccessible(fields.toArray(new Field[] {}), true);
    final StringBuilder log = new StringBuilder();
    boolean invalid = false;
    for (final Field field : fields) {
        log.append(log.length() == 0 ? simpleName(builder) + "(" : ", ");
        final Annotation[] annotations = field.getDeclaredAnnotations();
        final String[] aliases = extractPluginAliases(annotations);
        for (final Annotation a : annotations) {
            if (a instanceof PluginAliases) {
                // already processed
                continue;
            }
            final PluginVisitor<? extends Annotation> visitor = PluginVisitors.findVisitor(a.annotationType());
            if (visitor != null) {
                final Object value = visitor.setAliases(aliases).setAnnotation(a).setConversionType(field.getType()).setStrSubstitutor(configuration.getStrSubstitutor()).setMember(field).visit(configuration, node, event, log);
                // don't overwrite default values if the visitor gives us no value to inject
                if (value != null) {
                    field.set(builder, value);
                }
            }
        }
        final Collection<ConstraintValidator<?>> validators = ConstraintValidators.findValidators(annotations);
        final Object value = field.get(builder);
        for (final ConstraintValidator<?> validator : validators) {
            if (!validator.isValid(field.getName(), value)) {
                invalid = true;
            }
        }
    }
    log.append(log.length() == 0 ? builder.getClass().getSimpleName() + "()" : ")");
    LOGGER.debug(log.toString());
    if (invalid) {
        throw new ConfigurationException("Arguments given for element " + node.getName() + " are invalid");
    }
    checkForRemainingAttributes();
    verifyNodeChildrenUsed();
}
Also used : Annotation(java.lang.annotation.Annotation) Field(java.lang.reflect.Field) ConstraintValidator(org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator) PluginAliases(org.apache.logging.log4j.core.config.plugins.PluginAliases) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) AccessibleObject(java.lang.reflect.AccessibleObject)

Example 10 with ConfigurationException

use of org.apache.logging.log4j.core.config.ConfigurationException in project logging-log4j2 by apache.

the class PropertiesConfigurationBuilder method createRootLogger.

private RootLoggerComponentBuilder createRootLogger(final Properties properties) {
    final String level = (String) properties.remove("level");
    final String type = (String) properties.remove(CONFIG_TYPE);
    final String location = (String) properties.remove("includeLocation");
    final boolean includeLocation;
    final RootLoggerComponentBuilder loggerBuilder;
    if (type != null) {
        if (type.equalsIgnoreCase("asyncRoot")) {
            if (location != null) {
                includeLocation = Boolean.parseBoolean(location);
                loggerBuilder = builder.newAsyncRootLogger(level, includeLocation);
            } else {
                loggerBuilder = builder.newAsyncRootLogger(level);
            }
        } else {
            throw new ConfigurationException("Unknown Logger type for root logger" + type);
        }
    } else {
        if (location != null) {
            includeLocation = Boolean.parseBoolean(location);
            loggerBuilder = builder.newRootLogger(level, includeLocation);
        } else {
            loggerBuilder = builder.newRootLogger(level);
        }
    }
    addLoggersToComponent(loggerBuilder, properties);
    return addFiltersToComponent(loggerBuilder, properties);
}
Also used : ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) RootLoggerComponentBuilder(org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder)

Aggregations

ConfigurationException (org.apache.logging.log4j.core.config.ConfigurationException)12 Annotation (java.lang.annotation.Annotation)2 AccessibleObject (java.lang.reflect.AccessibleObject)2 Map (java.util.Map)2 Properties (java.util.Properties)2 RootLoggerComponentBuilder (org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder)2 PluginAliases (org.apache.logging.log4j.core.config.plugins.PluginAliases)2 ConstraintValidator (org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Field (java.lang.reflect.Field)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Appender (org.apache.logging.log4j.core.Appender)1