[Solved] Logging numbers in rules does not work as expected

Hi OpenHAB users,

I’m trying to log numbers in my rules into the log file, but it seems to be
not working as expected. Inspired by the documentation ([1]), I’ve created
the following rule:

rule "MyRule1"                                                                                   
when                                                                                                     
  Item Bedroom_SetTemp received update                                                                   
then                                                                                                     
  var Number foo = 1.1                                                                                   
  logWarn("Foo", "foo: %1$", foo)
  logWarn("Foo", "foo: %1$.1f", foo)
  logWarn("Foo", "foo: %1$ bla", 1.1)
  logWarn("Foo", "foo: %1$.1f bla", 1.1)
end

When the rule fires up, then I see the following output:

2018-02-09 19:20:27.284 [WARN ] [g.eclipse.smarthome.model.script.Foo] - foo: %1$
2018-02-09 19:20:27.287 [WARN ] [g.eclipse.smarthome.model.script.Foo] - foo: %1$.1f
2018-02-09 19:20:27.290 [WARN ] [g.eclipse.smarthome.model.script.Foo] - foo: %1$ bla
2018-02-09 19:20:27.293 [WARN ] [g.eclipse.smarthome.model.script.Foo] - foo: %1$.1f bla

However, I would expect the following output:

2018-02-09 19:20:27.284 [WARN ] [g.eclipse.smarthome.model.script.Foo] - foo: 1
2018-02-09 19:20:27.287 [WARN ] [g.eclipse.smarthome.model.script.Foo] - foo: 1.1
2018-02-09 19:20:27.290 [WARN ] [g.eclipse.smarthome.model.script.Foo] - foo: 1 bla
2018-02-09 19:20:27.293 [WARN ] [g.eclipse.smarthome.model.script.Foo] - foo: 1.1 bla

I’m running OpenHAB (installed through the provided package repository) on a Pine64 with Fedora 27 (aarch64) with the following java version (part of Fedora):

openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)

I hope the answer to this will not be a very embarrassing typo or so…

[1] https://docs.openhab.org/administration/logging.html#create-log-entries-in-rules

This is the second post I’ve seen similar to this. I don’t know if something changed recently or what is going on. But I can say that a work around is:

  var Number foo = 1.1                                                                                   
  logWarn("Foo", String::format("foo: %1$", foo))
  logWarn("Foo", String::format("foo: %1$.1f", foo))
  logWarn("Foo", String::format("foo: %1$ bla", 1.1))
  logWarn("Foo", String::format("foo: %1$.1f bla", 1.1))

Thanks a lot, that’s a working workaround!

After reading some more docs, I found out that there are two documented ways to log numbers.
The not working one from the link above and a working one from here: [1]

So in case you are struggling with logging as well, then instead of

  logWarn("Foo", "foo: %1$.1f", foo)

use the following logging style:

  logWarn("Foo", "foo: {}", foo)

Seems like OpenHAB changed the Logging API semantics without renaming the log functions.

[1] https://docs.openhab.org/addons/actions.html#logging-actions