GSLog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2019 03:32 AM
I have:
var gl = new GSLog('myAppName', 'mySourceName');
gl.logInfo('this is a test message');
gl.logErr('this is a test message');
I get the error message in the log, but I do NOT get the info message.
I WANT info messages to write to the log for my job execution.
it looks like sn is defaulting to error level only.
what do i need to do to see the info messages?
i do NOT see a sys property OOB for log level. do I need to set one up?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-31-2023 09:37 AM - edited ‎02-01-2023 12:53 AM
Hi,
You can use the "global." prefix in scoped scripts e.g.
var logger = new global.GSLog('','My script');
logger.setLevel(global.GSLog.DEBUG);
logger.includeTimestamp();
logger.logDebug('Hello World');
Refer ...
GSLog has the very handy millisecond includeTimestamp() method which, combined with a specified Source string, allows easy System log querying in exact chronological order. You can search for log entries containing the set Source, sorting by created, and then by message.
Lots of people write their own logging modules without knowing this OOTB functionality exists.
It would be nice if the application logging had similar optional millisecond stamping - production instance logging, even in the app log, can be huge.
A downside to GSLog is the logging methods are always evoked even if the log level is set to none.
The extra processing overhead is probably miniscule but I often create a project or process GSLog "wrapper" script include that only runs the GSLog methods if a log level is actually set.
// Example wrapper for GSLog
// Wrapper avoids always querying for the log level if no logging is set in the system property for the log level.
// Accessible from all application scopes
var yourProjectLogger = Class.create();
yourProjectLogger.prototype = {
initialize: function (source) {
yourProjectLogger.myLoglevel = gs.getProperty('yourProject.gslog_level', null);
if (yourProjectLogger.myLoglevel) {
// https://docs.servicenow.com/bundle/sandiego-application-development/page/script/useful-scripts/reference/r_GSLog.html
yourProjectLogger.LOG = new global.GSLog('yourProject.gslog_level', source);
yourProjectLogger.LOG.includeTimestamp();
}
},
logInfo: function (message) {
if (yourProjectLogger.myLoglevel) {
yourProjectLogger.LOG.logInfo(message);
}
},
logWarning: function (message) {
if (yourProjectLogger.myLoglevel) {
yourProjectLogger.LOG.logWarning(message);
}
},
logErr: function (message) {
if (yourProjectLogger.myLoglevel) {
yourProjectLogger.LOG.logErr(message);
}
},
logDebug: function (message) {
if (yourProjectLogger.myLoglevel) {
yourProjectLogger.LOG.logDebug(message);
}
},
type: 'yourProjectLogger'
};
// Example use
var yourcompanyYourAPI_v1 = Class.create();
yourcompanyYourAPI_v1.prototype = {
initialize: function () {
this.AGENT = 'YourAPI';
this.LOG = new global.yourProjectLogger('yourcompanyYourAPI');
this.LOG.logDebug('a debug message');
this.LOG.logInfo('a info message');
this.LOG.logWarning('a warning message');
},
createOutBoundMessage: function (options, source, payload) {
this.LOG.logDebug('payload: ' + payload);
var ecc_queue = new GlideRecord('ecc_queue');
ecc_queue.initialize();
ecc_queue.agent = this.AGENT;
ecc_queue.agent_correlator = options.integid;
ecc_queue.topic = options.messagename;
ecc_queue.name = options.methodname;
ecc_queue.source = source;
ecc_queue.queue = 'output';
ecc_queue.payload = payload;
return ecc_queue.insert();
},
.
.
.