loop91 Header logo
Experiments

Alternate way to Logging in ServiceNow - Alternate to gs.info()

Kunal Khatri
#servicenow#dev experinence#dx#logging

The Ask ?

Develop a method to log your messages without using gs.info().

Solution:

gs.info() / gs.error() adds a new record for each call to table syslog_list. This can get cluttered, if you are one of many developers using the same method.

I propose an alternate method. We create a new record on table task and add a new work_note to the task as form of logging.

Benefits:

  1. Task is unique to you, so it doesn’t get cluttered.
  2. Since work_notes show in Activites formatter, they auto reload and prevents you from being required to reload the page.
  3. At the end of the day, you can simply delete the task and associated work_notes

Cons:

  1. This should not be used on non-development environments. Production environment can have flows/buiness rules associated with task table.

Steps to implement this approach

  1. Create a new Script Includes sys_script_include, marking Accesible from to All application scopes
var logToTask = Class.create();
logToTask.prototype = {
    initialize: function() {
    },
	logToTask : function(string,sys_id){
		var taskGr = new GlideRecord("task");
		if (taskGr.get(sys_id)){
			taskGr.work_notes = string;
			taskGr.update();
			return true;
		}
		return false;
	},
	type: 'logToTask'
};
  1. Create a new task record. Copy the sys_id of newly created task.
  2. Now whenever you need to log a string, use following code to do so
var ltt = new global.logToTask().logToTask;
// ltt ( string to log,   sys_id of task record )
ltt("Some things are better with logging.","6f6f85d5c3030210dea6160ed40131eb");
  1. It should show up like this task record showing log code
← Back to Blog