Create a contextString and a real argsString key

This commit is contained in:
Guillaume Wuip 2014-11-02 17:11:16 +01:00
parent 7c6dcb8523
commit c1447eb09d

View file

@ -341,73 +341,16 @@
return this;
};
/**
* Console2.prototype.buildMessage
* Console2.prototype.buildArgs
*
* @param {Object} log The log object
* Build the args string
* ie. the string composed with the arguments send to the logger
*
* @param {Object} opt Optional options telling what to include in the message
* @param {Boolean} opt.tags Print Tags ? Default false.
* @param {Boolean} opt.location Print location ? Default false.
* @param {Boolean} opt.time Print time ? Default false.
* @param {Boolean} opt.date Print date ? Default false.
*
* @return {String} The message to print usualy.
* @param {Object} log
* @return {String} the string
*/
Console2.prototype.buildMessage = function (log, opt) {
if (!opt) {
opt = {};
}
opt.tags = opt.tags === true;
opt.location = opt.location === true;
opt.time = opt.time === true;
opt.date = opt.date === true;
var result = "", //final output
space = " ", //space between context parts
length = 0; //length of the context part (human readable string)
//ie. without escapted or colors caracters
if (opt.tags && log.context.tags) {
var tags = buildTags(log.context.tags);
result += applyColors(tags.msg, log.opt.tagsColors) + space;
length += tags.msgLength + space.length;
}
if (opt.location && log.context.location.filename && log.context.location.line) {
var infos = buildFileInfos(log.context.location, log.opt.fileColors, log.opt.lineColors);
result += infos.msg + space;
length += infos.msgLength + space.length;
}
if (opt.time && log.context.time) {
var time = buildTime(log.context.time);
result += applyColors(time.msg, log.opt.timeColors) + space;
length += time.msgLength + space.length;
}
if (opt.date && log.context.time) {
var date = buildDate(log.context.time);
result += applyColors(date.msg, log.opt.dateColors) + space;
length += date.msgLength + space.length;
}
if (length > 0) { //if there is context string
//add space according to the contextMediumSize
var offset = this.opt.contextMediumSize - length;
if (offset < 0) { //context string could be longer than medium size
offset = 0;
}
result += new Array(offset + this.opt.spaceSize).join(' ');
}
//It's time to log the args
Console2.prototype.buildArgs = function (log) {
var args = Array.prototype.slice.call(log.args, 0), //transform args in an array
msg = ""; //the log message
@ -450,9 +393,72 @@
}
}
result += msg;
return msg;
return result;
};
/**
* Console2.prototype.buildContext
*
* Build the context string
* ie. the string composed with arguments passed to Console2 context functions
*
* @param {Object} log The log object
*
* @param {Object} opt Optional options telling what to include in the message
* @param {Boolean} opt.tags Print Tags ? Default false.
* @param {Boolean} opt.location Print location ? Default false.
* @param {Boolean} opt.time Print time ? Default false.
* @param {Boolean} opt.date Print date ? Default false.
*
* @return {Object}
* @return {String} result
* @return {Int} length the "human readable" length of the result
*/
Console2.prototype.buildContext = function (log, opt) {
if (!opt) {
opt = {};
}
opt.tags = opt.tags === true;
opt.location = opt.location === true;
opt.time = opt.time === true;
opt.date = opt.date === true;
var result = "", //final output
space = " ", //space between context parts
length = 0; //length of the context part (human readable string)
//ie. without escapted or colors caracters
if (opt.tags && log.context.tags) {
var tags = buildTags(log.context.tags);
result += applyColors(tags.msg, log.opt.tagsColors) + space;
length += tags.msgLength + space.length;
}
if (opt.location && log.context.location.filename && log.context.location.line) {
var infos = buildFileInfos(log.context.location, log.opt.fileColors, log.opt.lineColors);
result += infos.msg + space;
length += infos.msgLength + space.length;
}
if (opt.time && log.context.time) {
var time = buildTime(log.context.time);
result += applyColors(time.msg, log.opt.timeColors) + space;
length += time.msgLength + space.length;
}
if (opt.date && log.context.time) {
var date = buildDate(log.context.time);
result += applyColors(date.msg, log.opt.dateColors) + space;
length += date.msgLength + space.length;
}
return {
result : result,
length : length
};
};
@ -525,18 +531,41 @@
location : location
},
args : arguments,
argsString : util.format.apply(console, arguments), //stringify arguments
opt : opt
};
//Build the string message
log.message = this.buildMessage(log, {
//It's time to build te result string
var offsetSpace = "";
//Build the context string
var context = this.buildContext(log, {
tags : this._tags.length > 0 || this.opt.alwaysTags,
location : this._location || this.opt.alwaysLocation,
time : this._time || this.opt.alwaysTime,
date : this._date || this.opt.alwaysDate
});
log.contextString = context.result;
//Build the args string
log.argsString = this.buildArgs(log);
//Generate the according number of space between context and args strings
if (context.length > 0) { //if there is context string
//add space according to the contextMediumSize
var offset = this.opt.contextMediumSize - context.length;
if (offset < 0) { //context string could be longer than medium size
offset = 0;
}
offsetSpace = new Array(offset + this.opt.spaceSize).join(' ');
}
//Finally, the message
log.message = log.contextString + offsetSpace + log.argsString;
//Emit events
this.emit('new', log, log.type);
this.emit(log.type, log);