Search in sources :

Example 1 with ScriptManager

use of org.apache.logging.log4j.core.script.ScriptManager in project logging-log4j2 by apache.

the class AbstractConfiguration method initialize.

/**
     * Initialize the configuration.
     */
@Override
public void initialize() {
    LOGGER.debug("Initializing configuration {}", this);
    subst.setConfiguration(this);
    try {
        scriptManager = new ScriptManager(this, watchManager);
    } catch (final LinkageError | Exception e) {
        // LOG4J2-1920 ScriptEngineManager is not available in Android
        LOGGER.info("Cannot initialize scripting support because this JRE does not support it.", e);
    }
    pluginManager.collectPlugins(pluginPackages);
    final PluginManager levelPlugins = new PluginManager(Level.CATEGORY);
    levelPlugins.collectPlugins(pluginPackages);
    final Map<String, PluginType<?>> plugins = levelPlugins.getPlugins();
    if (plugins != null) {
        for (final PluginType<?> type : plugins.values()) {
            try {
                // Cause the class to be initialized if it isn't already.
                Loader.initializeClass(type.getPluginClass().getName(), type.getPluginClass().getClassLoader());
            } catch (final Exception e) {
                LOGGER.error("Unable to initialize {} due to {}", type.getPluginClass().getName(), e.getClass().getSimpleName(), e);
            }
        }
    }
    setup();
    setupAdvertisement();
    doConfigure();
    setState(State.INITIALIZED);
    LOGGER.debug("Configuration {} initialized", this);
}
Also used : PluginManager(org.apache.logging.log4j.core.config.plugins.util.PluginManager) ScriptManager(org.apache.logging.log4j.core.script.ScriptManager) PluginType(org.apache.logging.log4j.core.config.plugins.util.PluginType) IOException(java.io.IOException)

Example 2 with ScriptManager

use of org.apache.logging.log4j.core.script.ScriptManager in project logging-log4j2 by apache.

the class Routes method getPattern.

/**
     * Returns the pattern.
     * @param event The log event passed to the script (if there is a script.)
     * @param scriptStaticVariables The script's static variables.
     * @return the pattern.
     */
public String getPattern(final LogEvent event, final ConcurrentMap<Object, Object> scriptStaticVariables) {
    if (patternScript != null) {
        final ScriptManager scriptManager = configuration.getScriptManager();
        final Bindings bindings = scriptManager.createBindings(patternScript);
        bindings.put(STATIC_VARIABLES_KEY, scriptStaticVariables);
        bindings.put(LOG_EVENT_KEY, event);
        final Object object = scriptManager.execute(patternScript.getName(), bindings);
        bindings.remove(LOG_EVENT_KEY);
        return Objects.toString(object, null);
    }
    return pattern;
}
Also used : ScriptManager(org.apache.logging.log4j.core.script.ScriptManager) Bindings(javax.script.Bindings)

Example 3 with ScriptManager

use of org.apache.logging.log4j.core.script.ScriptManager in project logging-log4j2 by apache.

the class RoutingAppender method start.

@Override
public void start() {
    if (defaultRouteScript != null) {
        if (configuration == null) {
            error("No Configuration defined for RoutingAppender; required for Script element.");
        } else {
            final ScriptManager scriptManager = configuration.getScriptManager();
            scriptManager.addScript(defaultRouteScript);
            final Bindings bindings = scriptManager.createBindings(defaultRouteScript);
            bindings.put(STATIC_VARIABLES_KEY, scriptStaticVariables);
            final Object object = scriptManager.execute(defaultRouteScript.getName(), bindings);
            final Route route = routes.getRoute(Objects.toString(object, null));
            if (route != null) {
                defaultRoute = route;
            }
        }
    }
    // Register all the static routes.
    for (final Route route : routes.getRoutes()) {
        if (route.getAppenderRef() != null) {
            final Appender appender = configuration.getAppender(route.getAppenderRef());
            if (appender != null) {
                final String key = route == defaultRoute ? DEFAULT_KEY : route.getKey();
                appenders.put(key, new AppenderControl(appender, null, null));
            } else {
                error("Appender " + route.getAppenderRef() + " cannot be located. Route ignored");
            }
        }
    }
    super.start();
}
Also used : AbstractAppender(org.apache.logging.log4j.core.appender.AbstractAppender) Appender(org.apache.logging.log4j.core.Appender) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) ScriptManager(org.apache.logging.log4j.core.script.ScriptManager) Bindings(javax.script.Bindings)

Aggregations

ScriptManager (org.apache.logging.log4j.core.script.ScriptManager)3 Bindings (javax.script.Bindings)2 IOException (java.io.IOException)1 Appender (org.apache.logging.log4j.core.Appender)1 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)1 AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)1 PluginManager (org.apache.logging.log4j.core.config.plugins.util.PluginManager)1 PluginType (org.apache.logging.log4j.core.config.plugins.util.PluginType)1