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

  • Platform information:
    • Hardware: Raspberry Pi Model 3B+
    • OS: Linux devpi 5.10.103-v7+ #1529 SMP Tue Mar 8 12:21:37 GMT 2022 armv7l GNU/Linux
    • Java Runtime Environment: OpenJDK Runtime Environment Zulu11.54+25-CA (build 11.0.14.1+1-LTS)
    • openHAB version: 3.2.0 - Release Build

I am able to successfully enable/disable DEBUG logging using the openhab-cli console.

Is it possible to assign this functionality to a Switch item so that an end-user can enable/disable extra logging?

I assume that the openhab-cli password prompt may be an issue.

1 Like

Have a look at the exec binding to issue she’ll commands

There are some PRs that I think are not yet merged that will let you set the logging level of openhab core as well as bindings through MainUI. Until that happens, you’ll have to use the Exec binding or executeCommandLine to issue the command.

Thanks for the replies.

Is it possible to supply openhab-cli with the password on the command line? Otherwise, I assume the exec binding will hang on the password request. Of course, this will expose a security hole but I don’t see another way.

Core support for that is already added, so you can use the REST API (e.g. via the HTTP binding). Only the UI support is missing.

3 Likes

That is a feature that i am looking forward to very much. If the logging level setting also does individual levels on scripts then it would be truly awesome

Thanks @J-N-K for the info.

I have been searching for info/examples on how to use the http binding to change the logging level but can’t find anything.

Are you able to please point me to an example or tutorial?

Thanks.

Have not done it, but in latest Milestones there is now a API explorer that guides you through what is possible with the API. This is found in the developer tools menu.

I don’t have a tutorial or example for that. But if you look in the API Explorer (Administration / Developer Tools / API Explorer) you can scroll down to /logging and see what is possible and what the correct format is. If you click on “Try it out / Execute”, you also see the corresponding curl command, that should be easily adaptable to the HTTP binding. Keep in mind that you need authentication for that, so either enable Basic Authentication or create an API Token.

1 Like

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

I have been searching for these PR (on openhab-webui) but could not find them.
Can you post the link to the PRs you were reffering to?

I did not look it up but it is merged since a while and you can find it here:

thanks, that is interesting to know.

Wonder why this possibility is not even mentioned here: Logging | openHAB

1 Like

You have the possibility to scroll down on that page and click on the edit link and add complete the documentation.

Caught a mistake or want to contribute to the documentation? Edit this page on GitHub

1 Like