To log or not to log. That is the question!?

General Add comments
by:

We all like to develop new and ingenious code which does lots of spectacular things. In order to achieve this we probably need some help in the form of logging as not always we can write the code perfectly the first time, after all we are only human and a typo is easily made. A mistake that can take hours of your time to find it.

The issue Logging is as old as software programming itself.

You have built in all the necessary code for logging, code is accepted to go to production, but we don’t want this code to be active in production as that will clutter the log for no good reason.
So you decide to delete the logging lines completely or you have commented them out. But why would you want to burden the next developer by necessitating going back and re-adding logging for problematic areas in the future!?
Or, if you did it really fancy you left all the debugging lines in and created a toggle to enable or disable the logging, this has probably cost you some serious time to make as you need to make sure all the logging statements are included in an ‘if’ statement. See example below.

if(this.debug){
gs.log(‘something to log’);
}

Turns out there is an easier and neater way of doing this and it is shipped with ServiceNow out of the box. It is called GSLog! It is a “script include” which makes logging and debugging from script easier by implementing levels of log output, selectable by per-caller identified sys_properties values. Logs can be at the level of Debug, Info, Notice, Warning, Err, or Crit. The default logging level would normally be Notice, so levels should be chosen accordingly.

You can setup different logs for different modules/interfaces.
To have something written to a specific log you can use the following code:

var gl= new GSLog(“com.logicalissmc.integration.netcool”, “Netcool”);
gl.log(“debug”, “debug message”);
gl.log(“info”, “info message”);
gl.log(“crit”, “crticial message”)

Below are the steps to make the property accessible from the navigator.

Figure 1: Setup of the system property category
image001

Figure 2: Setup of the system property
image003

“com.logicalissmc.integration.netcool” is the system property that contains a value indicating the level at or above which messages will be written to the log. Click here for details on how to create a property & module in ServiceNow.
When you create the property then make it a choice list with the options; debug, info, notice, warning, err, crit. This makes it easier for other to toggle the correct debug level.
“Netcool” is what you will see in the “Source” field of the log. You can state the name of the script that you are dealing with but you also call it something more generic like the interface name that you want to create some logging for.

Figure 3: how it will look when someone wants to change the debug level for a module or integration.
image005

Figure 4: This is how it shows in the logs.
image007

Hope this helps you creating more understandable, useful and more permanent on demand debugging information for yourself and others. If you have questions, please let me know via email or a response below!

Good luck!
Kind regards,
Laurens Brand (.img[at].img)

Leave a Reply