Several years ago I did a blog post about creating a custom logger within ServiceNow. However, that method is a bit out of date and originally dived into Packages calls that could cause problems in the future. The following tutorial is a quick way to set up your own logging levels within ServiceNow that uses standard API calls.

Create a System Property

You will want your administrators to be able to easily change the logging level of your ServiceNow application/integration. In order to do this, you will likely use a System Property.

I created the following System Property for this purpose:

VerbosityProperty

I like to give detailed descriptions for my properties. Properties can render basic HTML, so I usually take advantage of that:

1
2
3
4
<b>Log Verbosity Level</b><br/>
Low - Warning and possible Error condition statements<br/>
Medium - Informational statements listing general operations of the integration<br/>
High - Variable values, comparison tests, etc.

This property uses a “Choice List” option. When using the Choice List property, you enter in your choices via a comma separated list of values. These values serve as both your field value and label. In my example, I am going to have just three log levels: Low, Medium, and High. Of course you can do whatever fits your business needs.

Once I have this set up, the user will see the following interface:

LogSettings

Create the functions

Now that we have our property, we will create the logging functions. Now, there are a lot of ways to do this. The following way was simple given the situation I was in. You can make it as simple or complicated as you wish.

The code for your script include may look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
var MyIntegration = Class.create();
MyIntegration.prototype = {
   initialize: function() {
      this.logSource = "MyIntegration"; //Set the Source value for the log statements
      this.verbosity = gs.getProperty("com.snc.integration.myintegration.verbosity", "Low");
   },
   
   //Log Everything
   log: function(msg) {
      gs.log(msg, this.logSource);
   },

   //Only log for Medium or High levels  
   logMedium: function(msg){
      if( this.verbosity != "Low"){
         this.log(msg);
      }
   },
   
   //Only log for the High Level
   logHigh: function(msg){
      if( this.verbosity == "High" ){
         this.log(msg);
      }
   },
   
   type: 'MyIntegration'
}

Test the Code

You can test out the code easily in your “Scripts – Background” module:

1
2
3
4
var smi = new SolManIntegration();
smi.log("Hello");
smi.logMedium("Hello Medium Logger");
smi.logHigh("hello High Logger");