Search in sources :

Example 1 with StrLookup

use of org.apache.logging.log4j.core.lookup.StrLookup in project geode by apache.

the class LogService method isUsingGemFireDefaultConfig.

public static boolean isUsingGemFireDefaultConfig() {
    final Configuration config = ((org.apache.logging.log4j.core.Logger) LogManager.getLogger(ROOT_LOGGER_NAME, GemFireParameterizedMessageFactory.INSTANCE)).getContext().getConfiguration();
    final StrSubstitutor sub = config.getStrSubstitutor();
    final StrLookup resolver = sub.getVariableResolver();
    final String value = resolver.lookup(GEMFIRE_DEFAULT_PROPERTY);
    return "true".equals(value);
}
Also used : StrSubstitutor(org.apache.logging.log4j.core.lookup.StrSubstitutor) Configuration(org.apache.logging.log4j.core.config.Configuration) StrLookup(org.apache.logging.log4j.core.lookup.StrLookup)

Example 2 with StrLookup

use of org.apache.logging.log4j.core.lookup.StrLookup in project logging-log4j2 by apache.

the class AbstractConfiguration method doConfigure.

protected void doConfigure() {
    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) {
            subst.setVariableResolver((StrLookup) first.getObject());
        }
    } else {
        final Map<String, String> map = this.getComponent(CONTEXT_PROPERTIES);
        final StrLookup lookup = map == null ? null : new MapLookup(map);
        subst.setVariableResolver(new Interpolator(lookup, pluginPackages));
    }
    boolean setLoggers = false;
    boolean setRoot = false;
    for (final Node child : rootNode.getChildren()) {
        if (child.getName().equalsIgnoreCase("Properties")) {
            if (tempLookup == subst.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")) {
            for (final AbstractScript script : child.getObject(AbstractScript[].class)) {
                if (script instanceof ScriptRef) {
                    LOGGER.error("Script reference to {} not added. Scripts definition cannot contain script references", script.getName());
                } else {
                    if (scriptManager != null) {
                        scriptManager.addScript(script);
                    }
                }
            }
        } 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) AsyncLoggerConfig(org.apache.logging.log4j.core.async.AsyncLoggerConfig) StrLookup(org.apache.logging.log4j.core.lookup.StrLookup) Filter(org.apache.logging.log4j.core.Filter) AbstractScript(org.apache.logging.log4j.core.script.AbstractScript) MapLookup(org.apache.logging.log4j.core.lookup.MapLookup) 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) ScriptRef(org.apache.logging.log4j.core.script.ScriptRef)

Aggregations

StrLookup (org.apache.logging.log4j.core.lookup.StrLookup)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Appender (org.apache.logging.log4j.core.Appender)1 Filter (org.apache.logging.log4j.core.Filter)1 AsyncAppender (org.apache.logging.log4j.core.appender.AsyncAppender)1 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)1 AsyncLoggerConfig (org.apache.logging.log4j.core.async.AsyncLoggerConfig)1 Configuration (org.apache.logging.log4j.core.config.Configuration)1 Interpolator (org.apache.logging.log4j.core.lookup.Interpolator)1 MapLookup (org.apache.logging.log4j.core.lookup.MapLookup)1 StrSubstitutor (org.apache.logging.log4j.core.lookup.StrSubstitutor)1 AbstractScript (org.apache.logging.log4j.core.script.AbstractScript)1 ScriptRef (org.apache.logging.log4j.core.script.ScriptRef)1