Search in sources :

Example 1 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 2 with ConfigurationException

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

the class PropertiesConfigurationBuilder method build.

@Override
public PropertiesConfiguration build() {
    for (final String key : rootProperties.stringPropertyNames()) {
        if (!key.contains(".")) {
            builder.addRootProperty(key, rootProperties.getProperty(key));
        }
    }
    builder.setStatusLevel(Level.toLevel(rootProperties.getProperty(STATUS_KEY), Level.ERROR)).setShutdownHook(rootProperties.getProperty(SHUTDOWN_HOOK)).setShutdownTimeout(Long.parseLong(rootProperties.getProperty(SHUTDOWN_TIMEOUT, "0")), TimeUnit.MILLISECONDS).setVerbosity(rootProperties.getProperty(VERBOSE)).setDestination(rootProperties.getProperty(DEST)).setPackages(rootProperties.getProperty(PACKAGES)).setConfigurationName(rootProperties.getProperty(CONFIG_NAME)).setMonitorInterval(rootProperties.getProperty(MONITOR_INTERVAL, "0")).setAdvertiser(rootProperties.getProperty(ADVERTISER_KEY));
    final Properties propertyPlaceholders = PropertiesUtil.extractSubset(rootProperties, "property");
    for (final String key : propertyPlaceholders.stringPropertyNames()) {
        builder.addProperty(key, propertyPlaceholders.getProperty(key));
    }
    final Map<String, Properties> scripts = PropertiesUtil.partitionOnCommonPrefixes(PropertiesUtil.extractSubset(rootProperties, "script"));
    for (final Map.Entry<String, Properties> entry : scripts.entrySet()) {
        final Properties scriptProps = entry.getValue();
        final String type = (String) scriptProps.remove("type");
        if (type == null) {
            throw new ConfigurationException("No type provided for script - must be Script or ScriptFile");
        }
        if (type.equalsIgnoreCase("script")) {
            builder.add(createScript(scriptProps));
        } else {
            builder.add(createScriptFile(scriptProps));
        }
    }
    final Properties levelProps = PropertiesUtil.extractSubset(rootProperties, "customLevel");
    if (levelProps.size() > 0) {
        for (final String key : levelProps.stringPropertyNames()) {
            builder.add(builder.newCustomLevel(key, Integer.parseInt(levelProps.getProperty(key))));
        }
    }
    final String filterProp = rootProperties.getProperty("filters");
    if (filterProp != null) {
        final String[] filterNames = filterProp.split(",");
        for (final String filterName : filterNames) {
            final String name = filterName.trim();
            builder.add(createFilter(name, PropertiesUtil.extractSubset(rootProperties, "filter." + name)));
        }
    } else {
        final Map<String, Properties> filters = PropertiesUtil.partitionOnCommonPrefixes(PropertiesUtil.extractSubset(rootProperties, "filter"));
        for (final Map.Entry<String, Properties> entry : filters.entrySet()) {
            builder.add(createFilter(entry.getKey().trim(), entry.getValue()));
        }
    }
    final String appenderProp = rootProperties.getProperty("appenders");
    if (appenderProp != null) {
        final String[] appenderNames = appenderProp.split(",");
        for (final String appenderName : appenderNames) {
            final String name = appenderName.trim();
            builder.add(createAppender(appenderName.trim(), PropertiesUtil.extractSubset(rootProperties, "appender." + name)));
        }
    } else {
        final Map<String, Properties> appenders = PropertiesUtil.partitionOnCommonPrefixes(PropertiesUtil.extractSubset(rootProperties, Appender.ELEMENT_TYPE));
        for (final Map.Entry<String, Properties> entry : appenders.entrySet()) {
            builder.add(createAppender(entry.getKey().trim(), entry.getValue()));
        }
    }
    final String loggerProp = rootProperties.getProperty("loggers");
    if (loggerProp != null) {
        final String[] loggerNames = loggerProp.split(",");
        for (final String loggerName : loggerNames) {
            final String name = loggerName.trim();
            if (!name.equals(LoggerConfig.ROOT)) {
                builder.add(createLogger(name, PropertiesUtil.extractSubset(rootProperties, "logger." + name)));
            }
        }
    } else {
        final Map<String, Properties> loggers = PropertiesUtil.partitionOnCommonPrefixes(PropertiesUtil.extractSubset(rootProperties, "logger"));
        for (final Map.Entry<String, Properties> entry : loggers.entrySet()) {
            final String name = entry.getKey().trim();
            if (!name.equals(LoggerConfig.ROOT)) {
                builder.add(createLogger(name, entry.getValue()));
            }
        }
    }
    final Properties props = PropertiesUtil.extractSubset(rootProperties, "rootLogger");
    if (props.size() > 0) {
        builder.add(createRootLogger(props));
    }
    builder.setLoggerContext(loggerContext);
    return builder.build(false);
}
Also used : ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) Properties(java.util.Properties) Map(java.util.Map)

