Is there an analogy of Log() function for Embedded Python to Log errors?
Hi developers!
Happy holidays!
In ObjectScript there is neat option to log errors - call Log() method of an exception intance, e.g.:
ClassMethod MyMethod() {
Try {
w1/0
}
Catch e {
do e.Log() // logging error in the app logwrite"the error happened: "_e.DisplayString()
}
}Is there anything like that for Embedded Python? e.g:
ClassMethod MyMethod() [Language = python] {
try:
print(1/0)
except ZeroDivisionError as e:
// how can I log e into IRIS App Error Log here?
print("Caught exception: " + str(e))
}Any ideas?
Comments
Found one option!
ClassMethod DivideByZeroPython() [ Language = python ]
{
import sample
import iris
try:
print("divide by zero=" + str(sample.dividezero(1)))
except ZeroDivisionError as e:
errobj=iris.cls("%Exception.General")._New(str(e),42)
a=errobj.Log()
print("Caught exception: " + str(e))
}This stores the error in Apps Log, but no stack (of course):

Any ideas how I can store at least the class and line of code in parameters? I can provide the name and location to %Exception.General class that will be stored in the App Log.
Are you looking for something like:
ClassMethod ExceptionTest() [ Language = python ]
{
import sys
import iris
import traceback
try:
print(1/0)
except ZeroDivisionError as e:
errobj=iris.cls("%Exception.General")._New(str(e),42)
stack_trace_list = traceback.format_tb(e.__traceback__)
if stack_trace_list:
last_instruction_line = stack_trace_list[-1].strip()
errobj.Location = last_instruction_line
a = errobj.Log()
}Which gives:
.png)
Very cool, @David Hockenbroch ! This is what I'm getting in my case:

To get specific details like the line number, class, and function name during an exception, you can use the traceback module to extract the "execution frame" from the error.
By incorporating this logic into your code, we can generate a structured error message in IRIS format. However the PYTHON EXCEPTION has stack information but not detailed error information
ClassMethod pyClsError() [ Language = python ]
{
import traceback
import iris
class MathOperations:
def divide_numbers(self):
try:
print(1/0)
except Exception as e:
tb = e.__traceback__
stack = traceback.extract_tb(tb)[-1]
name = f"<{type(e).__name__.upper()}>"
cls = self.__class__.__name__
loc = stack[2]
lineNo = stack[1]
data = f"{loc}+{lineNo}^{cls}"
errobj=iris.cls("%Exception.General")._New(name,2603,data)
a=errobj.Log()
print("Caught exception: " + str(e))
obj = MathOperations()
obj.divide_numbers()
}ClassMethod pyFuncError() [ Language = python ]
{
import traceback
import os
import iris
try:
print(1/0)
except Exception as e:
tb = e.__traceback__
last_frame = traceback.extract_tb(tb)[-1]
# 2. Extract specific parts
error_name = f"<{type(e).__name__.upper()}>" # e.g., <NAMEERROR>
line_no = last_frame.lineno # e.g., 6
func_name = last_frame.name # e.g., <module> or my_func
filename = os.path.basename(last_frame.filename).replace('.py', '')
iris_error = f"{func_name}+{line_no}^{filename}"
errobj=iris.cls("%Exception.General")._New(error_name,2603,iris_error)
a=errobj.Log()
}Application Error Log
.png)
Glad to hear that. Thank you!
Thanks for the PR with even more examples!
The new version has been released!
thank you @Ashok Kumar T @David Hockenbroch both!
Included your code into the new embedded python template release - here.
