cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Logging Stack Trace of an Exception from Java Extension.

Participant

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 8

Re: Logging Stack Trace of an Exception from Java Extension.

Thanks shiva kumar. excellent blog

Re: Logging Stack Trace of an Exception from Java Extension.

Thanks Shiv, Good blog and helpful.

Re: Logging Stack Trace of an Exception from Java Extension.

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

Re: Logging Stack Trace of an Exception from Java Extension.

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

Re: Logging Stack Trace of an Exception from Java Extension.

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

Highlighted

Re: Logging Stack Trace of an Exception from Java Extension.

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

Re: Logging Stack Trace of an Exception from Java Extension.

Hi Shiva,

 

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

 

My Bests,

Hung Tran

Re: Logging Stack Trace of an Exception from Java Extension.

Hi  VladimirRosu

 

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

 

My Bests,

Hung Tran

Announcements

Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum! ______________________________