Logger Implementation Slf4j with varargs

When i use slf4j in javascript in OH3 like this var log = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.lights_on");
I cannot use var args like this log.info("foo: {}, bar: {}, blub: {}", "1", "2", "3");
Just 2 Objects are allowed as parameters. Is it possible to change the implementation or what is the best way to deal with it?
Furthermore is it possible to implement a shortcut like logInfo(“logger tag”, “test with params”, “a lot of args”, …) as in dsl rules?

Thanks

Possibly the issue here is Nashorn failing to wrap the args into an array to locate the correct Java method. You may want to try doing that manually, e.g.:

log.info("foo: {}, bar: {}, blub: {}", ["1", "2", "3"]);

(Untested, just a guess!)

You can use the logInfo openHAB Actions directly.

var Log = Java.type("org.openhab.core.model.script.actions.Log");
Log.logError("Experiments", "This is an OH error log");
Log.logWarn("Experiments", "This is an OH warn log");
Log.logInfo("Experiments", "This is an OH info log");
Log.logDebug("Experiments", "This is an OH debug log");

I can’t suggest anything concerning the multiple arguments. I’d definitely trust jpg0 on that part.

And if you wanted to, you could save the methods to local variables…

var logInfo = Log.logInfo

You could put that into a library that you import and use in all of your rules.

1 Like

The info, debug, error, etc. methods of SLF4Js logger object accepts one argument, two arguments, or more arguments passed as an Object. So…

var LOG = Java.type("org.slf4j.LoggerFactory").getLogger("javascript.TEST");

LOG.warn("Test: {}", 1);
LOG.warn("Test: {}, {}", 1, 2);
LOG.warn("Test: {}, {}, {}", [1, 2, 3]);

More details here…

… and in the javadoc…

The LogAction (Log in OH3) actions also use SLF4J, but they take care of the multiple arguments for you. As a side note, this is also taken care of when using core.log from the Jython helper libraries. This is also addressed in an upcoming major revision of the JS helper libraries.

Wrapping the arguments in an array is working, but it’s a litte overhead.
With @rlkoshak suggest, varargs are possible. I will use this, thanks.
Maybe a little offtopic: Is it possible to use varargs in methods in the used js version? I don’t get it.