Search in sources :

Example 1 with Node

use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.

the class AbstractConfiguration method createPluginMap.

private static Map<String, ?> createPluginMap(final Node node) {
    final Map<String, Object> map = new LinkedHashMap<>();
    for (final Node child : node.getChildren()) {
        final Object object = child.getObject();
        map.put(child.getName(), object);
    }
    return map;
}
Also used : Node(org.apache.logging.log4j.plugins.Node) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with Node

use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.

the class AbstractConfiguration method preConfigure.

protected void preConfigure(final Node node) {
    try {
        for (final Node child : node.getChildren()) {
            if (child.getType() == null) {
                LOGGER.error("Unable to locate plugin type for " + child.getName());
                continue;
            }
            final Class<?> clazz = child.getType().getPluginClass();
            if (clazz.isAnnotationPresent(Scheduled.class)) {
                configurationScheduler.incrementScheduledItems();
            }
            preConfigure(child);
        }
    } catch (final Exception ex) {
        LOGGER.error("Error capturing node data for node " + node.getName(), ex);
    }
}
Also used : Node(org.apache.logging.log4j.plugins.Node) IOException(java.io.IOException)

Example 3 with Node

use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.

the class AbstractConfiguration method doConfigure.

protected void doConfigure() {
    processConditionals(rootNode);
    preConfigure(rootNode);
    configurationScheduler.start();
    if (rootNode.hasChildren() && rootNode.getChildren().get(0).getName().equalsIgnoreCase("Properties")) {
        final Node first = rootNode.getChildren().get(0);
        createConfiguration(first, null);
        if (first.getObject() != null) {
            StrLookup lookup = (StrLookup) first.getObject();
            runtimeStrSubstitutor.setVariableResolver(lookup);
            configurationStrSubstitutor.setVariableResolver(lookup);
        }
    } else {
        final Map<String, String> map = this.getComponent(CONTEXT_PROPERTIES);
        final StrLookup lookup = map == null ? null : new PropertiesLookup(map);
        Interpolator interpolator = new Interpolator(lookup, pluginPackages);
        runtimeStrSubstitutor.setVariableResolver(interpolator);
        configurationStrSubstitutor.setVariableResolver(interpolator);
    }
    boolean setLoggers = false;
    boolean setRoot = false;
    for (final Node child : rootNode.getChildren()) {
        if (child.getName().equalsIgnoreCase("Properties")) {
            if (tempLookup == runtimeStrSubstitutor.getVariableResolver()) {
                LOGGER.error("Properties declaration must be the first element in the configuration");
            }
            continue;
        }
        createConfiguration(child, null);
        if (child.getObject() == null) {
            continue;
        }
        if (child.getName().equalsIgnoreCase("Scripts")) {
            if (scriptManager != null) {
                scriptManager.addScripts(child);
            }
        } else if (child.getName().equalsIgnoreCase("Appenders")) {
            appenders = child.getObject();
        } else if (child.isInstanceOf(Filter.class)) {
            addFilter(child.getObject(Filter.class));
        } else if (child.getName().equalsIgnoreCase("Loggers")) {
            final Loggers l = child.getObject();
            loggerConfigs = l.getMap();
            setLoggers = true;
            if (l.getRoot() != null) {
                root = l.getRoot();
                setRoot = true;
            }
        } else if (child.getName().equalsIgnoreCase("CustomLevels")) {
            customLevels = child.getObject(CustomLevels.class).getCustomLevels();
        } else if (child.isInstanceOf(CustomLevelConfig.class)) {
            final List<CustomLevelConfig> copy = new ArrayList<>(customLevels);
            copy.add(child.getObject(CustomLevelConfig.class));
            customLevels = copy;
        } else {
            final List<String> expected = Arrays.asList("\"Appenders\"", "\"Loggers\"", "\"Properties\"", "\"Scripts\"", "\"CustomLevels\"");
            LOGGER.error("Unknown object \"{}\" of type {} is ignored: try nesting it inside one of: {}.", child.getName(), child.getObject().getClass().getName(), expected);
        }
    }
    if (!setLoggers) {
        LOGGER.warn("No Loggers were configured, using default. Is the Loggers element missing?");
        setToDefault();
        return;
    } else if (!setRoot) {
        LOGGER.warn("No Root logger was configured, creating default ERROR-level Root logger with Console appender");
        setToDefault();
    // return; // LOG4J2-219: creating default root=ok, but don't exclude configured Loggers
    }
    for (final Map.Entry<String, LoggerConfig> entry : loggerConfigs.entrySet()) {
        final LoggerConfig loggerConfig = entry.getValue();
        for (final AppenderRef ref : loggerConfig.getAppenderRefs()) {
            final Appender app = appenders.get(ref.getRef());
            if (app != null) {
                loggerConfig.addAppender(app, ref.getLevel(), ref.getFilter());
            } else {
                LOGGER.error("Unable to locate appender \"{}\" for logger config \"{}\"", ref.getRef(), loggerConfig);
            }
        }
    }
    setParents();
}
Also used : AsyncAppender(org.apache.logging.log4j.core.appender.AsyncAppender) Appender(org.apache.logging.log4j.core.Appender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) Node(org.apache.logging.log4j.plugins.Node) AsyncLoggerConfig(org.apache.logging.log4j.core.async.AsyncLoggerConfig) StrLookup(org.apache.logging.log4j.core.lookup.StrLookup) Filter(org.apache.logging.log4j.core.Filter) Interpolator(org.apache.logging.log4j.core.lookup.Interpolator) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) PropertiesLookup(org.apache.logging.log4j.core.lookup.PropertiesLookup)

