OH3 - Is it possible to enable/disable DEBUG logging via a Switch item?

Here’s the thing configuration I use for this for the zwave logger:

UID: http:url:OhLogAPI
label: OH Log Api
thingTypeUID: http:url
configuration:
  authMode: BASIC_PREEMPTIVE
  ignoreSSLErrors: false
  baseURL: http://localhost:9080/rest/logging/
  delay: 0
  stateMethod: GET
  refresh: 30
  commandMethod: PUT
  contentType: application/json
  timeout: 3000
  bufferSize: 2048
  username: API_TOKEN_GOES_HERE
channels:
  - id: Zwave
    channelTypeUID: http:string
    label: Zwave Log Level
    description: ""
    configuration:
      commandTransformation: 'REGEX:s/(.*)/\{"loggerName": "org.openhab.binding.zwave","level": $1"\}/'
      stateExtension: org.openhab.binding.zwave
      commandExtension: org.openhab.binding.zwave
      stateTransformation: JSONPATH:$.loggers[0].level

Note the three different places where you have to set the logger name (commandTransformation, commandExtension, and stateExtension). You could add the logger name to the base url instead but then you would need a new thing for each logger. This way you just use one thing and each different logger is just a channel.

Also, as Jan said, you need an API token for authentication so make sure to create one first and put it in the username field.

As for the channel, the state half of the channel returns a JSON, so using the JSONPATH transform to extract the log level is trivial. The only slightly tricky part is that the command (Put) call expects a JSON payload. So there I use the REGEX transform to convert the single log level string into the appropriate JSON form. Now the channel receives a string and sends a JSON based on a string so I can just connect the channel to a string item with the following commandOptions:

OFF=Off
ERROR=Error
WARN=Warn
INFO=Info
DEBUG=Debug
5 Likes