Change logging level through a UI and access Karaf in a rule

rules
karaf
logging
Tags: #<Tag:0x00007f18293e5e98> #<Tag:0x00007f18293e5c18> #<Tag:0x00007f18293e5a60>

(Scott Rushworth) #1

Here is an example of how logging levels can be changed without having to manually enter commands into the Karaf console or editing org.ops4j.pax.logging.cfg. I set this up and it is working in my environment, but the ssh command will need to be modified to work for someone running openHabian or if OH is being run on Windows. The key based authentication is the trickiest bit to setup. Be sure to configure Karaf with the public key and then ssh in manually first to accept the host’s public key. What you see below is simple… but the setup can be daunting. :wink:

ITEM

String  LogLevel_Rules    "Logging Level: Rules [%s]"   <text>
String  LogLevel_Zwave    "Logging Level: Zwave [%s]"   <text>
String  LogLevel_Zigbee   "Logging Level: Zigbee [%s]"  <text>

SITEMAP

Selection item=LogLevel_Rules  mappings=["INFO"="INFO","DEBUG"="DEBUG"]
Selection item=LogLevel_Zwave  mappings=["INFO"="INFO","DEBUG"="DEBUG"]
Selection item=LogLevel_Zigbee mappings=["INFO"="INFO","DEBUG"="DEBUG"]

RULE

rule "Set log level"
when 
    Item LogLevel_Rules received command
    or
    Item LogLevel_Zwave received command
    or
    Item LogLevel_Zigbee received command
then
    val String commandPrefix = "/bin/bash@@-c@@/usr/bin/ssh openhab@localhost -p 8101 log:set "
    //val String commandPrefix = "/bin/bash@@-c@@/usr/bin/ssh openhabian@localhost -p 8101 log:set "// this might work for openHabian?
    switch (triggeringItem.getName) {
        case "Virtual_String_1"  : executeCommandLine(commandPrefix + receivedCommand + " org.eclipse.smarthome.model.script.Rules"),
        case "LogLevel_Zwave"  : executeCommandLine(commandPrefix + receivedCommand + " org.openhab.binding.zwave"),
        case "LogLevel_Zigbee" : {
            executeCommandLine(commandPrefix + receivedCommand + " org.openhab.binding.zigbee")
            executeCommandLine(commandPrefix + receivedCommand + " com.zsmartsystems.zigbee")
        }
    }
    logInfo("Rules", "Set log level: {} set to {}",triggeringItem.getName,receivedCommand)
end

Calculate periodic consumption of gas based on meter readings
(E. Gerland) #2

Thanks.
This was one topic I had on my list since the first OH2 stable.
Thanks for sharing!


(E. Gerland) #3

Actually I had successfully connect to karaf by key authentication and wrote a tutorial about it here:


(E. Gerland) #4

I did not find a command to set globally the loglevel to Error.
Isn’t there such option?


(Scott Rushworth) #5

Globally, as in for all of the appenders? Not that I am aware of.


(E. Gerland) #6

I try to figure out where my CPU load comes from and I have concerns that it’s from logging!?
(Raspi 3)

When changing the global logfile org.ops4j.pax.logging.cfg
Does this require a restart of OH?


(Scott Rushworth) #7

No, it’s immediate. Doing a log:set [blah] modifies this file. I find VS Code (with LSP enabled) puts a good bit of load on my server, and sometimes shutting it down does not help, but an OH restart clears it up.


(Angelos) #8

I think that the first line will work for both since you are logging into Karaf console and the username there is openhab.

I think that the Linux username is different in openHABian versus “standard” Raspbian (pi vs openhabian)


(Scott Rushworth) #9

I thought openHABian used a different set of credentials (I’ve never used it). From here…

Connect to your Raspberry Pi SSH console using the username openhabian and password openhabian.

(Angelos) #10

I used it also some time ago (when it was started), but not currently.

I think that this doc entry refers only to the O/S. The console docs entry is common for all O/S.

The username being used is: https://docs.openhab.org/administration/console.html#changing-the-password