PageRenderTime 29ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/node_modules/mongoose/node_modules/mongodb-core/lib/connection/logger.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 246 lines | 136 code | 17 blank | 93 comment | 39 complexity | c1058202b683f2f0733406c5c6873b67 MD5 | raw file
  1'use strict';
  2
  3var f = require('util').format,
  4  MongoError = require('../error').MongoError;
  5
  6// Filters for classes
  7var classFilters = {};
  8var filteredClasses = {};
  9var level = null;
 10// Save the process id
 11var pid = process.pid;
 12// current logger
 13var currentLogger = null;
 14
 15/**
 16 * Creates a new Logger instance
 17 * @class
 18 * @param {string} className The Class name associated with the logging instance
 19 * @param {object} [options=null] Optional settings.
 20 * @param {Function} [options.logger=null] Custom logger function;
 21 * @param {string} [options.loggerLevel=error] Override default global log level.
 22 * @return {Logger} a Logger instance.
 23 */
 24var Logger = function(className, options) {
 25  if (!(this instanceof Logger)) return new Logger(className, options);
 26  options = options || {};
 27
 28  // Current reference
 29  this.className = className;
 30
 31  // Current logger
 32  if (options.logger) {
 33    currentLogger = options.logger;
 34  } else if (currentLogger == null) {
 35    currentLogger = console.log;
 36  }
 37
 38  // Set level of logging, default is error
 39  if (options.loggerLevel) {
 40    level = options.loggerLevel || 'error';
 41  }
 42
 43  // Add all class names
 44  if (filteredClasses[this.className] == null) classFilters[this.className] = true;
 45};
 46
 47/**
 48 * Log a message at the debug level
 49 * @method
 50 * @param {string} message The message to log
 51 * @param {object} object additional meta data to log
 52 * @return {null}
 53 */
 54Logger.prototype.debug = function(message, object) {
 55  if (
 56    this.isDebug() &&
 57    ((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
 58      (Object.keys(filteredClasses).length === 0 && classFilters[this.className]))
 59  ) {
 60    var dateTime = new Date().getTime();
 61    var msg = f('[%s-%s:%s] %s %s', 'DEBUG', this.className, pid, dateTime, message);
 62    var state = {
 63      type: 'debug',
 64      message: message,
 65      className: this.className,
 66      pid: pid,
 67      date: dateTime
 68    };
 69    if (object) state.meta = object;
 70    currentLogger(msg, state);
 71  }
 72};
 73
 74/**
 75 * Log a message at the warn level
 76 * @method
 77 * @param {string} message The message to log
 78 * @param {object} object additional meta data to log
 79 * @return {null}
 80 */
 81(Logger.prototype.warn = function(message, object) {
 82  if (
 83    this.isWarn() &&
 84    ((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
 85      (Object.keys(filteredClasses).length === 0 && classFilters[this.className]))
 86  ) {
 87    var dateTime = new Date().getTime();
 88    var msg = f('[%s-%s:%s] %s %s', 'WARN', this.className, pid, dateTime, message);
 89    var state = {
 90      type: 'warn',
 91      message: message,
 92      className: this.className,
 93      pid: pid,
 94      date: dateTime
 95    };
 96    if (object) state.meta = object;
 97    currentLogger(msg, state);
 98  }
 99}),
100  /**
101   * Log a message at the info level
102   * @method
103   * @param {string} message The message to log
104   * @param {object} object additional meta data to log
105   * @return {null}
106   */
107  (Logger.prototype.info = function(message, object) {
108    if (
109      this.isInfo() &&
110      ((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
111        (Object.keys(filteredClasses).length === 0 && classFilters[this.className]))
112    ) {
113      var dateTime = new Date().getTime();
114      var msg = f('[%s-%s:%s] %s %s', 'INFO', this.className, pid, dateTime, message);
115      var state = {
116        type: 'info',
117        message: message,
118        className: this.className,
119        pid: pid,
120        date: dateTime
121      };
122      if (object) state.meta = object;
123      currentLogger(msg, state);
124    }
125  }),
126  /**
127   * Log a message at the error level
128   * @method
129   * @param {string} message The message to log
130   * @param {object} object additional meta data to log
131   * @return {null}
132   */
133  (Logger.prototype.error = function(message, object) {
134    if (
135      this.isError() &&
136      ((Object.keys(filteredClasses).length > 0 && filteredClasses[this.className]) ||
137        (Object.keys(filteredClasses).length === 0 && classFilters[this.className]))
138    ) {
139      var dateTime = new Date().getTime();
140      var msg = f('[%s-%s:%s] %s %s', 'ERROR', this.className, pid, dateTime, message);
141      var state = {
142        type: 'error',
143        message: message,
144        className: this.className,
145        pid: pid,
146        date: dateTime
147      };
148      if (object) state.meta = object;
149      currentLogger(msg, state);
150    }
151  }),
152  /**
153   * Is the logger set at info level
154   * @method
155   * @return {boolean}
156   */
157  (Logger.prototype.isInfo = function() {
158    return level === 'info' || level === 'debug';
159  }),
160  /**
161   * Is the logger set at error level
162   * @method
163   * @return {boolean}
164   */
165  (Logger.prototype.isError = function() {
166    return level === 'error' || level === 'info' || level === 'debug';
167  }),
168  /**
169   * Is the logger set at error level
170   * @method
171   * @return {boolean}
172   */
173  (Logger.prototype.isWarn = function() {
174    return level === 'error' || level === 'warn' || level === 'info' || level === 'debug';
175  }),
176  /**
177   * Is the logger set at debug level
178   * @method
179   * @return {boolean}
180   */
181  (Logger.prototype.isDebug = function() {
182    return level === 'debug';
183  });
184
185/**
186 * Resets the logger to default settings, error and no filtered classes
187 * @method
188 * @return {null}
189 */
190Logger.reset = function() {
191  level = 'error';
192  filteredClasses = {};
193};
194
195/**
196 * Get the current logger function
197 * @method
198 * @return {function}
199 */
200Logger.currentLogger = function() {
201  return currentLogger;
202};
203
204/**
205 * Set the current logger function
206 * @method
207 * @param {function} logger Logger function.
208 * @return {null}
209 */
210Logger.setCurrentLogger = function(logger) {
211  if (typeof logger !== 'function') throw new MongoError('current logger must be a function');
212  currentLogger = logger;
213};
214
215/**
216 * Set what classes to log.
217 * @method
218 * @param {string} type The type of filter (currently only class)
219 * @param {string[]} values The filters to apply
220 * @return {null}
221 */
222Logger.filter = function(type, values) {
223  if (type === 'class' && Array.isArray(values)) {
224    filteredClasses = {};
225
226    values.forEach(function(x) {
227      filteredClasses[x] = true;
228    });
229  }
230};
231
232/**
233 * Set the current log level
234 * @method
235 * @param {string} level Set current log level (debug, info, error)
236 * @return {null}
237 */
238Logger.setLevel = function(_level) {
239  if (_level !== 'info' && _level !== 'error' && _level !== 'debug' && _level !== 'warn') {
240    throw new Error(f('%s is an illegal logging level', _level));
241  }
242
243  level = _level;
244};
245
246module.exports = Logger;