0

I am trying to pass an slf4j Logger object to my Drools rule as a global.

In my calling Java class I define the logger and pass it to the rule via the setGlobal kie server command:

private static final Logger LOGGER = LoggerFactory.getLogger(KieServerTester.class);

Command<?> setGlobalCommand = commandsFactory.newSetGlobal("logger", LOGGER);
    commands.add(setGlobalCommand);

And in my rule file I have the global defined as:

global org.slf4j.Logger logger;

However when I execute the command I get this error:

ERROR [org.kie.server.services.drools.DroolsKieContainerCommandServiceImpl] (default task-1) Error calling container 'mycontainer': java.lang.RuntimeException: Illegal class for global. Expected [org.slf4j.Logger], found [java.util.LinkedHashMap].

I have no idea where it's getting LinkedHashMap from. I have stepped through the calling code but can't see where the map appears.

If I change my global in the rule file to be a LinkedHashMap, I don't get the error. So somehow it looks like my logger object in the Java class is being converted to a LinkedHashMap.

Any help appreciated, thanks.

Neil
  • 413
  • 4
  • 22

1 Answers1

0

You can't just pass whatever object you want to a Kie-Server. I assume you are using REST to communicate with the Kie-Server, and in that case, objects have to be serialized at your end and deserialized at the Kie-Server's end. I guess that the Kie-Server can't deserialize the object you are passing, and it is just leaving it as a Map.

Hope it helps,

Esteban Aliverti
  • 6,259
  • 2
  • 19
  • 31
  • From what I have read slf4j loggers can be serialized (although they do not implement Serializable, so perhaps that is the issue). However I have read several sources showing that it should be possible to pass an sl4fj logger to rules. Is the issue here that I am using KIE server rather than setting the global via KIE session? – Neil Oct 07 '19 at 14:29
  • Do you have the Logger class in the Kie server classpath? – Esteban Aliverti Oct 08 '19 at 09:38