diff --git a/lib/console2.js b/lib/console2.js index 8655566..58521a4 100644 --- a/lib/console2.js +++ b/lib/console2.js @@ -16,6 +16,23 @@ */ 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 * @@ -62,8 +79,48 @@ * Console2 * * @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 @@ -95,16 +152,11 @@ /** * _location * - * Store the file and line + * Should we log filename and line number ? * - { - filename : 'main.js', - line : 6 - } - * - * @type {Object} + * @type {Boolean} */ - this._location = {}; + this._location = false; /** * _reset @@ -168,25 +220,9 @@ * Console2.prototype.file * * Log the file name + line - * @param {String} filename Optional - * @param {Number} line Optional */ - Console2.prototype.file = Console2.prototype.f = function (filename, line) { - - 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; + Console2.prototype.file = Console2.prototype.f = function () { + this._location = true; return this; }; @@ -196,11 +232,13 @@ * Console2.prototype.buildMessage * * @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 {Boolean} opt.location Optional. Print location ? Default false. - * @param {Boolean} opt.time Optional. Print time ? Default false. - * @param {Boolean} opt.date Optional. Print date ? Default false. + * + * @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. */ Console2.prototype.buildMessage = function (log, opt) { @@ -232,9 +270,15 @@ result += buildFileInfos(log.context.location); } - if (result.length > 0) { - //add tab - result += " "; + if (result.length > 0) { //if there is context string + //add space according to the contextMediumSize + 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; @@ -252,29 +296,43 @@ * Create a new logger * You can then use it with console.myNewLogger * - * @param {String} name The name of the logger. - * @param {String} color Optional. Color of the console output. Default blue. - * See text colors from https://github.com/Marak/colors.js - * @param {Object} opt Options object - * @param {Boolean} opt.logInConsole Optional. Default true. - * Should the logger print to the console ? - * @param {Boolean} opt.logInFile Optional. Default true. - * If the log should be save in file. + * @param {String} name The name of the logger. + * @param {String} color Optional. Color of the console output. Default blue. + * See text colors from https://github.com/Marak/colors.js + * + * @param {Object} opt Optional options object. @see Console2 opt for default values. + * @param {Boolean} opt.logInConsole Should the logger print to the console ? + * @param {Boolean} opt.logInFile 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) { if (!opt) { opt = {}; } + + if (!name) { + throw new Error("No name given at addLogger"); + } - opt.name = name; - opt.color = color || "blue"; - opt.type = opt.type || opt.name; - opt.logInConsole = opt.logInConsole || true; - opt.logInFile = opt.logInFile || true; + opt.name = name; + opt.color = color || this.opt.color; + opt.type = opt.type || opt.name; + opt.logInConsole = opt.logInConsole || this.opt.logInConsole; + 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 () { + var location = getLocation(); + var time = Date.now(); + //Let's build the log object var log = { @@ -282,8 +340,8 @@ context : { tags : this._tags, file : this._location, - time : Date.now(), - location : this._location + time : time, + location : location }, args : arguments, argsString : util.format.apply(console, arguments), //stringify arguments @@ -292,10 +350,10 @@ //Build the string message log.message = this.buildMessage(log, { - tags : this._tags.length > 0 || false, - location : this._location.filename || false, - time : this._time || false, - date : this._date || false + 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 }); //Emit events