openHAB version: 2.5.11
When I catch a an exception and log the error by my own, I don’t get a full stack trace, just the last line.
If I’m not handling the exception I get the full trace but my code broke.
I played also around with @log_traceback but this had no effect. May be, because I haven’t understood how it works.
A way would be to beautify the output from traceback.format_stack(), but I’m quite sure that there is a better way.
How can I achieve the logging of the full traceback?
from core.rules import rule
from core.triggers import when
from core.actions import LogAction
from time import sleep
import traceback
from core.log import logging, LOG_PREFIX
LOG = logging.getLogger(u"{}.Test".format(LOG_PREFIX))
def t1():
try:
raise NameError
except:
LOG.error(traceback.format_exc())
def t2():
t1()
try:
raise NameError
except:
LOG.error(traceback.format_exc())
LOG.error(traceback.format_stack())
def t3():
raise NameError
@rule("Test: Generisch")
@when("Item swi_Sys_Test changed to ON")
def TestRule(event):
TestRule.log.info("Test Rule getriggert")
LogAction.logInfo("Test: TestRule", "Test Rule getriggert")
# immer am Anfang, falls das Skript nicht zum Ende durchläuft...
sleep(1.5)
events.postUpdate("swi_Sys_Test", "OFF")
# eigentlicher Code
t1()
t2()
t3()
# Ende vom eigentlichen Code
TestRule.log.info("Test Rule beendet")
LogAction.logInfo("Test: TestRule", "Test Rule beendet")
return
The result is:
26-Dez-2020 16:16:38.962 [INFO ] [jsr223.jython.Test: Generisch ] - Test Rule getriggert
26-Dez-2020 16:16:40.605 [ERROR] [jsr223.jython.Test ] - Traceback (most recent call last):
File "<script>", line 14, in t1
NameError
26-Dez-2020 16:16:40.695 [ERROR] [jsr223.jython.Test ] - Traceback (most recent call last):
File "<script>", line 14, in t1
NameError
26-Dez-2020 16:16:40.737 [ERROR] [jsr223.jython.Test ] - Traceback (most recent call last):
File "<script>", line 23, in t2
NameError
26-Dez-2020 16:16:40.809 [ERROR] [jsr223.jython.Test ] - [' File "/etc/openhab2/automation/lib/python/core/rules.py", line 107, in execute\n self.callback(inputs.get(\'event\'))\n', ' File "/etc/openhab2/automation/lib/python/core/log.py", line 51, in wrapper\n return fn(*args, **kwargs)\n', ' File "<script>", line 46, in TestRule\n', ' File "<script>", line 26, in t2\n']
26-Dez-2020 16:16:40.879 [ERROR] [jsr223.jython.Test: Generisch ] - Traceback (most recent call last):
File "/etc/openhab2/automation/lib/python/core/log.py", line 51, in wrapper
return fn(*args, **kwargs)
File "<script>", line 47, in TestRule
File "<script>", line 29, in t3
NameError
26-Dez-2020 16:16:40.951 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule '02ff2c00-0ee1-4f3c-9cb2-e89b51847ba8': Fail to execute action: 1