# Printing stack trace to gateway logs from a try

**URL:** <https://forum.inductiveautomation.com/t/printing-stack-trace-to-gateway-logs-from-a-try/77776>\
**Category:** Ignition\
**Tags:** scripting, ignition81\
**Created:** [August 11, 2023, 8:15pm UTC](https://forum.inductiveautomation.com/t/printing-stack-trace-to-gateway-logs-from-a-try/77776 "2023-08-11T20:15:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AIO](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/aio/32/87549_2.png) [@AIO](https://forum.inductiveautomation.com/u/AIO)\
**Post date:** [August 11, 2023, 8:15pm UTC](https://forum.inductiveautomation.com/t/printing-stack-trace-to-gateway-logs-from-a-try/77776/1 "2023-08-11T20:15:55Z")

</div>

Hey folks,

We have a gateway script running stored procedures. If a stored procedure fails, we need to notify the users so they know to halt the process and review the data. However, by implementing a try, we lose out on the stack trace that would ordinarily print to the gateway logs.

Ideally, we allow the error to hit the logs naturally and we take action to alert the users. However it seems like we need to do both in our except in order to make it happen. Is there any way to print a complete stack trace from a try? Ideally, it would mimic the standard stack trace, with the option to expand for details rather than filling the log with a wall of text.

I've been working with support and right now we're at a bit of an impasse with the following.  
(This code is incomplete in that it's not in a logger, just showcasing what we have)

```auto
import java.lang.Throwable

tag = 123
db = "DB_Doesnt_Exist"

try:
    Custom.Function.Calling.Sproc(tag, db)

except java.lang.Throwable, e:
    #prints full stack trace and back trace to console.
    #This has all the information we want, but cannot seem to redirect to gateway logs
    e.printStackTrace()

    #print the general error mesage
    print e.getMessage()

    #print the stack trace line-by-line
    #does not contain back trace
    stack = e.getStackTrace()
    for trace in stack:
        print trace

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 11, 2023, 8:27pm UTC](https://forum.inductiveautomation.com/t/printing-stack-trace-to-gateway-logs-from-a-try/77776/2 "2023-08-11T20:27:53Z")

</div>

`system.util.getLogger("someLogger").error("some message", someException)`

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 11, 2023, 8:38pm UTC](https://forum.inductiveautomation.com/t/printing-stack-trace-to-gateway-logs-from-a-try/77776/3 "2023-08-11T20:38:32Z")

</div>

Fundamentally, the problem is that print doesn't go to the logging subsystem. It just goes out the JVM's standard output. (Which is connected to the console window in the designer and in Vision clients.)

Always use loggers. Use the logger methods that have a throwable argument to get a throwable's stack trace in to the log. If you need python traces, too, use the `PythonAsJavaException` wrapper class from [later.py](https://www.automation-pros.com/ignition/later.py) to make them acceptable to the logger methods.

---

<div class="post-metadata">

**Author:** ![AIO](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/aio/32/87549_2.png) [@AIO](https://forum.inductiveautomation.com/u/AIO)\
**Post date:** [August 11, 2023, 8:47pm UTC](https://forum.inductiveautomation.com/t/printing-stack-trace-to-gateway-logs-from-a-try/77776/4 "2023-08-11T20:47:09Z")

</div>

Thank you, both.

I just stumbled upon this solution while I was working through hail-mary options. The face-palm moment was audible in my office.

I think my confusion could have been dissapated much earlier in the process if I'd known that [system.util.logger](https://docs.inductiveautomation.com/display/DOC80/system.util.getLogger) could process throwables. The documentation does not indicate this anywhere, and I (and IA phone support) spent an inordinate amount of time trying to process a throwable into a string that I could pass to the logger.

🤦‍♂️

```auto
myLogger = system.util.getLogger("TestLogger")
try:
    Custom.Function.Calling.Sproc(tag, db)
except java.lang.Throwable, e:
    myLogger.error("Error Message", e)
```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 11, 2023, 8:49pm UTC](https://forum.inductiveautomation.com/t/printing-stack-trace-to-gateway-logs-from-a-try/77776/5 "2023-08-11T20:49:37Z")

</div>

The "returns" part of the documentation links to the javadoc for [LoggerEx](https://files.inductiveautomation.com/sdk/javadoc/ignition80/8.0.12/com/inductiveautomation/ignition/common/util/LoggerEx.html), which shows the rest of the available methods.

The current docs should be pointing to v8.1 javadoc, but whatever.

---

<div class="post-metadata">

**Author:** ![AIO](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/aio/32/87549_2.png) [@AIO](https://forum.inductiveautomation.com/u/AIO)\
**Post date:** [August 11, 2023, 8:51pm UTC](https://forum.inductiveautomation.com/t/printing-stack-trace-to-gateway-logs-from-a-try/77776/6 "2023-08-11T20:51:55Z")

</div>

Thanks, Phil.

I hadn't noticed that.

Still, I think it would be a nice example to add to the docs on that page. I worked with 2 IA reps, my client, and office mates today and none of us were aware of this capability. 🙂

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 11, 2023, 9:02pm UTC](https://forum.inductiveautomation.com/t/printing-stack-trace-to-gateway-logs-from-a-try/77776/7 "2023-08-11T21:02:08Z")

</div>

> [@pturmel](#):
>
> If you need python traces, too, use the `PythonAsJavaException` wrapper class from [later.py](https://www.automation-pros.com/ignition/later.py) to make them acceptable to the logger methods.

Tangent, but might be good to relax that requirement. I'll put it on the long list of ideas to explore. A "solution" to the split throwable types issue would be nice, though if Jython devs haven't come up with it it's probably not forthcoming.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 11, 2023, 9:06pm UTC](https://forum.inductiveautomation.com/t/printing-stack-trace-to-gateway-logs-from-a-try/77776/8 "2023-08-11T21:06:01Z")

</div>

Python and Java traceback information is just different enough that it is ugly to convert. As my implementation shows. The core of the issue is that PyException must be a subclass of PyObject, and other PyObjects cannot be subclasses of Throwable. Noodle on that. (Multiple inheritance for the win!)

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 12, 2023, 8:31am UTC](https://forum.inductiveautomation.com/t/printing-stack-trace-to-gateway-logs-from-a-try/77776/9 "2023-08-12T08:31:24Z")

</div>

I _think_ you can just re-raise caught exceptions after doing whatever you need to do with them.

I use something like this:

```auto
try:
	0/0
except ZeroDivisionError:
	message = "You divided by zero"
	raise
finally:
	print(message)

```

And I believe I get the full stacktrace in the logs.  
I'll have to check, it's been a while since I wrote the wrappers I use to handle it.  
I re-raise errors in case I need to handle them in some higher layer, instead of just logging them.  
And if you don't catch them, they'll be logged naturally.

---

<div class="post-metadata">

**Author:** ![AIO](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/aio/32/87549_2.png) [@AIO](https://forum.inductiveautomation.com/u/AIO)\
**Post date:** [August 14, 2023, 9:43pm UTC](https://forum.inductiveautomation.com/t/printing-stack-trace-to-gateway-logs-from-a-try/77776/10 "2023-08-14T21:43:29Z")

</div>

Neat - I'd have not thought to try it that way. I'll have to test it and see what I get!
