Formatting log statements

I have a log statement in a javascript script action that works fine:

logger.info("RsAutoDayNightRule: E_Day:" + eastDay + ", S_Day:" + southDay + ", W_Day:" + westDay + ", E_Dark:" + eastDark + ", S_Dark:" + southDark + ", W_Dark:" + westDark + ", LastCommand:" + this.LastCommandRsAutoDayNight);

Now I wanted to get rid of all the + operators, but I can’t find a working solution.
All these do NOT work:

logger.info("RsAutoDayNightRule: E_Day:{}, S_Day:{}, W_Day:{}, E_Dark:{}, S_Dark:{}, W_Dark:{}, LastCommand:{}", eastDay, southDay, westDay, eastDark, southDark, westDark, this.LastCommandRsAutoDayNight);

logger.info("RsAutoDayNightRule: E_Day:[{}], S_Day:[{}], W_Day:[{}], E_Dark:[{}], S_Dark:[{}], W_Dark:[{}], LastCommand:[{}]", eastDay, southDay, westDay, eastDark, southDark, westDark, this.LastCommandRsAutoDayNight);

logger.info("RsAutoDayNightRule: E_Day:[{0}], S_Day:[{1}], W_Day:[{2}], E_Dark:[{3}], S_Dark:[{4}], W_Dark:[{5}], LastCommand:[{6}]", eastDay, southDay, westDay, eastDark, southDark, westDark, this.LastCommandRsAutoDayNight);

It always gives the error:

TypeError: Can not invoke method [jdk.dynalink.beans.OverloadedDynamicMethod
 void org.ops4j.pax.logging.slf4j.Slf4jLogger.info(Marker,String,Object,Object)
 void org.ops4j.pax.logging.slf4j.Slf4jLogger.info(Marker,String,Object)
 void org.ops4j.pax.logging.slf4j.Slf4jLogger.info(Marker,String,Object[])
 void org.ops4j.pax.logging.slf4j.Slf4jLogger.info(Marker,String,Throwable)
 void org.ops4j.pax.logging.slf4j.Slf4jLogger.info(Marker,String)
 void org.ops4j.pax.logging.slf4j.Slf4jLogger.info(String,Object,Object)
 void org.ops4j.pax.logging.slf4j.Slf4jLogger.info(String,Object)
 void org.ops4j.pax.logging.slf4j.Slf4jLogger.info(String,Object[])
 void org.ops4j.pax.logging.slf4j.Slf4jLogger.info(String,Throwable)
 void org.ops4j.pax.logging.slf4j.Slf4jLogger.info(String)
] with the passed arguments; they do not match any of its method signatures. in <eval> at line number 23

Any help appreciated!

The documentation at SLF4J Manual
gives an example that works for me

logger.debug("Temperature set to {}. Old temperature was {}.", t, oldT);

But it does not work in my case with 7 arguments :frowning:

Reading further, it should also work with 3 or more parameters:
http://www.slf4j.org/faq.html#logging_performance

If three or more arguments need to be passed, you can make use of the Object... variant of the printing methods. For example, you can write:

logger.debug("Value {} was inserted between {} and {}.", newVal, below, above);

However, this example does not work in OH3!
But according to the error message, the overload with object[] exists:
void org.ops4j.pax.logging.slf4j.Slf4jLogger.info(Marker,String,Object[])

Do I have to explicitly create an object array?

i had the same problem an until now i prepare my string via java.util.Formatter

// Formatter Tests
// https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html
var JavaUtilFormatter = Java.type("java.util.Formatter");

var sFormatter = new JavaUtilFormatter();
// create my string
var sOut = sFormatter.format("--%s--%d--%2.2f--%s--","Ein String", 13, 56.789012356, sTest);
// log my string
logger.info("{} : {}", 'sOut ', sOut); 
1 Like

Creating an object array explicitly and passing that to the logger does work :slight_smile:

var args = [eastDay, southDay, westDay, eastDark, southDark, westDark, this.LastCommandRsAutoDayNight];
logger.info("RsAutoDayNightRule: E_Day:{}, S_Day:{}, W_Day:{}, E_Dark:{}, S_Dark:{}, W_Dark:{}, LastCommand:{}", args);

the array method join will also work.

logger.info(["RsAutoDayNightRule: E_Day:", eastDay, ", S_Day:", southDay, ", W_Day:", westDay, ", E_Dark:", eastDark, ", S_Dark:", southDark, ", W_Dark:", westDark, ", LastCommand:", this.LastCommandRsAutoDayNight].join(""));
1 Like