Sorting log entries by actual creation order?

mike_sol
Giga Contributor

I'm currently working on a reasonably advanced script, and due to the lack of any other method for debugging I'm using the good old lots-of-logging technique.

However, it seems that timestamp granularity in ServiceNow's syslog table is only down to the second, not the millisecond, and so I can't easily see a list of my logging output in the order I'm actually writing it to the log in. This makes reading the logs infuriating and time consuming.

Not only that, but the sort order is not deterministic - reloading the same view multiple times with a bunch of log entries on the same timestamp will return them in a different order each time. (How the **** does that work?)

Is there any mechanism to view the logs in the order they were written?

9 REPLIES 9

geoffcox
Giga Guru

You can start your log message with a sequence number. Then sort the log by create date z-a AND message z-a. start the sequence number with 100 or 1000 or whatever is big enough so that it will sort properly.


I agree with Geoff, this is probably the best course of action if you need your logs to be sorted more accurately than by the second.

If this is something you think will be something you'll be needing in the future, it might not hurt to create an enhancement request for better timestamping. I could see the benefit to allow us to control the depth of the timestamps for reporting purposes.


This worked great for me as logs by the millisecond still weren't cutting it.

Leaving here what I added to my Script Include, in case it is useful.

	LOG_COUNT: 1000,

	_infoLog: function(title, target){
		this.LOG_COUNT ++;
		return gs.info("xxxxxNAME_SCRIPT_INCLUDExxxxx " + this.LOG_COUNT + " " + title + ": " + target);
	},

mike_sol
Giga Contributor

I guess that could work, but it's far from ideal, and would require me to maintain counter variables when logging from a loop (something I only do for development and disable in production, of course).