Code coverage report for lib\log.js

Statements: 92.16% (47 / 51)      Branches: 83.33% (20 / 24)      Functions: 92.31% (12 / 13)      Lines: 92.16% (47 / 51)      Ignored: none     

All files » lib/ » log.js
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222                                            1 1       1                     1                                                                   97 91     97 6         91     97 3 3       94     97               5                                         1 489     489 489     1                         1 1604 94 1     94 94   94 94 44   94                 1 50             1 19             1 108             1 1433               1 27 27 27               1 70 70     1           1
/*
 * @copyright
 * Copyright © Microsoft Open Technologies, Inc.
 *
 * All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http: *www.apache.org/licenses/LICENSE-2.0
 *
 * THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
 * ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A
 * PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
 *
 * See the Apache License, Version 2.0 for the specific language
 * governing permissions and limitations under the License.
 */
'use strict';
 
var _ = require('underscore');
var uuid = require('node-uuid');  // want to replace with this in the future: https://gist.github.com/jed/982883
 
 
 
var LEVEL_STRING_MAP = {
  0 : 'ERROR:',
  1 : 'WARNING:',
  2 : 'INFO:',
  3 : 'VERBOSE:'
};
 
/**
 * Methods for controling global logging options for ADAL
 * @namespace
 */
var Logging = {
 
  /**
   * @callback LoggingCallback
   * @memberOf Logging
   * @param {Logging.LOGGING_LEVEL} level The level of this log entry.
   * @param {string} message The text content of the log entry.
   * @param {Error}  [error] An Error object if this is an {@link Logging.LOGGING_LEVEL.ERROR|ERROR} level log entry.
   */
 
  /**
   * @typedef LoggingOptions
   * @memberOf Logging
   * @property {LoggingCallback} [log] The function to call when ADAL generates a log entry.
   * @property {Logging.LOGGING_LEVEL} [level] The maximum level of log entries to generate.
   */
 
  /**
   * Describes the available logging levels.
   * @enum
   * @type {Number}
   */
  LOGGING_LEVEL : {
    ERROR    : 0,
    WARN     : 1,
    INFO     : 2,
    VERBOSE  : 3
  },
 
  /**
   * Sets global logging options for ADAL.
   * @param {LoggingOptions} options
   */
  setLoggingOptions : function(options) {
    if (!options) {
      options = {};
    }
 
    if (options.log) {
      Iif (!_.isFunction(options.log)) {
        throw new Error('setLogOptions expects the log key in the options parameter to be a function');
      }
    } else {
      // if no log function was passed set it to a default no op function.
      options.log = function() {};
    }
 
    if (options.level) {
      var level = options.level;
      Iif (level < 0 || level > 3) {
        throw new Error('setLogOptions expects the level key to be in the range 0 to 3 inclusive');
      }
    } else {
      options.level = this.LOGGING_LEVEL.ERROR;
    }
 
    this.LogOptions = options;
  },
 
  /**
   * Get's the current global logging options.
   * @return {LoggingOptions}
   */
  getLoggingOptions : function() {
    return this.LogOptions;
  },
 
  /**
   * Stores the current global logging options.
   * @private
   * @type {LoggingOptions}
   */
  LogOptions : {
    log : function() {},
    level : 0,
  }
};
 
/**
 * An internal logging object.
 * @class
 * @private
 * @param {string} componentName The name of the component that created this instance.  This name will be
 *                               prepended to the beginning of all log entries generated by this instance.
 */
function Logger(componentName, logContext) {
  Iif (!logContext) {
    throw new Error('Logger: logContext is a required parameter');
  }
  this._componentName = componentName;
  this._logContext = logContext;
}
 
Object.defineProperty(Logger.prototype, 'context', {
  get: function () {
    return this._logContext;
  }
});
 
/**
 * Generates a log entry
 * @param  {Logging.LOGGING_LEVEL} level The level of this log entry
 * @param  {string|function} message A message string, or a function that returns a message string, to log.
 * @param  {Error} [error] If this is a {@link Logging.LOGGING_LEVEL.ERROR|ERROR} level log entry then the caller
 *                       should pass an error object in this parameter.
 */
Logger.prototype.log = function(level, message, error) {
  if (level <= Logging.LogOptions.level) {
    if (_.isFunction(message)) {
      message = message();
    }
 
    var correlationId = this._logContext.correlationId || '<no correlation id>';
    var timeStamp = new Date().toUTCString();
 
    var formattedMessage = timeStamp + ':' + correlationId + ' - ' + this._componentName + ': ' + LEVEL_STRING_MAP[level] + ' ' + message;
    if (error) {
      formattedMessage += '\nStack:\n' + error.stack;
    }
    Logging.LogOptions.log(level, formattedMessage, error);
  }
};
 
/**
 * Generate an {@link Logging.LOGGING_LEVEL.ERROR|ERROR} level log entry.
 * @param  {string} message A message to log
 * @param  {Error} error The Error object associated with this log entry
 */
Logger.prototype.error = function(message, error) {
  this.log(Logging.LOGGING_LEVEL.ERROR, message, error);
};
 
/**
 * Generate an {@link Logging.LOGGING_LEVEL.WARN|WARN} level log entry.
 * @param  {string} message A message to log
 */
Logger.prototype.warn = function(message) {
  this.log(Logging.LOGGING_LEVEL.WARN, message, null);
};
 
/**
 * Generate an {@link Logging.LOGGING_LEVEL.INFO|INFO} level log entry.
 * @param  {string} message A message to log
 */
Logger.prototype.info = function(message) {
  this.log(Logging.LOGGING_LEVEL.INFO, message, null);
};
 
/**
 * Generate an {@link Logging.LOGGING_LEVEL.VERBOSE|VERBOSE} level log entry.
 * @param  {string} message A message to log
 */
Logger.prototype.verbose = function(message) {
  this.log(Logging.LOGGING_LEVEL.VERBOSE, message, null);
};
 
/**
 * Generate a {@link Logging.LOGGING_LEVEL.ERROR|ERROR} level log entry, as well as an
 * Error object to go with it.  This is a convenience method for throwing logged errors.
 * @param  {string} message A message to log
 */
Logger.prototype.createError = function(message) {
  var err = new Error(message);
  this.error(message, err);
  return err;
};
 
/**
 * Creates a new log context based on the correlationId passed in.  If no correlationId is passed in
 * then one is generated, by the function uuid.v4()
 * @private
 */
function createLogContext(correlationId) {
  var id = correlationId || uuid.v4();
  return { correlationId : id };
}
 
var exports = {
  Logging : Logging,
  Logger : Logger,
  createLogContext : createLogContext
};
 
module.exports = exports;