Example 4 with Node

use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.

the class BuiltConfiguration method convertToNode.

protected Node convertToNode(final Node parent, final Component component) {
    final String name = component.getPluginType();
    final PluginType<?> pluginType = pluginManager.getPluginType(name);
    final Node node = new Node(parent, name, pluginType);
    node.getAttributes().putAll(component.getAttributes());
    node.setValue(component.getValue());
    final List<Node> children = node.getChildren();
    for (final Component child : component.getComponents()) {
        children.add(convertToNode(node, child));
    }
    return node;
}
Also used : Node(org.apache.logging.log4j.plugins.Node) Component(org.apache.logging.log4j.core.config.builder.api.Component)

Example 5 with Node

use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.

the class RoutingAppender method createAppender.

private Appender createAppender(final Route route, final LogEvent event) {
    final Node routeNode = route.getNode();
    for (final Node node : routeNode.getChildren()) {
        if (node.getType().getElementName().equals(Appender.ELEMENT_TYPE)) {
            final Node appNode = new Node(node);
            configuration.createConfiguration(appNode, event);
            if (appNode.getObject() instanceof Appender) {
                final Appender app = appNode.getObject();
                app.start();
                return app;
            }
            error("Unable to create Appender of type " + node.getName());
            return null;
        }
    }
    error("No Appender was configured for route " + route.getKey());
    return null;
}
Also used : AbstractAppender(org.apache.logging.log4j.core.appender.AbstractAppender) Appender(org.apache.logging.log4j.core.Appender) Node(org.apache.logging.log4j.plugins.Node)

Aggregations

Node (org.apache.logging.log4j.plugins.Node)27 PluginManager (org.apache.logging.log4j.plugins.util.PluginManager)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 Appender (org.apache.logging.log4j.core.Appender)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 Arbiter (org.apache.logging.log4j.core.config.arbiters.Arbiter)2 SelectArbiter (org.apache.logging.log4j.core.config.arbiters.SelectArbiter)2 HostAndPort (org.apache.logging.log4j.plugins.test.validation.HostAndPort)2 HashMap (java.util.HashMap)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Filter (org.apache.logging.log4j.core.Filter)1 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)1 AsyncAppender (org.apache.logging.log4j.core.appender.AsyncAppender)1