Java PropertyWrapperForScripts-class And Method Code Example


The PropertyWrapperForScripts class from the logback.core package can be used to create a wrapper around a property map that can be used by a script engine to access properties in a Logback configuration.

Here is an example of how the PropertyWrapperForScripts class could be used to create a wrapper around a property map:

import ch.qos.logback.core.script.ScriptEvaluator;
import ch.qos.logback.core.script.ScriptTypes;
import ch.qos.logback.core.util.PropertyWrapperForScripts;

//...
Map<String, Object> propertyMap = new HashMap<>();
propertyMap.put("host", "localhost");
propertyMap.put("port", 8080);

PropertyWrapperForScripts wrapper = new PropertyWrapperForScripts(propertyMap);
ScriptEvaluator evaluator = new ScriptEvaluator(ScriptTypes.GROOVY, wrapper, null);

String script = "host + ':' + port";
String result = evaluator.evaluate(script);

// result is "localhost:8080"

In this example, the PropertyWrapperForScripts class is used to create a wrapper around a map of properties and passed it to a ScriptEvaluator object, which is then used to evaluate a script.

Here the script is host + ':' + port, when evaluated it will return "localhost:8080".

The PropertyWrapperForScripts is a utility class that allows script engines to access properties stored in a map. By wrapping the property map in a PropertyWrapperForScripts object, the script engine can access the properties as if they were properties of the object.

It supports different types of script engines like groovy, javascript etc.. based on the scriptType passed while creating ScriptEvaluator object.

It's up to the developer to choose when and how to use this class, which can make your logback configuration more powerful and flexible by allowing you to compute values of properties at runtime.