removing mocha, switch to nodeunit
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
var _ = require('./Utils')
|
||||
, transports = _.requireDir(module, './transports');
|
||||
, transports = _.requireDir(module, './transports')
|
||||
, Log = require('./Log');
|
||||
|
||||
// Expose the client object
|
||||
function Client(config) {
|
||||
@ -9,7 +10,7 @@ function Client(config) {
|
||||
|
||||
// For convenience
|
||||
// this.transport = this.options.transport || new transports.NodeHttp(this.options);
|
||||
this.logger = config.logger || new es.Log(this.logger);
|
||||
this.logger = config.logger || new Log(this.logger);
|
||||
// this.serializer = this.options.serializer || new es.Serializer.json();
|
||||
|
||||
}
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
var _ = require('../Utils'),
|
||||
events = require('events'),
|
||||
loggers = _.requireDir(module, './loggers');
|
||||
var _ = require('./Utils'),
|
||||
EventEmitter = require('events').EventEmitter;
|
||||
|
||||
/**
|
||||
*
|
||||
* Log bridge, which writes using one or more Logger's. Setup these loggers by
|
||||
* specifying their config with the first argument, or by calling addOutput.
|
||||
*
|
||||
@ -20,13 +18,13 @@ function Log(output) {
|
||||
|
||||
output = output || 2;
|
||||
|
||||
if (_.isString(output)) {
|
||||
if (_.isString(output) || _.isFinite(output)) {
|
||||
output = [
|
||||
{
|
||||
level: output
|
||||
}
|
||||
];
|
||||
} else if (_.isObject(output)) {
|
||||
} else if (_.isPlainObject(output)) {
|
||||
output = [output];
|
||||
} else if (_.isArray(output)) {
|
||||
for (i = 0; i < output.length; i++) {
|
||||
@ -46,14 +44,12 @@ function Log(output) {
|
||||
* A list of the log streams, which are listening to this logger.
|
||||
* @type {Array}
|
||||
*/
|
||||
this.outputs = [];
|
||||
|
||||
for (i = 0; i < output.length; i++) {
|
||||
this.addLogger(output[i]);
|
||||
this.addOutput(output[i]);
|
||||
}
|
||||
|
||||
}
|
||||
_.inherits(Log, events.EventEmitter);
|
||||
_.inherits(Log, EventEmitter);
|
||||
|
||||
/**
|
||||
* Levels observed by the loggers, with their rank
|
||||
@ -76,7 +72,7 @@ Log.levelsInverted = _.invert(Log.levels);
|
||||
|
||||
/**
|
||||
* Converts a log identifier to an integer representing it's level
|
||||
* @private
|
||||
* @static
|
||||
* @param {*} ident - The identifying to convert, if invalid then the default will be returned
|
||||
* @param {Integer} default - The default log level to use if the identifier is not valid
|
||||
* @return {Integer} - The number reprsenting the log level
|
||||
@ -93,6 +89,21 @@ Log.level = function (ident, def) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Combine the array-like param into a simple string
|
||||
* @param {*} arrayish - An array like object that can be itterated by _.each
|
||||
* @return {String} - The final string.
|
||||
*/
|
||||
Log.join = function (arrayish) {
|
||||
return _.map(arrayish, function (item) {
|
||||
if (_.isPlainObject(item)) {
|
||||
return _.inspect(item, { showHidden: true, depth: null, color: true}) + '\n';
|
||||
} else {
|
||||
return item.toString();
|
||||
}
|
||||
}).join(' ');
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a new logger, based on the config.
|
||||
* @param {object} config An object with config options for the logger. Type is used to find the
|
||||
@ -102,15 +113,12 @@ Log.level = function (ident, def) {
|
||||
*/
|
||||
Log.prototype.addOutput = function (config) {
|
||||
_.defaults(config, {
|
||||
type: 'stdio',
|
||||
type: 'StdIo',
|
||||
level: Log.level(config.type, 2)
|
||||
});
|
||||
|
||||
if (loggers[config.type]) {
|
||||
return this.outputs[this.outputs.push(new loggers[config.type](config, this)) - 1];
|
||||
} else {
|
||||
throw new TypeError('Invalid logger type ' + config.type);
|
||||
}
|
||||
var Logger = require('./loggers/' + config.type);
|
||||
return new Logger(config, this);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -119,17 +127,21 @@ Log.prototype.addOutput = function (config) {
|
||||
* @return {undefined}
|
||||
*/
|
||||
Log.prototype.error = function (e) {
|
||||
this.emit('error', e instanceof Error ? e : new Error(e));
|
||||
if (EventEmitter.listenerCount(this, 'error')) {
|
||||
return this.emit('error', e instanceof Error ? e : new Error(e));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Log a warning message
|
||||
* @param {String} message The warning message
|
||||
* @param {...*} msg - Any amount of messages that will be joined before logged
|
||||
* @return {undefined}
|
||||
*/
|
||||
Log.prototype.warning = function (message) {
|
||||
this.emit('warning', message);
|
||||
Log.prototype.warning = function (/* ...msg */) {
|
||||
if (EventEmitter.listenerCount(this, 'warning')) {
|
||||
return this.emit('warning', Log.join(arguments));
|
||||
}
|
||||
};
|
||||
/** @alias Log.warning */
|
||||
Log.prototype.warn = Log.prototype.warning;
|
||||
@ -137,29 +149,35 @@ Log.prototype.warn = Log.prototype.warning;
|
||||
|
||||
/**
|
||||
* Log useful info about what's going on
|
||||
* @param {String} message The warning message
|
||||
* @param {...*} msg - Any amount of messages that will be joined before logged
|
||||
* @return {undefined}
|
||||
*/
|
||||
Log.prototype.info = function (message) {
|
||||
this.emit('info', message);
|
||||
Log.prototype.info = function (/* ...msg */) {
|
||||
if (EventEmitter.listenerCount(this, 'info')) {
|
||||
return this.emit('info', Log.join(arguments));
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Log a debug level message
|
||||
* @param {String} message The warning message
|
||||
* @param {...*} msg - Any amount of messages that will be joined before logged
|
||||
* @return {undefined}
|
||||
*/
|
||||
Log.prototype.debug = function (message) {
|
||||
this.emit('debug', message);
|
||||
Log.prototype.debug = function (/* ...msg */) {
|
||||
if (EventEmitter.listenerCount(this, 'debug')) {
|
||||
return this.emit('debug', Log.join(arguments));
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Log a trace level message
|
||||
* @param {String} message The warning message
|
||||
* @param {...*} msg - Any amount of messages that will be joined before logged
|
||||
* @return {undefined}
|
||||
*/
|
||||
Log.prototype.trace = function (message) {
|
||||
this.emit('trace', message);
|
||||
Log.prototype.trace = function (/* ...msg */) {
|
||||
if (EventEmitter.listenerCount(this, 'trace')) {
|
||||
return this.emit('trace', Log.join(arguments));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -20,7 +20,12 @@ mixins.joinPath = path.join;
|
||||
* @param {Module} module The module object which will own the required modules.
|
||||
* @param {String} path Path to the directory which will be traversed
|
||||
*/
|
||||
mixins.requireDir = requireDir;
|
||||
mixins.requireDir = function (module, dirPath) {
|
||||
if (dirPath && dirPath[0] === '.') {
|
||||
dirPath = path.join(path.dirname(module.filename), dirPath);
|
||||
}
|
||||
return requireDir(module, dirPath);
|
||||
};
|
||||
|
||||
/**
|
||||
* isArrayOf(Strings|Objects|Arrays|Finites|Functions|RegExps)
|
||||
@ -38,6 +43,28 @@ mixins.requireDir = requireDir;
|
||||
};
|
||||
});
|
||||
|
||||
var origBind = _.bind;
|
||||
/**
|
||||
* Slightly modified version of bind, which can accept the context as the first
|
||||
* arg and the method name as the second, like jquery's proxy
|
||||
* @param {Function|Object} func - The method to bind, or the context if the method will be
|
||||
* specified using a string in param 2
|
||||
* @param {Object|String} context - The context when `func` is a function, or the method name to bind
|
||||
* when func is an object
|
||||
* @param {...*} [args] Args to be bound to the function
|
||||
* @return {Function} The bound function
|
||||
*/
|
||||
mixins.bind = function (func, context) {
|
||||
var args = _.rest(arguments, 2);
|
||||
if (typeof context === 'string') {
|
||||
// args[1] is actually a method name, like _.bind(this, 'method');
|
||||
args.unshift(func[context], func);
|
||||
} else {
|
||||
args.unshift(func, context);
|
||||
}
|
||||
return origBind.apply(_, args);
|
||||
};
|
||||
|
||||
_.mixin(mixins);
|
||||
|
||||
module.exports = _;
|
||||
@ -15,10 +15,12 @@ function StdIo(config, bridge) {
|
||||
|
||||
var handlers = this.handlers = {};
|
||||
|
||||
this.color = true;
|
||||
|
||||
_.each(Log.levels, function (i, name) {
|
||||
// create a version of each log event handler that is bound to "this"
|
||||
handlers[Log.levels[name]] = 'on' + name.subString(0, 1).toUpperCase() + name.subString(1).toLowerCase();
|
||||
});
|
||||
handlers[Log.levels[name]] = _.bind(this, 'on' + name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase());
|
||||
}, this);
|
||||
this.setupListeners(config.level);
|
||||
}
|
||||
|
||||
@ -28,7 +30,7 @@ function StdIo(config, bridge) {
|
||||
* @return {undefined}
|
||||
*/
|
||||
StdIo.prototype.setupListeners = function (level) {
|
||||
this.removeListeners();
|
||||
this.cleanUpListeners();
|
||||
for (this.listeningLevel = level; level > 0; level--) {
|
||||
this.bridge.on(Log.levelsInverted[level], this.handlers[level]);
|
||||
}
|
||||
@ -39,27 +41,12 @@ StdIo.prototype.setupListeners = function (level) {
|
||||
* @return {undefined}
|
||||
*/
|
||||
StdIo.prototype.cleanUpListeners = function () {
|
||||
for (; this.listeningLevel < 0; this.listeningLevel--) {
|
||||
for (; this.listeningLevel > 0; this.listeningLevel--) {
|
||||
// remove the listeners for each event
|
||||
this.bridge.removeListener(Log.levelsInverted[this.listeningLevel], this.handlers[this.listeningLevel]);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Combine the array_like param into a simple string
|
||||
* @param {Array|Object} array_like - An array like object that can be itterated by _.each
|
||||
* @return {String} - The final string.
|
||||
*/
|
||||
function join(array_like) {
|
||||
return _.map(array_like, function (item) {
|
||||
if (_.isPlainObject(item)) {
|
||||
return _.inspect(item, { showHidden: true, depth: null, color: true}) + '\n';
|
||||
} else {
|
||||
return item.toString();
|
||||
}
|
||||
}).join(' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends output to a stream, does some formatting first
|
||||
* @param {WriteableStream} to - The stream that should receive this message
|
||||
@ -72,7 +59,7 @@ StdIo.prototype.write = function (to, label, colorize, what) {
|
||||
if (this.color) {
|
||||
label = colorize(label);
|
||||
}
|
||||
to.write(label + ': ' + (typeof what === 'object' ? join(what) : what));
|
||||
to.write(label + ': ' + what + '\n');
|
||||
};
|
||||
|
||||
/**
|
||||
@ -81,43 +68,43 @@ StdIo.prototype.write = function (to, label, colorize, what) {
|
||||
* @return {undefined}
|
||||
*/
|
||||
StdIo.prototype.onError = function (e) {
|
||||
this.write(process.strderr, 'ERROR', clc.red.bold, e.name + ': ' + e.message + '\nStack Trace:\n' + e.stack);
|
||||
this.write(process.stderr, e.name === 'Error' ? 'ERROR' : e.name, clc.red.bold, [e.message, '\n\nStack Trace:\n' + e.stack]);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handler for the bridges "warning" event
|
||||
* @param {...*} msg - Any amount of messages that will be joined before logged
|
||||
* @param {String} msg - The message to be logged
|
||||
* @return {undefined}
|
||||
*/
|
||||
StdIo.prototype.onWarning = function (/* ...msg */) {
|
||||
this.write(process.strderr, 'warning', clc.yellow.bold, arguments);
|
||||
StdIo.prototype.onWarning = function (msg) {
|
||||
this.write(process.stderr, 'WARNING', clc.yellow.bold, msg);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handler for the bridges "info" event
|
||||
* @param {...*} msg - Any amount of messages that will be joined before logged
|
||||
* @param {String} msg - The message to be logged
|
||||
* @return {undefined}
|
||||
*/
|
||||
StdIo.prototype.onInfo = function (/* ...msg */) {
|
||||
this.write(process.stdout, 'INFO', clc.cyan.bold, arguments);
|
||||
StdIo.prototype.onInfo = function (msg) {
|
||||
this.write(process.stdout, 'INFO', clc.cyan.bold, msg);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handler for the bridges "debug" event
|
||||
* @param {...*} msg - Any amount of messages that will be joined before logged
|
||||
* @param {String} msg - The message to be logged
|
||||
* @return {undefined}
|
||||
*/
|
||||
StdIo.prototype.onDebug = function (/* ...msg */) {
|
||||
this.write(process.stdout, 'DEBUG', clc.magentaBright.bold, arguments);
|
||||
StdIo.prototype.onDebug = function (msg) {
|
||||
this.write(process.stdout, 'DEBUG', clc.magentaBright.bold, msg);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handler for the bridges "trace" event
|
||||
* @param {...*} msg - Any amount of messages that will be joined before logged
|
||||
* @param {String} msg - The message to be logged
|
||||
* @return {undefined}
|
||||
*/
|
||||
StdIo.prototype.onTrace = function (/* ...msg */) {
|
||||
this.write(process.stdout, 'TRACE', clc.cyanBright.bold, arguments);
|
||||
StdIo.prototype.onTrace = function (msg) {
|
||||
this.write(process.stdout, 'TRACE', clc.cyanBright.bold, msg);
|
||||
};
|
||||
|
||||
module.exports = StdIo;
|
||||
@ -1,6 +1,6 @@
|
||||
/* elasticsearch-js nodejs transport */
|
||||
|
||||
var http = require('http');
|
||||
var http = require('http')
|
||||
, _ = require('../Utils');
|
||||
|
||||
function NodeHttp() {
|
||||
|
||||
@ -55,12 +55,12 @@ function NodeHttp() {
|
||||
request.on('error', errorcb);
|
||||
}
|
||||
|
||||
if(method !== 'GET' && method !== 'HEAD') {
|
||||
if (method !== 'GET' && method !== 'HEAD') {
|
||||
request.write(body);
|
||||
}
|
||||
|
||||
request.end();
|
||||
};
|
||||
}
|
||||
|
||||
// Public functions
|
||||
return {
|
||||
@ -71,4 +71,6 @@ function NodeHttp() {
|
||||
head : _.bind(performRequest, this, 'HEAD')
|
||||
};
|
||||
|
||||
} ());
|
||||
}
|
||||
|
||||
module.exports = NodeHttp;
|
||||
Reference in New Issue
Block a user