Set Log-Level for OH3.4

JS Scripting allows for different Log-Outputs:

console.info
console.debug
console....

How do I bring OH3 into the respective Log-Level?

Use Case:

  • Especially in testing of new rules, I’d like to insert bazillion of log-outpus, which are helpful for debugging
  • I don’t want to comment each and every log out, if I’m ready
  • also: if I return to a rule, because I’d like to add something new or fix a bug, I don’t want to un-comment each of those “debug-related” outputs

First, for the logging level of bindings you can set the level by navigating to Settings → Bindings → Binding Name and click the little gear icon in the upper right or through the REST API or through the Karaf Console, or by editing log4j2.xml.

Unfortunately the REST API doesn’t support changing any other logger’s logging level so you’ll have to change those using the Karaf console, or by editing log4j2.xml and adding a new Logger to the bottom and setting the level there.

JS Scripting also supports setting the logging level in the rule itself.

osgi.getService('org.apache.karaf.log.core.LogService').setLevel(console.loggerName, 'INFO');

To set the logger name in a given JS Scripting script:

console.loggerName = 'org.openhab.automation.rules_tools.MQTTEventBus';

To set the logger’s level in the Karaf Console or by editing the log4j2.xml file you’ll need to know the name of the logger you want to adjust.

Note: in JS Scripting console.trace will also log out a tiny stack trace of the call stack up to that point, so don’t treat it as simply a level below DEBUG, it has another purpose.

oh, that’s cool, didn’t know that - I always went right into the Karaf Console.

But - what I really meant was just to add some Rules-Logging:

some
console.info("here some info values: " + INFOVARIABLE);
rule
console.debug("here some debug values: " + DEBUGVARIABLE);
code
...

What I want is to leave the rule “as is”, but print the console.debug values only to the logs, if I want to really debug my rules, not 24/7.
But I think I found it here:

log:set debug org.openhab.core.model.script should do it then.
But: Is there a JS Scripting equivalent for logging a “subpackage” in DLS with logError("heating", "This is a log entry of type Error!"), meaning I could set only the “heating” logs to e.g. DEBUG?

Read my full reply. Everything after the first paragraph is about logging in rules. Logging level is controlled by logger name and I show how to change the name of the logger used in a given rule.

1 Like

Sorry, I missed that. Thought org.openhab.core.model.script would change the logLevel already as it it already in my log4j2.xml.
I know nothing about log4j, so I just add a line after the last one?

                <Logger level="INFO" name="org.openhab.binding.modbus"/>
                <Logger level="INFO" name="org.openhab.core.model.script"/>
=> newEntry:    <Logger level="INFO" name="org.openhab.automation.rules_tools.myRulesName"/>
        </Loggers>

</Configuration>

Then I can set the LogLevel of that logger in the Karaf Console or inside the rule itself? Log4j 2 automatically reloads the log4j2.xml - at least Apacha says so:
https://logging.apache.org/log4j/log4j-2.3/

But I still get:

2023-02-15 16:16:49.946 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'EMS_HauptProgramm' failed: org.graalvm.polyglot.PolyglotException: TypeError: (intermediate value).myRulesName is not a function

If you add a new entry to log4j2.xml you are done. The level is changed. The level is set by that “level” property in the Logger statement.

That’s something different. You’ve a syntax error in your rule. Show the code.

it’s just:

console.myRulesName("a logentry");

Where did you get that syntax?

console.loggerName = 'org.openhab.automation.rules_tools.MQTTEventBus';

That gives the logger it’s name. In this case, on the Karaf Console, or in the log4j2.xml file that’s the name I’d use for the “name” of the logger.

But that doesn’t change how console works. You still use console.info("blah blah blah");, console.debug("blah blah blah"); and so on.

And I cannot stress this enough. Any time you have a question about how to do something in JS Scripting, review the JS Scripting’s add-on docs. They are astonishingly complete and comprehensive. The answer to almost every question will be there. In this particular case: JavaScript Scripting - Automation | openHAB

1 Like

THAT’s the one! Thanks!

in Logging | openHAB it says

log:set debug org.openhab.core.model.script

and in JavaScript Scripting - Automation | openHAB it says

log:set DEBUG org.openhab.automation.script

and with that set, I get iwith the following console-commands:

console.info("a INFO logentry");
console.debug("a DEGUB logentry");
console.info("a INFO logentry again");
2023-02-15 17:23:52.070 [INFO ] [tomation.script.ui.TestRule] - a INFO logentry
2023-02-15 17:23:52.071 [DEBUG] [tomation.script.ui.TestRule] - a DEBUG logentry
2023-02-15 17:23:52.073 [INFO ] [tomation.script.ui.TestRule] - a INFO logentry again

and of course the DEBUG logentry is gone, if I set log:set INFO org.openhab.automation.script in e.g. the console

I’m on OH 3.4.2 but do not see a gear icon. Is it a later addition?

I’m pretty sure it was included in 3.4 but maybe not.

The gear icon might have been introduced earlier for binding configs, but setting log levels was just recently merged into 4.0

1 Like

OK thanks, that’s why I don’t have it.

And does PUT/logging/{loggerName} using API Explorer also not work for 3.4.2?

If I put in org.openhab.binding.zwave for the loggerName and a body of

{
“loggerName”: “org.openhab.binding.zwave”,
“level”: “debug”
}

I get a 400 Error: Bad request - Payload is invalid

I’ve also tried several other loggerNames but with the same response.