forked from mirrors/Scribe.js
Add options + refactor location + indent output
This commit is contained in:
parent
7c22bdcf16
commit
bc2ceb7b91
1 changed files with 112 additions and 54 deletions
166
lib/console2.js
166
lib/console2.js
|
@ -16,6 +16,23 @@
|
||||||
*/
|
*/
|
||||||
var consoleOriginal = console;
|
var consoleOriginal = console;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getLocation
|
||||||
|
*
|
||||||
|
* Get location of log.
|
||||||
|
* ie. filename and line number
|
||||||
|
*/
|
||||||
|
var getLocation = function () {
|
||||||
|
|
||||||
|
var st = stack()[2],
|
||||||
|
result = {};
|
||||||
|
|
||||||
|
result.filename = path.basename(st.getFileName());
|
||||||
|
result.line = st.getLineNumber();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* buildTime
|
* buildTime
|
||||||
*
|
*
|
||||||
|
@ -62,8 +79,48 @@
|
||||||
* Console2
|
* Console2
|
||||||
*
|
*
|
||||||
* @constructor
|
* @constructor
|
||||||
|
*
|
||||||
|
* @param {Object} opt Optional default options for all loggers.
|
||||||
|
* @param {Boolean} opt.logInConsole Should all loggers print to console by default ? Default true.
|
||||||
|
* @param {Boolean} opt.logInFile Should all loggers saver log in file by default ? Default true.
|
||||||
|
*
|
||||||
|
* @param {int} opt.contextMediumSize Medium size of the context part of a log message.
|
||||||
|
* Used when calculating indent. Default to 45.
|
||||||
|
* @param {int} opt.spaceSize Space between context part and log part. Default to 4.
|
||||||
|
* @param {String} opt.color Default color output for all loggers. Default blue.
|
||||||
|
*
|
||||||
|
* @param {Boolean} opt.alwaysTags Always print tags (even without tag() ). Default false.
|
||||||
|
* @param {Boolean} opt.alwaysLocation Always print location (even without file() ). Default false.
|
||||||
|
* @param {Boolean} opt.alwaysTime Always print time (even without time() ). Default false.
|
||||||
|
* @param {Boolean} opt.alwaysDate Always print date (even without date() ). Default false.
|
||||||
*/
|
*/
|
||||||
var Console2 = function () {
|
var Console2 = function (opt) {
|
||||||
|
|
||||||
|
if (!opt) {
|
||||||
|
opt = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* opt
|
||||||
|
*
|
||||||
|
* Constructor opt.
|
||||||
|
* Setting default.
|
||||||
|
*/
|
||||||
|
this.opt = {
|
||||||
|
logInConsole : opt.logInConsole || true,
|
||||||
|
logInFile : opt.logInFile || true,
|
||||||
|
|
||||||
|
contextMediumSize : opt.contextMediumSize || 45,
|
||||||
|
spaceSize : opt.spaceSize || 4,
|
||||||
|
|
||||||
|
color : opt.color || "blue",
|
||||||
|
|
||||||
|
alwaysTags : opt.alwaysTags || false,
|
||||||
|
alwaysLocation : opt.alwaysLocation || false,
|
||||||
|
alwaysTime : opt.alwaysTime || false,
|
||||||
|
alwaysDate : opt.alwaysDate || false
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* _tags
|
* _tags
|
||||||
|
@ -95,16 +152,11 @@
|
||||||
/**
|
/**
|
||||||
* _location
|
* _location
|
||||||
*
|
*
|
||||||
* Store the file and line
|
* Should we log filename and line number ?
|
||||||
*
|
*
|
||||||
{
|
* @type {Boolean}
|
||||||
filename : 'main.js',
|
|
||||||
line : 6
|
|
||||||
}
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
*/
|
||||||
this._location = {};
|
this._location = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* _reset
|
* _reset
|
||||||
|
@ -168,25 +220,9 @@
|
||||||
* Console2.prototype.file
|
* Console2.prototype.file
|
||||||
*
|
*
|
||||||
* Log the file name + line
|
* Log the file name + line
|
||||||
* @param {String} filename Optional
|
|
||||||
* @param {Number} line Optional
|
|
||||||
*/
|
*/
|
||||||
Console2.prototype.file = Console2.prototype.f = function (filename, line) {
|
Console2.prototype.file = Console2.prototype.f = function () {
|
||||||
|
this._location = true;
|
||||||
var st = stack()[1];
|
|
||||||
|
|
||||||
if (!filename) {
|
|
||||||
filename = path.basename(st.getFileName());
|
|
||||||
} else {
|
|
||||||
filename = path.basename(filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!line) {
|
|
||||||
line = st.getLineNumber();
|
|
||||||
}
|
|
||||||
|
|
||||||
this._location.filename = filename;
|
|
||||||
this._location.line = line;
|
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
@ -196,11 +232,13 @@
|
||||||
* Console2.prototype.buildMessage
|
* Console2.prototype.buildMessage
|
||||||
*
|
*
|
||||||
* @param {Object} log The log object
|
* @param {Object} log The log object
|
||||||
* @param {Object} opt Optional. Options telling what to include in the message
|
*
|
||||||
* @param {Boolean} opt.tags Optional. Print Tags ? Default false.
|
* @param {Object} opt Optional options telling what to include in the message
|
||||||
* @param {Boolean} opt.location Optional. Print location ? Default false.
|
* @param {Boolean} opt.tags Print Tags ? Default false.
|
||||||
* @param {Boolean} opt.time Optional. Print time ? Default false.
|
* @param {Boolean} opt.location Print location ? Default false.
|
||||||
* @param {Boolean} opt.date Optional. Print date ? 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.
|
* @return {String} The message to print usualy.
|
||||||
*/
|
*/
|
||||||
Console2.prototype.buildMessage = function (log, opt) {
|
Console2.prototype.buildMessage = function (log, opt) {
|
||||||
|
@ -232,9 +270,15 @@
|
||||||
result += buildFileInfos(log.context.location);
|
result += buildFileInfos(log.context.location);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.length > 0) {
|
if (result.length > 0) { //if there is context string
|
||||||
//add tab
|
//add space according to the contextMediumSize
|
||||||
result += " ";
|
var offset = this.opt.contextMediumSize - result.length;
|
||||||
|
|
||||||
|
if (offset < 0) { //context string could be longer than medium size
|
||||||
|
offset = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
result += new Array(offset + this.opt.spaceSize).join(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
result += log.argsString;
|
result += log.argsString;
|
||||||
|
@ -252,29 +296,43 @@
|
||||||
* Create a new logger
|
* Create a new logger
|
||||||
* You can then use it with console.myNewLogger
|
* You can then use it with console.myNewLogger
|
||||||
*
|
*
|
||||||
* @param {String} name The name of the logger.
|
* @param {String} name The name of the logger.
|
||||||
* @param {String} color Optional. Color of the console output. Default blue.
|
* @param {String} color Optional. Color of the console output. Default blue.
|
||||||
* See text colors from https://github.com/Marak/colors.js
|
* See text colors from https://github.com/Marak/colors.js
|
||||||
* @param {Object} opt Options object
|
*
|
||||||
* @param {Boolean} opt.logInConsole Optional. Default true.
|
* @param {Object} opt Optional options object. @see Console2 opt for default values.
|
||||||
* Should the logger print to the console ?
|
* @param {Boolean} opt.logInConsole Should the logger print to the console ?
|
||||||
* @param {Boolean} opt.logInFile Optional. Default true.
|
* @param {Boolean} opt.logInFile If the log should be save in file.
|
||||||
* If the log should be save in file.
|
* @param {Boolean} opt.alwaysTags Always print tags (even without tag() )
|
||||||
|
* @param {Boolean} opt.alwaysLocation Always print location (even without file() )
|
||||||
|
* @param {Boolean} opt.alwaysTime Always print time (even without time() )
|
||||||
|
* @param {Boolean} opt.alwaysDate Always print date (even without date() )
|
||||||
*/
|
*/
|
||||||
Console2.prototype.addLogger = function (name, color, opt) {
|
Console2.prototype.addLogger = function (name, color, opt) {
|
||||||
|
|
||||||
if (!opt) {
|
if (!opt) {
|
||||||
opt = {};
|
opt = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!name) {
|
||||||
|
throw new Error("No name given at addLogger");
|
||||||
|
}
|
||||||
|
|
||||||
opt.name = name;
|
opt.name = name;
|
||||||
opt.color = color || "blue";
|
opt.color = color || this.opt.color;
|
||||||
opt.type = opt.type || opt.name;
|
opt.type = opt.type || opt.name;
|
||||||
opt.logInConsole = opt.logInConsole || true;
|
opt.logInConsole = opt.logInConsole || this.opt.logInConsole;
|
||||||
opt.logInFile = opt.logInFile || true;
|
opt.logInFile = opt.logInFile || this.opt.logInFile;
|
||||||
|
opt.alwaysTags = opt.alwaysTags || this.opt.alwaysTags;
|
||||||
|
opt.alwaysLocation = opt.alwaysLocation || this.opt.alwaysLocation;
|
||||||
|
opt.alwaysTime = opt.alwaysTime || this.opt.alwaysTime;
|
||||||
|
opt.alwaysDate = opt.alwaysDate || this.opt.alwaysDate;
|
||||||
|
|
||||||
this[name] = function () {
|
this[name] = function () {
|
||||||
|
|
||||||
|
var location = getLocation();
|
||||||
|
var time = Date.now();
|
||||||
|
|
||||||
//Let's build the log object
|
//Let's build the log object
|
||||||
|
|
||||||
var log = {
|
var log = {
|
||||||
|
@ -282,8 +340,8 @@
|
||||||
context : {
|
context : {
|
||||||
tags : this._tags,
|
tags : this._tags,
|
||||||
file : this._location,
|
file : this._location,
|
||||||
time : Date.now(),
|
time : time,
|
||||||
location : this._location
|
location : location
|
||||||
},
|
},
|
||||||
args : arguments,
|
args : arguments,
|
||||||
argsString : util.format.apply(console, arguments), //stringify arguments
|
argsString : util.format.apply(console, arguments), //stringify arguments
|
||||||
|
@ -292,10 +350,10 @@
|
||||||
|
|
||||||
//Build the string message
|
//Build the string message
|
||||||
log.message = this.buildMessage(log, {
|
log.message = this.buildMessage(log, {
|
||||||
tags : this._tags.length > 0 || false,
|
tags : this._tags.length > 0 || this.opt.alwaysTags,
|
||||||
location : this._location.filename || false,
|
location : this._location || this.opt.alwaysLocation,
|
||||||
time : this._time || false,
|
time : this._time || this.opt.alwaysTime,
|
||||||
date : this._date || false
|
date : this._date || this.opt.alwaysDate
|
||||||
});
|
});
|
||||||
|
|
||||||
//Emit events
|
//Emit events
|
||||||
|
|
Loading…
Add table
Reference in a new issue