Name

sn_itom_pattern.Logger

Description

Logger for info and debug messages in the discovery and service mapping patterns app The logger will send messages to the syslog with the prefix provided in the constructor. the debug method will send messages if property sn_itom_pattern.logger_level = debug AND (prefix is undefied OR property sn_itom_pattern.logger_names is undefined OR prefix is one of the logger names in sn_itom_pattern.logger_names) The arguments sent can be string/number, array or a map.

Script

var Logger = Class.create();
Logger.prototype = {
  initialize: function(prefix) {
  	
  	// The debug method will print if:
  	//     property sn_itom_pattern.logger_level = debug AND
  	//     (prefix is undefied OR 
  	//      property sn_itom_pattern.logger_names is undefined OR
  	//      prefix is one of the logger names in sn_itom_pattern.logger_names)
  	this.debugPrint = this.isDebugPrint(prefix);
  	
  	if (prefix)
  		this.prefix = 'PatternsApp:[' + prefix + '] ';
  	else
  		this.prefix = 'PatternsApp: ';
  },

  /*
  *  Check if we need to print the message when calling logger.debug
  */
  isDebugPrint: function(prefix) {
  	
  	if (gs.getProperty('sn_itom_pattern.logger_level') != 'debug')
  		return false;
  	
  	if (!prefix)
  		return true;
  	
  	var loggerNames = gs.getProperty('sn_itom_pattern.logger_names');
  	if (!loggerNames)
  		return true;
  	
  	var loggerNamesArr = loggerNames.split(',');
  	for (var loggerNamesIdx in loggerNamesArr) {
  		if (loggerNamesArr[loggerNamesIdx] == prefix) {
  			return true;
  		}
  	}

  	return false;
  },
  
  /*
  * args can be scalar, map or array. the method will know how to convert to readable message
  */
  debug: function(args) {
  	if (this.debugPrint)
  		gs.info(this.prefix + this._argsToMessage(args));
  },
  
  /*
  * args can be scalar, map or array. the method will know how to convert to readable message
  */
  log: function(args) {
  	gs.info(this.prefix + this._argsToMessage(args));
  },
  
  /*
  * args can be scalar, map or array. the method will know how to convert to readable message
  */
  error: function(args) {
  	gs.error(this.prefix + this._argsToMessage(args));
  },
  
  _argsToMessage: function (args) {
  	if (!args)
  		return '';
  	var argsType = this.getObjectType(args);
  	if (argsType == 'map')
  		return JSON.stringify(args);
  	
  	var message = '';
  	if (argsType == 'array') {
  		for (var idx in args) {
  			message += this._argsToMessage(args[idx]);
  		}
  		return message;
  	}
  	
  	message = args + '';
  	return message;
  },
  
  getObjectType: function(obj) {
   if (typeof(obj) === 'string' || typeof(obj) === 'number' || typeof(obj) == 'boolean')
      return 'scalar';
   else
      if (obj instanceof Array) 
          return  'array';
      else
          return 'map';
  },
  
  type: 'Logger'
};

Sys ID

211cedf8a12b5510f8775b4b0fc68db9

Offical Documentation

Official Docs: