Formatting of log entries

In the docs there is an example of formatting items in log messages:

logDebug("heating-control.rules", "Bedroom: Temperature: %1$.1f°C, Mode %2$s", Bedroom_Temp.state, Bedroom_Heater_Mode.state)

If I try this, only the format strings are printed in the message, but not the values:

logInfo("SmartSmartMeter.rules", "Water Meter updated rule - Cur: %1$d - Last: %2$d  - 15min:  %3$.2fl - 1h: %4$.2fl - Day: %5$.2fl - Month: %6$.2fl - Year: %7$.2fl", WaterCurrent, WaterLast, WaterMeter_15min.state, WaterMeter_1h.state, WaterMeter_24h.state, WaterMeter_Month.state, WaterMeter_Year.state)

I double checked my rule, but can not find the error. Is there anything I am missing?

Thanks for your help!

You might need to cast the state of your Items to Number.

WaterMeter_15min.state as Number, WaterMeter_1h.state as Number,

I’m assuming WaterCurrent, and WaterLast are variables and not Items. If not you need .state on those as well.

No matter what I try, I don’t manage to print a variable to logging.

I made this simple rule:

rule "Variable logging"
    Time cron "0/15 * * * * ?"   // every 15 seconds
	var Number myNumber = 10.01
	logDebug("hvd","My number: %1$.2f", myNumber)

I tried variations like

logDebug("hvd","My number: %1$.2f", myNumber.floatValue)

The logging keeps printing

2018-02-07 12:04:45.145 [DEBUG] [g.eclipse.smarthome.model.script.hvd] - My number: %1$.2f

Your formatting looks the same as the examples in the docs:

I can’t say why it is not working. I don’t really use logs like that so have no direct experience to draw from.

You can try:

logDebug("hvd", String::format("My number: %1$.2f", myNumber))
1 Like

Works like a charm! I hadn’t thought about passing the whole logtext as one string to the logDebug() call.

Thank you very much, @rlkoshak

In case anyone is stumbling over this thread, one can also use the following:

logDebug("hvd", "My number: {}", myNumber)
1 Like

The way it’s documented it is definitely not working. I put up an issue with the documentation:

1 Like