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

rules
karaf
logging
Tags: #<Tag:0x00007f6cefb0a438> #<Tag:0x00007f6cefb09cb8> #<Tag:0x00007f6cefb09920>

(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