diff --git a/examples/console2.js b/examples/console2.js index d66e39b..0a8320f 100644 --- a/examples/console2.js +++ b/examples/console2.js @@ -14,6 +14,8 @@ console.log("A string %s and a number %d", "hello", "123"); //you can use printf //Time console.time().log("Print the full time"); console.date().log("Just print the date"); +//custom time +console.time((new Date()).setFullYear(1999)).log("Custom time"); //Tags console.tag("My Tag").log("Add a tag"); @@ -22,6 +24,7 @@ console.tag({msg : 'my-tag', colors : ['red', 'inverse']}).log("Use colors.js co //File and line number console.file().log("Print the file and the line of the call"); +console.file('myFile.js', 42).log("Custom filename and line"); //Object console.log({just : 'an object'}); diff --git a/lib/console2.js b/lib/console2.js index e28397e..65327ce 100644 --- a/lib/console2.js +++ b/lib/console2.js @@ -269,7 +269,7 @@ * * Log time (full date) ? * - * @type {Boolean} + * @type {Boolean|Number|String} */ this._time = false; @@ -287,7 +287,7 @@ * * Should we log filename and line number ? * - * @type {Boolean} + * @type {Boolean|Object} */ this._location = false; @@ -325,9 +325,11 @@ * Console2.prototype.time * * Log the time + * + * @param {String|Number} time Optional time if need to override. */ - Console2.prototype.time = function () { - this._time = true; + Console2.prototype.time = function (time) { + this._time = time || true; return this; }; @@ -362,9 +364,21 @@ * Console2.prototype.file * * Log the file name + line + * You could force the filename and line by passing args. + * + * @param {String} file Filename. Optional + * @param {String|Number} line Line. Optional */ - Console2.prototype.file = Console2.prototype.f = function () { - this._location = true; + Console2.prototype.file = Console2.prototype.f = function (file, line) { + + if (file || line) { + this._location = { + filename : file, + line : line + }; + } else { + this._location = true; + } return this; }; @@ -569,8 +583,11 @@ */ this[name] = function () { - var location = getLocation(); - var time = Date.now(); + //use this._location if it's an object (custom location) + //or build the location + var location = (this._location || this.opt.alwaysLocation) === true ? getLocation() : this._location; + + var time = (typeof this._time !== 'boolean') ? this._time : Date.now(); // Let's build the log object @@ -578,8 +595,8 @@ type : opt.type || name, show : { tags : this._tags.length > 0 || this.opt.alwaysTags || opt.defaultTags.length > 0, - location : this._location || this.opt.alwaysLocation, - time : this._time || this.opt.alwaysTime, + location : this._location !== false || this.opt.alwaysLocation, + time : this._time !== false || this.opt.alwaysTime, date : this._date || this.opt.alwaysDate }, context : {