Search in sources :

Example 1 with LoggerComponentBuilder

use of org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder in project logging-log4j2 by apache.

the class Log4j1ConfigurationParser method buildLoggers.

private void buildLoggers(final String prefix) {
    final int preLength = prefix.length();
    for (final Map.Entry<Object, Object> entry : properties.entrySet()) {
        final Object keyObj = entry.getKey();
        if (keyObj != null) {
            final String key = keyObj.toString();
            if (key.startsWith(prefix)) {
                final String name = key.substring(preLength);
                final Object value = entry.getValue();
                if (value != null) {
                    // a Level may be followed by a list of Appender refs.
                    final String valueStr = value.toString();
                    final String[] split = valueStr.split(COMMA_DELIMITED_RE);
                    final String level = getLevelString(split, null);
                    if (level == null) {
                        warn("Level is missing for entry " + entry);
                    } else {
                        final LoggerComponentBuilder newLogger = builder.newLogger(name, level);
                        if (split.length > 1) {
                            // Add Appenders to this logger
                            final String[] sortedAppenderNames = Arrays.copyOfRange(split, 1, split.length);
                            Arrays.sort(sortedAppenderNames);
                            for (final String appenderName : sortedAppenderNames) {
                                newLogger.add(builder.newAppenderRef(appenderName));
                            }
                        }
                        builder.add(newLogger);
                    }
                }
            }
        }
    }
}
Also used : RootLoggerComponentBuilder(org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder) LoggerComponentBuilder(org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with LoggerComponentBuilder

use of org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder 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)

Aggregations

LoggerComponentBuilder (org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder)2 RootLoggerComponentBuilder (org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConfigurationException (org.apache.logging.log4j.core.config.ConfigurationException)1