Example 3 with ConfigurationException

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

the class PropertiesConfigurationBuilder method createLogger.

private LoggerComponentBuilder createLogger(final String key, final Properties properties) {
    final String name = (String) properties.remove(CONFIG_NAME);
    final String location = (String) properties.remove("includeLocation");
    if (Strings.isEmpty(name)) {
        throw new ConfigurationException("No name attribute provided for Logger " + key);
    }
    final String level = (String) properties.remove("level");
    final String type = (String) properties.remove(CONFIG_TYPE);
    final LoggerComponentBuilder loggerBuilder;
    boolean includeLocation;
    if (type != null) {
        if (type.equalsIgnoreCase("asyncLogger")) {
            if (location != null) {
                includeLocation = Boolean.parseBoolean(location);
                loggerBuilder = builder.newAsyncLogger(name, level, includeLocation);
            } else {
                loggerBuilder = builder.newAsyncLogger(name, level);
            }
        } else {
            throw new ConfigurationException("Unknown Logger type " + type + " for Logger " + name);
        }
    } else {
        if (location != null) {
            includeLocation = Boolean.parseBoolean(location);
            loggerBuilder = builder.newLogger(name, level, includeLocation);
        } else {
            loggerBuilder = builder.newLogger(name, level);
        }
    }
    addLoggersToComponent(loggerBuilder, properties);
    addFiltersToComponent(loggerBuilder, properties);
    final String additivity = (String) properties.remove("additivity");
    if (!Strings.isEmpty(additivity)) {
        loggerBuilder.addAttribute("additivity", additivity);
    }
    return loggerBuilder;
}
Also used : ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) RootLoggerComponentBuilder(org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder) LoggerComponentBuilder(org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder)

Example 4 with ConfigurationException

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

the class PropertiesConfigurationBuilder method createAppender.

private AppenderComponentBuilder createAppender(final String key, final Properties properties) {
    final String name = (String) properties.remove(CONFIG_NAME);
    if (Strings.isEmpty(name)) {
        throw new ConfigurationException("No name attribute provided for Appender " + key);
    }
    final String type = (String) properties.remove(CONFIG_TYPE);
    if (Strings.isEmpty(type)) {
        throw new ConfigurationException("No type attribute provided for Appender " + key);
    }
    final AppenderComponentBuilder appenderBuilder = builder.newAppender(name, type);
    addFiltersToComponent(appenderBuilder, properties);
    final Properties layoutProps = PropertiesUtil.extractSubset(properties, "layout");
    if (layoutProps.size() > 0) {
        appenderBuilder.add(createLayout(name, layoutProps));
    }
    return processRemainingProperties(appenderBuilder, properties);
}
Also used : AppenderComponentBuilder(org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) Properties(java.util.Properties)

Example 5 with ConfigurationException

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

the class PropertiesConfigurationBuilder method createLayout.

private LayoutComponentBuilder createLayout(final String appenderName, final Properties properties) {
    final String type = (String) properties.remove(CONFIG_TYPE);
    if (Strings.isEmpty(type)) {
        throw new ConfigurationException("No type attribute provided for Layout on Appender " + appenderName);
    }
    final LayoutComponentBuilder layoutBuilder = builder.newLayout(type);
    return processRemainingProperties(layoutBuilder, properties);
}
Also used : ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) LayoutComponentBuilder(org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder)

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