Skip to main content
1-Visitor
Oct 26, 2017 3:38 PM UTC
Question

Logging Stack Trace of an Exception from Java Extension.

Thingworx does not log the exact exceptions thrown by the Services but rather masks it  with intuitive Exceptions like "Unable to Invoke Services",

sometimes the log messages just say "null" not describing the exact origin of the "NullPointerException". Needless to explain how difficult it can get to debug.

Below is a small piece of Java code that can used in Java Extension that will help you log the Stack trace of the Exception.

public static void extractStackTrace(Exception exception, Logger _logger) {

     StackTraceElement[] elements = exception.getStackTrace();

     String log = null;

for (StackTraceElement element : elements) {

     log = (log == null) ? element.toString() : log + ";" + element.toString();

}

     _logger.error(log);

}

You can monitor the Exception in the Application log.

It is better if this Stack Track log can be made optional only on Debug Mode.

public static void logError(Exception exception, Logger loggerObject, String message) {

if (exception != null) {

if (isDebugMode) {

     extractStackTrace(exception, loggerObject, message);

} else {

     loggerObject.error(message + " : " + exception);

}

} else {

     loggerObject.error(message);

}

}

8 replies

1-Visitor
Nov 22, 2017 6:01 PM UTC

Thanks shiva kumar. excellent blog

Translate
1-Visitor
Dec 1, 2017 2:33 PM UTC

Thanks Shiv, Good blog and helpful.

Translate
1-Visitor
Dec 8, 2017 6:45 AM UTC

Great..How can we incorporate this for user created services in things? Please explain.

Translate
skumar-811-VisitorAuthor
1-Visitor
Dec 21, 2017 3:17 PM UTC

This blog is for User created services in Java, But it does not apply for Entities created on Composer with JavaScript Services.

Translate
19-Tanzanite
Dec 21, 2017 3:46 PM UTC

Hi Shiva,

One remark, in the Logging subsystem there is a checkbox called Enable Stack Tracing, which if enabled will write the stack trace to the ErrorLog.log. Can you check if by enabling this checkbox you will not get the same behavior (I'm curious if it has the same effect) ?

Translate
15-Moonstone
Apr 25, 2018 7:45 PM UTC

Hi Shiva,

 

Do you know how i could enable the checkbox when thingworx could not be loaded?

 

My Bests,

Hung Tran

Translate
skumar-811-VisitorAuthor
1-Visitor
Dec 21, 2017 4:52 PM UTC

Good Point Vladimir,

I have tried that earlier, it does not log exceptions from your code and even hardly logs stack traces and errors from platform..

Translate