Inject Logger into Script engine groovy

Hello,

I want to inject my Logger class into script engine, is it possible? Should work same like execution variable in groovy script.

I use camunda.spring.boot.starter.version 3.2.0

Thanks for help.

If you want to inject it, then you need to create /override you instance of the Camunda script engine to add you variables.

How do you want to use this logger? You can also just import your logger or have a static class you use.

Thank you for your answer. Which option is better overwrite or create? And about which class are you Talking?

I think you are better to just import / use a static getter such as the logger factory getLogger method.

1 Like

You can replace the default script environment with your own:

@Configuration
public class CamundaScriptConfig {

    @Autowired
    public CamundaScriptConfig(ProcessEngine processEngine) {
        ProcessEngineConfigurationImpl pc = (ProcessEngineConfigurationImpl) processEngine.getProcessEngineConfiguration();
        pc.setScriptingEnvironment(new LogScriptingEnvironment(pc));
    }
}

And attach your custom logger to it:

public class LogScriptingEnvironment extends ScriptingEnvironment {

    private static final Logger LOGGER = LoggerFactory.getLogger("CustomLogger");

    LogScriptingEnvironment(ProcessEngineConfigurationImpl config) {
        super(config.getScriptFactory(), config.getEnvScriptResolvers(), config.getScriptingEngines());
    }

    @Override
    public Object execute(ExecutableScript script, VariableScope scope, Bindings bindings, ScriptEngine scriptEngine) {
        bindings.put("log", LOGGER);
        return super.execute(script, scope, bindings, scriptEngine);
    }
}

Disclaimer: you have to cast to ProcessEngineConfigurationImpl, because setScriptingEnvironment is not part of the ProcessEngineConfiguration api.

1 Like