Python logging question

Hi,

When I run this python test in OH 5.1.1:

from openhab import logger

logger.info("logger is used")
print("print is used")

the result is this:

2026-01-09 15:23:00.013 [INFO ] [g.openhab.automation.pythonscripting] - logger is used
2026-01-09 15:23:00.013 [INFO ] [utomation.pythonscripting.71b019046e] - print is used

Is it not supposed to add the rule ID after “pythonscripting” in both methods?

I think (not sure) that it was like that in 5.0.

Best Regards, Valter

Never mind,

Using this instead:

from openhab.actions import Log
def log_info(log_text): 
  Log.logInfo(ctx["ruleUID"], str(log_text))
  
def log_warn(log_text): 
  Log.logWarn(ctx["ruleUID"], str(log_text))

def log_debug(log_text): 
  Log.logDebug(ctx["ruleUID"], str(log_text))

def log_error(log_text): 
  Log.logError(ctx["ruleUID"], str(log_text))

Best Regards, Valter

@v61 you are right!

Looks like it happens only for UI based rules. I will check!

@v61

The reason was that the prefix was generated when the module was imported.

With file-based rules, this happens when the rule is loaded and directly executed. This works as expected. However, with UI-based rules, the rule is precompiled or cached. At that point, the log prefix was already generated, even though the rule ID hadn’t yet been determined.

I’ve now implemented this in a more “lazy” way. This means the prefix is ​​generated the first time the logging object is used. This should fix the error.

If you want to test this, simply update to the latest helper library version.

The easiest way to do this is via the openHAB CLI. Just enter pythonscripting update install latest.

Hi,

It works!

Best Regards, Valter