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);
}
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;
}
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();
}
Aggregations