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

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Logging Stack Trace of an Exception from Java Extension.

skumar-81
4-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

Thanks shiva kumar. excellent blog

Thanks Shiv, Good blog and helpful.

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

skumar-81
4-Participant
(To:skumar-81)

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

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

Hi Shiva,

 

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

 

My Bests,

Hung Tran

Hi  VladimirRosu

 

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

 

My Bests,

Hung Tran

skumar-81
4-Participant
(To:skumar-81)

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

Top Tags