Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request winstonjs#156 from FB55/master
Browse files Browse the repository at this point in the history
Added util.format-like functionality
  • Loading branch information
mmalecki committed Aug 9, 2012
2 parents 31dbaeb + a5950cb commit af5e1f7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
25 changes: 25 additions & 0 deletions lib/winston/common.js
Expand Up @@ -257,3 +257,28 @@ exports.serialize = function (obj, key) {

return msg;
};

//
// ### function format (arr)
// #### @arr {Array} The array that contains all parts of the message
// similar to util.format
// returns the formated string
// uses the original array and removes items as it proceeds
//
exports.format = function (arr) {
var msg = arr.shift();

if (typeof msg !== "string") {
return msg;
}

return msg.replace(/%[sdj%]/g, function (x){
switch (x.charAt(1)) {
case "j": return JSON.stringify(arr.shift());
case "s": return String(arr.shift());
case "d": return Number(arr.shift());
case "%": return "%";
default: return x;
}
});
};
22 changes: 12 additions & 10 deletions lib/winston/logger.js
Expand Up @@ -120,23 +120,25 @@ Logger.prototype.extend = function (target) {
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
Logger.prototype.log = function (level, msg) {
Logger.prototype.log = function (level) {
var self = this,
args = Array.prototype.slice.call(arguments, 1),
msg = common.format(args),
callback,
meta;

if (arguments.length === 3) {
if (typeof arguments[2] === 'function') {
if (args.length === 1) {
if (typeof args[0] === 'function') {
meta = {};
callback = arguments[2];
callback = args[0];
}
else if (typeof arguments[2] === 'object') {
meta = arguments[2];
else if (typeof args[0] === 'object') {
meta = args[0];
}
}
else if (arguments.length === 4) {
meta = arguments[2];
callback = arguments[3];
else if (args.length === 2) {
meta = args[0];
callback = args[1];
}

// If we should pad for levels, do so
Expand All @@ -150,7 +152,7 @@ Logger.prototype.log = function (level, msg) {
}
else if (self.emitErrs) {
self.emit('error', err);
};
}
}

if (this.transports.length === 0) {
Expand Down

0 comments on commit af5e1f7

Please sign in to comment.