Skip to main content
1-Visitor
October 26, 2017
Question

Logging Stack Trace of an Exception from Java Extension.

  • October 26, 2017
  • 6 replies
  • 6195 views

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);

}

}

6 replies

1-Visitor
November 22, 2017

Thanks shiva kumar. excellent blog

1-Visitor
December 1, 2017

Thanks Shiv, Good blog and helpful.

1-Visitor
December 8, 2017

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

skumar-811-VisitorAuthor
1-Visitor
December 21, 2017

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

19-Tanzanite
December 21, 2017

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) ?

15-Moonstone
April 25, 2018

Hi Shiva,

 

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

 

My Bests,

Hung Tran

skumar-811-VisitorAuthor
1-Visitor
December 21, 2017

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..