JSR Jython Logs

I have been using Jython for some time now and it is great!

The problem I am struggling though are the error messages.
The messages on the normal console are not useful because they relate to java and not to Jython.
The workaround I am using is the “try:” construct. e.g.

         os.rename(fn, new_name)  # error
        oh.logError("TestRule", traceback.format_exc())

But this approach is inconvienient because it requires alteration of the code.
Is there a better way?
A console output for jython for example.

I don’t think there is a better way. Ultimately the Jython is running on Java so if you don’t catch the exceptions yourself like the above it will fall into Java code and print the stack trace accordingly.

You could possibly create a class or a function and annotations to automatically add a try/except to your rules. I am by no means an expert, but you can see an example of how I adopted this approach to handle signals (e.g. CTRL-c) in one of my scripts. See the check function in sensorReporter.py for the annotation and signalProc.py for the handling code.

Hi Rich,
Thanks for the reply and also the link to your python code which for sure will serve as a reference for quality coding and which I will start to study.

I use a class decorator which automatically wraps the execute method with a method decorator (which can also be used independently for other functions) that does the traceback logging. The class decorator also creates a class-specific logger, adds a method for notification support (only supports Pushover, for now) and causes the rule class to inherit from the openHAB Rule class. The class decorator is used like in the following snippet…

import openhab

class MyRule(object):
   # the rule definition ...

You can take a look at the decorator code at:


1 Like

Hi Steve,

Looks interesting. But I guess my jython understanding is not good enough to know how to integrate this code into my jython rules.
For the time being I would require a kind of step by step explaination where to put this code and also what remains to be done in my jython rules.
If you find time to describe this a bit further, this would be great.

Did you take a look at the associated wiki (linked to in the README.md)? If so, I can try to answer some specific questions about those instructions (and/or update them to be more clear).

Hi Steve,

I have read the wiki and tried to follow the instructions.

  1. I copied the openhab directory into configuration directory containing the 7 python files
  2. I have added the “import openhab” line and the “@openhab.rule”. But when doing so, I receive a jython script error.
  3. Additionally I have restarted openhab.

I think the import statement cannot find the openhab directory in the configuration folder

I have checked my startup file and discovered that -Dpython.path = was set to "-Dpython.path=“configurations/scripts/lib” and that the directory “lib” did not exist.
I have no clue why this was done this way. When removing “lib”, the script error is done.

the only change is the Info stating:
EngineName: jython
compiler: discarding redundant interface: org.openhab.core.jsr223.internal.shared.Rule

But It is working now. I am receiving helpfull error messages.
Thanks for the great support!

I’m guessing that when you used the class decorator (@openhab.rule) you didn’t change the Rule base to object from your rule class. I can update the decorator to detect this situation, but if you change…

class MyRule(Rule):
   # rule defn


class MyRule(object):
   # rule defn

that should eliminate the duplicate interface warning. The warning is given because the decorator adds the Rule interface automatically already.

hi Steve,
Thanks. No problem. I can change easily from Rule to object.

Hi @steve1,
is there any way to suppress the log entry regarding the cron job in jython?

I mean this message:

2016-06-18 19:39:04.002 [INFO ] [.o.c.j.i.engine.TimeTriggerJob] - TimeTrigger for rule: org.python.proxies.__builtin__$PresenceCheckmanager$16@18711d
0, scriptName: jsr223_PresenceCheckManager.py 

Thank you in advance!

I have the following in my logback.xml file to set the logging level of the TimeTriggerJob logging category to WARN.

<logger name="org.openhab.core.jsr223.internal.engine.TimeTriggerJob" level="WARN"/>