Good Way of Logging Messages

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2023 02:03 PM - edited 08-31-2023 02:10 PM
When doing development work, I find that being able to SEE what is going on is critical, so I rely heavily on logging.
But I have had a lot of frustration with trying to log things to the regular ServiceNow system log, so I decided to create my own Log Table and my own Logging Functions. What do you guys think of the following?
Screenshot of logged messages:
Zoomed Out Screenshot of logged messages:
Example of Code to generate these log messages:
Easy Table Definition:
User Log Function:
function UserLog(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)
{
var gdt = new GlideDateTime();
var grNew = new GlideRecord("u_log");
grNew.initialize();
grNew.u_string0 = v0;
grNew.u_string1 = v1;
grNew.u_string2 = v2;
grNew.u_string3 = v3;
grNew.u_string4 = v4;
grNew.u_string5 = v5;
grNew.u_string6 = v6;
grNew.u_string7 = v7;
grNew.u_string8 = v8;
grNew.u_string9 = v9;
grNew.u_timestamp = gdt.getLocalDate() + " " + gdt.getUserFormattedLocalTime() + " " + AddCommas(gdt.getNumericValue());
grNew.insert();
}
Clear Log Function: (optional)
function ClearUserLog()
{
var gr = new GlideRecord("u_log");
gr.query();
gr.deleteMultiple();
}
Add Commas Function: (optional)
function AddCommas(x)
{
x = x.toString();
var pattern = /(-?\d+)(\d{3})/;
while (pattern.test(x))
x = x.replace(pattern, "$1,$2");
return x;
}
UserLogH (Heading) Function: (optional)
function UserLogH(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)
{
var gdt = new GlideDateTime();
var grNew = new GlideRecord("u_log");
grNew.initialize();
grNew.u_timestamp = gdt.getLocalDate() + " " + gdt.getUserFormattedLocalTime() + " " + AddCommas(gdt.getNumericValue());
grNew.insert();
gdt = new GlideDateTime();
grNew = new GlideRecord("u_log");
grNew.initialize();
grNew.u_string0 = v0;
grNew.u_string1 = v1;
grNew.u_string2 = v2;
grNew.u_string3 = v3;
grNew.u_string4 = v4;
grNew.u_string5 = v5;
grNew.u_string6 = v6;
grNew.u_string7 = v7;
grNew.u_string8 = v8;
grNew.u_string9 = v9;
grNew.u_timestamp = gdt.getLocalDate() + " " + gdt.getUserFormattedLocalTime() + " " + AddCommas(gdt.getNumericValue());
grNew.insert();
}
Advantages
- I don't have to wade through tons of log messages from other modules in table syslog.
- I can easily Clear the log whenever I want, without affecting anyone else. Clearing can be done manually, or via script.
- I can generate logs in Tabular form; no need to cram multiple values into a single string.
- I can easily give my log values Names (Headings) and they do not interfere with the values themselves.
- I can add Blank Likes to separate groups of log messages.
- Everything is still inside of ServiceNow. No extra tools are required.
- I still have the full Sort / Filter / Grouping capabilities that come with any ServiceNow table.
- My log messages have Ordering to them, down to millisecond granularity, so no confusion with log messages getting out of order.
- Log functions are easy to call since they are classless. No need to instantiate a special log object.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2023 04:11 PM
Don't forget log points and script tracer. They can give you the "zoomed in" context you're looking for.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2023 05:58 AM
Those are good tools to know about. I have not had good luck with that "Script Debugger" window in the past. Why is it not a regular browser tab?