Java PropertyEvalScriptBuilder-class And Method Code Example


The PropertyEvalScriptBuilder class from the logback.core package can be used to create an evaluator script that can be used to evaluate properties in a Logback configuration.

Here is an example of how the PropertyEvalScriptBuilder class could be used to create a script that evaluates properties:

import ch.qos.logback.core.script.ScriptBuilder;
import ch.qos.logback.core.script.ScriptBuilderFactory;
import ch.qos.logback.core.script.ScriptEvaluator;
import ch.qos.logback.core.script.ScriptTypes;

// ...

ScriptBuilder builder = new PropertyEvalScriptBuilder();
ScriptEvaluator evaluator = builder.build(ScriptTypes.GROOVY);

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

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

// result is "localhost:8080"

In this example, the PropertyEvalScriptBuilder class is used to create a new script builder, which is then used to create a new ScriptEvaluator object.

The map containing properties is passed to the evaluator , which can then be used to evaluate a script.

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

The PropertyEvalScriptBuilder allows you to use a script engine to evaluate properties in logback configuration. It supports different types of script engines like groovy, javascript etc.. based on the scriptType passed.

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.