forked from mirrors/Scribe.js
Adding multiline tags
This commit is contained in:
parent
41a815a764
commit
21e3342d01
1 changed files with 72 additions and 66 deletions
138
lib/console2.js
138
lib/console2.js
|
@ -86,12 +86,13 @@
|
||||||
length = 0;
|
length = 0;
|
||||||
|
|
||||||
tags.forEach(function (tag) {
|
tags.forEach(function (tag) {
|
||||||
if (tag !== null && typeof tag === 'object') {
|
if(typeof tag === "undefined" || tag === null) return;
|
||||||
|
if (typeof tag === 'object') {
|
||||||
result += applyColors('[' + tag.msg + ']', tag.colors);
|
result += applyColors('[' + tag.msg + ']', tag.colors);
|
||||||
length += ("" + tag.msg).length + 2;
|
length += tag.msg.toString().length + 2;
|
||||||
} else {
|
} else {
|
||||||
result += '[' + tag + ']';
|
result += '[' + tag + ']';
|
||||||
length += ("" + tag).length + 2;
|
length += tag.toString().length + 2;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -116,16 +117,12 @@
|
||||||
* @return {int} msgLength
|
* @return {int} msgLength
|
||||||
*/
|
*/
|
||||||
var buildFileInfos = function (infos, fileColors, lineColors) {
|
var buildFileInfos = function (infos, fileColors, lineColors) {
|
||||||
var result = '[' +
|
|
||||||
applyColors(infos.filename, fileColors) +
|
|
||||||
':' +
|
|
||||||
applyColors(infos.line, lineColors) +
|
|
||||||
']';
|
|
||||||
var length = ('[' + infos.filename + ':' + infos.line + ']').length;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
msg : result,
|
msg : '[' + applyColors(infos.filename, fileColors) + ':' +
|
||||||
msgLength : length
|
applyColors(infos.line, lineColors) + ']',
|
||||||
|
msgLength : (infos.filename.toString() +
|
||||||
|
infos.line.toString()).length + 3
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -293,7 +290,7 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//inherits form EventEmitter.prototype
|
// inherits form EventEmitter.prototype
|
||||||
util.inherits(Console2, EventEmitter);
|
util.inherits(Console2, EventEmitter);
|
||||||
|
|
||||||
|
|
||||||
|
@ -345,6 +342,14 @@
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var spaceLUT = {};
|
||||||
|
var getSpaces = function(offset){
|
||||||
|
if(typeof spaceLUT[offset] === "undefined"){
|
||||||
|
spaceLUT[offset] = new Array(offset).join(' ');
|
||||||
|
}
|
||||||
|
return spaceLUT[offset];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Console2.prototype.buildArgs
|
* Console2.prototype.buildArgs
|
||||||
*
|
*
|
||||||
|
@ -354,50 +359,59 @@
|
||||||
* @param {Object} log
|
* @param {Object} log
|
||||||
* @return {String} the string
|
* @return {String} the string
|
||||||
*/
|
*/
|
||||||
Console2.prototype.buildArgs = function (log) {
|
Console2.prototype.buildArgs = function (log, offset, context) {
|
||||||
|
|
||||||
var args = Array.prototype.slice.call(log.args, 0), //transform args in an array
|
var args = Array.prototype.slice.call(log.args, 0), // transform args in an array
|
||||||
msg = ""; //the log message
|
msg = "", // the log message; with newline
|
||||||
|
raw = ""; // no-newlines
|
||||||
|
|
||||||
//if all args are string or number, format args as usual
|
// if all args are string or number, format args as usual
|
||||||
if (areAllStringOrNumber(args)) {
|
if (areAllStringOrNumber(args)) {
|
||||||
msg = util.format.apply(this, args);
|
raw = util.format.apply(util, args);
|
||||||
|
msg = getSpaces(offset) + raw;
|
||||||
|
|
||||||
//if objects or array present
|
// if objects or array present
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
var delimiter = '\n',
|
var delimiter = '',
|
||||||
multiLines = false; //if the log need multiples lines (ie. object, array)
|
multiLines = false; // if the log need multiples lines (ie. object, array)
|
||||||
|
|
||||||
//Process arg one by one
|
// Process arg one by one
|
||||||
args.forEach(function (arg) {
|
args.forEach(function (arg, index) {
|
||||||
|
|
||||||
//if arg is an object / array
|
// if arg is an object / array
|
||||||
//use multiples lines
|
// use multiples lines
|
||||||
if (arg !== null && typeof arg === 'object') {
|
if (arg !== null && typeof arg === 'object') {
|
||||||
|
|
||||||
msg += delimiter;
|
msg += delimiter + JSON.stringify(arg, null, 2);
|
||||||
|
|
||||||
msg += JSON.stringify(arg, null, 2);
|
|
||||||
|
|
||||||
multiLines = true;
|
multiLines = true;
|
||||||
|
|
||||||
//for "normal" args
|
// for "normal" args
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (multiLines) {
|
if (multiLines) {
|
||||||
msg += delimiter;
|
msg += delimiter;
|
||||||
}
|
}
|
||||||
msg += arg + "";
|
|
||||||
|
msg += arg.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(index === 0){
|
||||||
|
delimiter = '\n';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
msg = getSpaces(offset) + msg;
|
||||||
|
|
||||||
if (multiLines) {
|
if (multiLines) {
|
||||||
msg += '\n';
|
msg = msg.replace(/\n/gm, '\n' + context + getSpaces(offset));
|
||||||
msg = msg.replace(/\n/gm, '\n' + new Array(this.opt.spaceSize).join(' '));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return msg;
|
return {
|
||||||
|
msg : msg,
|
||||||
|
raw : raw
|
||||||
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -430,10 +444,10 @@
|
||||||
opt.time = opt.time === true;
|
opt.time = opt.time === true;
|
||||||
opt.date = opt.date === true;
|
opt.date = opt.date === true;
|
||||||
|
|
||||||
var result = "", //final output
|
var result = "", // final output
|
||||||
space = " ", //space between context parts
|
space = " ", // space between context parts
|
||||||
length = 0; //length of the context part (human readable string)
|
length = 0; // length of the context part (human readable string)
|
||||||
//ie. without escapted or colors caracters
|
// ie. without escapted or colors caracters
|
||||||
|
|
||||||
if (opt.tags && log.context.tags) {
|
if (opt.tags && log.context.tags) {
|
||||||
var tags = buildTags(log.context.tags);
|
var tags = buildTags(log.context.tags);
|
||||||
|
@ -477,7 +491,7 @@
|
||||||
* @param {String} name The name of the logger.
|
* @param {String} name The name of the logger.
|
||||||
* @param {Array|String} colors Optional colorsjs colors of the console output.
|
* @param {Array|String} colors Optional colorsjs colors of the console output.
|
||||||
* Override constructor default.
|
* Override constructor default.
|
||||||
* See text colors from https://github.com/Marak/colors.js
|
* See text colors from https:// github.com/Marak/colors.js
|
||||||
*
|
*
|
||||||
* @param {Object} opt Optional options object. @see Console2 opt for default values.
|
* @param {Object} opt Optional options object. @see Console2 opt for default values.
|
||||||
*
|
*
|
||||||
|
@ -530,7 +544,7 @@
|
||||||
var location = getLocation();
|
var location = getLocation();
|
||||||
var time = Date.now();
|
var time = Date.now();
|
||||||
|
|
||||||
//Let's build the log object
|
// Let's build the log object
|
||||||
|
|
||||||
var log = {
|
var log = {
|
||||||
type : opt.type || name,
|
type : opt.type || name,
|
||||||
|
@ -550,35 +564,27 @@
|
||||||
opt : opt
|
opt : opt
|
||||||
};
|
};
|
||||||
|
|
||||||
//It's time to build the result string
|
// Build the context string
|
||||||
|
|
||||||
var offsetSpace = "";
|
|
||||||
|
|
||||||
//Build the context string
|
|
||||||
var context = this.buildContext(log, log.show);
|
var context = this.buildContext(log, log.show);
|
||||||
|
|
||||||
|
// Generate the according number of space between context and args strings
|
||||||
|
// add space according to the contextMediumSize
|
||||||
|
var offset = Math.max(0, this.opt.contextMediumSize - context.length);
|
||||||
|
|
||||||
|
// save context
|
||||||
log.contextString = context.result;
|
log.contextString = context.result;
|
||||||
|
|
||||||
//Build the args string
|
// Build the args string
|
||||||
log.argsString = this.buildArgs(log);
|
var built = this.buildArgs(log, offset, log.contextString);
|
||||||
|
|
||||||
//Generate the according number of space between context and args strings
|
// newline enabled
|
||||||
if (context.length > 0) { //if there is context string
|
log.argsString = built.msg;
|
||||||
//add space according to the contextMediumSize
|
|
||||||
var offset = this.opt.contextMediumSize - context.length;
|
|
||||||
|
|
||||||
if (offset < 0) { //context string could be longer than medium size
|
// Finally, the message
|
||||||
offset = 0;
|
log.message = log.contextString + log.argsString;
|
||||||
}
|
|
||||||
|
|
||||||
offsetSpace = new Array(offset + this.opt.spaceSize).join(' ');
|
// Emit events
|
||||||
}
|
this.emit('new', log, log.type); // 'new' event
|
||||||
|
|
||||||
//Finally, the message
|
|
||||||
log.message = log.contextString + offsetSpace + log.argsString;
|
|
||||||
|
|
||||||
//Emit events
|
|
||||||
this.emit('new', log, log.type); //'new' event
|
|
||||||
|
|
||||||
var msg = log.type;
|
var msg = log.type;
|
||||||
|
|
||||||
|
@ -590,10 +596,10 @@
|
||||||
if (msg === 'error') {
|
if (msg === 'error') {
|
||||||
msg += 'Event';
|
msg += 'Event';
|
||||||
}
|
}
|
||||||
this.emit(msg, log); //`log.type` event
|
this.emit(msg, log); // `log.type` event
|
||||||
|
|
||||||
//If the logger should print the message
|
// If the logger should print the message
|
||||||
//Print it
|
// Print it
|
||||||
if (opt.logInConsole) {
|
if (opt.logInConsole) {
|
||||||
global.console.log(applyColors(log.message, opt.colors));
|
global.console.log(applyColors(log.message, opt.colors));
|
||||||
}
|
}
|
||||||
|
@ -609,8 +615,8 @@
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//Keep the old console
|
// Keep the old console
|
||||||
//use also `global.console`
|
// use also `global.console`
|
||||||
Console2.prototype.Original = consoleOriginal;
|
Console2.prototype.Original = consoleOriginal;
|
||||||
|
|
||||||
module.exports = Console2;
|
module.exports = Console2;
|
||||||
|
|
Loading…
Add table
Reference in a new issue