How to properly Debug?

peterraeves
Mega Guru

I often see code like this

var debug = true;

if (debug) gs.print(msg);

if (debug) gs.info(msg);

Is there a specific reason why people don't use the GlideSystem instead? Like this

if (gs.isDebugging()) gs.print(msg);

Or maybe even shorter

gs.debug(msg);

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

FWIW, I often have distinct debugging in each of my script includes so I can debug them independent of each other (not get overwhelmed with debug output.) In those cases, I'll make an _debug function.



_debug : function(msg) {


        if (this.debug)


                  gs.debug(msg);


}



Then call it in various places as



this._debug(recordCount + ' records read');



I can then turn on and off the debug property in the script include with a property so I don't have to modify code in production to enable/disable debugging.


View solution in original post

9 REPLIES 9

Chuck Tomasi
Tera Patron

Hi Peter,



This should help for global scope or legacy apps not in a scope.



Debugging Tools Best Practices - ServiceNow Wiki



Although it is starting to become a bit dated as it does not yet include scoped applications. For scoped apps, gs.print() and gs.log() are not available. For those, the new API is gs.debug/info/warn/error. And you can set the destination and verbosity per application which makes it very useful to debug.



Scoped Script Logging - ServiceNow Wiki



There is no gs.isDebugging() method.


Thanks. I will be glad to read through them. It does surprise me that people keep linking to the old Fuji documentation.



PS: gs.isDebugging exists. It is mentioned in your second link and here is the official documentation on the (scoped) function ServiceNow Developers - gs.isDebugging()


Thanks for the education. I didn't know about gs.isDebugging(). #oldtimer It must have come out in Geneva. Sadly, the documentation doesn't say what it is predicated on. What turns debugging on/off? Is it the scoped properties scope.logging.verbosity and scope.logging.destination?



I'll ask and find out.


To answer your question, I'll quote your own link


The following method is available for determining if debug is active, returning true when either session debugging is active or log verbosity is set to debug for the current application:



gs.isDebugging()