Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format timestamp and meta independently of log function and allow timestamp to be passed into to log function #634

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 69 additions & 47 deletions lib/winston/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,22 @@ exports.clone = function (obj) {
// }
//
exports.log = function (options) {
var timestampFn = typeof options.timestamp === 'function'
? options.timestamp
: exports.timestamp,
timestamp = options.timestamp ? timestampFn() : null,
var timestamp = exports.formatTimestamp(options.timestamp),
showLevel = options.showLevel === undefined ? true : options.showLevel,
meta = options.meta !== null && options.meta !== undefined && !(options.meta instanceof Error)
? exports.clone(cycle.decycle(options.meta))
: options.meta || null,
meta = exports.formatMeta(options.meta, {
raw: options.raw,
json: options.json,
logstash: options.logstash,
prettyPrint: options.prettyPrint,
depth: options.depth,
colorize: options.colorize
}),
output;

//
// raw mode is intended for outputing winston as streaming JSON to STDOUT
//
if (options.raw) {
if (typeof meta !== 'object' && meta != null) {
meta = { meta: meta };
}
output = exports.clone(meta) || {};
output.level = options.level;
output.message = options.message.stripColors;
Expand All @@ -150,10 +149,6 @@ exports.log = function (options) {
// json mode is intended for pretty printing multi-line json to the terminal
//
if (options.json || true === options.logstash) {
if (typeof meta !== 'object' && meta != null) {
meta = { meta: meta };
}

output = exports.clone(meta) || {};
output.level = options.level;
output.message = output.message || '';
Expand Down Expand Up @@ -211,39 +206,7 @@ exports.log = function (options) {
: options.message;

if (meta !== null && meta !== undefined) {
if (meta && meta instanceof Error && meta.stack) {
meta = meta.stack;
}

if (typeof meta !== 'object') {
output += ' ' + meta;
}
else if (Object.keys(meta).length > 0) {
if (typeof options.prettyPrint === 'function') {
output += ' ' + options.prettyPrint(meta);
} else if (options.prettyPrint) {
output += ' ' + '\n' + util.inspect(meta, false, options.depth || null, options.colorize);
} else if (
options.humanReadableUnhandledException
&& Object.keys(meta).length === 5
&& meta.hasOwnProperty('date')
&& meta.hasOwnProperty('process')
&& meta.hasOwnProperty('os')
&& meta.hasOwnProperty('trace')
&& meta.hasOwnProperty('stack')) {

//
// If meta carries unhandled exception data serialize the stack nicely
//
var stack = meta.stack;
delete meta.stack;
delete meta.trace;
output += ' ' + exports.serialize(meta);
output += '\n' + stack.join('\n');
} else {
output += ' ' + exports.serialize(meta);
}
}
output += ' ' + meta;
}

return output;
Expand Down Expand Up @@ -394,3 +357,62 @@ exports.tailFile = function tail(options, callback) {

return destroy;
};

exports.formatTimestamp = function (timestamp) {
if (typeof timestamp === 'function') {
timestamp = timestamp();
} else if (timestamp && new Date(timestamp).toString() !== 'Invalid Date') {
null;
} else {
timestamp = exports.timestamp;
}
timestamp = timestamp ? timestamp : null;

return timestamp;
};

exports.formatMeta = function (meta, options) {
meta = meta !== null && meta !== undefined && !(meta instanceof Error)
? exports.clone(cycle.decycle(meta))
: meta || null;

if (meta !== null && meta !== undefined) {
if (
(options.raw || options.json || true === options.logstash)
&& (typeof meta !== 'object' && meta != null)
) {
meta = { meta: meta };
} else if (meta instanceof Error && meta.stack) {
meta = meta.stack;
} else if (typeof meta === 'object') {
if (Object.keys(meta).length > 0) {
if (typeof options.prettyPrint === 'function') {
meta = options.prettyPrint(meta);
} else if (options.prettyPrint) {
meta = '\n' + util.inspect(meta, false, options.depth || null, options.colorize);
} else if (
options.humanReadableUnhandledException
&& Object.keys(meta).length === 5
&& meta.hasOwnProperty('date')
&& meta.hasOwnProperty('process')
&& meta.hasOwnProperty('os')
&& meta.hasOwnProperty('trace')
&& meta.hasOwnProperty('stack')) {

//
// If meta carries unhandled exception data serialize the stack nicely
//
var stack = meta.stack;
delete meta.stack;
delete meta.trace;
meta = exports.serialize(meta);
meta += '\n' + stack.join('\n');
} else {
meta = exports.serialize(meta);
}
}
}
}

return meta;
};
55 changes: 43 additions & 12 deletions lib/winston/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,24 @@ 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) {
Logger.prototype.log = function (timestamp, level) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot change this method signature.

var self = this,
args = Array.prototype.slice.call(arguments, 1);
args;

if (new Date(timestamp).toString() === 'Invalid Date') {
args = Array.prototype.slice.call(arguments, 1);
level = timestamp;
timestamp = undefined;
} else {
args = Array.prototype.slice.call(arguments, 2);
}

while(args[args.length - 1] === null) {
args.pop();
}

var callback = typeof args[args.length - 1] === 'function' ? args.pop() : null,
meta = typeof args[args.length - 1] === 'object' && Object.prototype.toString.call(args[args.length - 1]) !== '[object RegExp]' ? args.pop() : {},
meta = typeof args[args.length - 1] === 'object' && Object.prototype.toString.call(args[args.length - 1]) !== '[object RegExp]' ? args.pop() : null,
msg = util.format.apply(null, args);

// If we should pad for levels, do so
Expand Down Expand Up @@ -183,15 +191,28 @@ Logger.prototype.log = function (level) {
var transport = self.transports[name];
if ((transport.level && self.levels[transport.level] <= self.levels[level])
|| (!transport.level && self.levels[self.level] <= self.levels[level])) {
transport.log(level, msg, meta, function (err) {
if (err) {
err.transport = transport;
cb(err);
return next();
}
self.emit('logging', transport, level, msg, meta);
next();
});

if (getParamNames(transport.log).indexOf('timestamp') != -1) {
transport.log(timestamp, level, msg, meta, function (err) {
if (err) {
err.transport = transport;
cb(err);
return next();
}
self.emit('logging', transport, level, msg, meta);
next();
});
} else {
transport.log(level, msg, meta, function (err) {
if (err) {
err.transport = transport;
cb(err);
return next();
}
self.emit('logging', level, msg, meta);
next();
});
}
} else {
next();
}
Expand Down Expand Up @@ -695,3 +716,13 @@ Logger.prototype._onError = function (transport, err) {
this.emit('error', err, transport);
}
};

function getParamNames(func) {
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
var ARGUMENT_NAMES = /([^\s,]+)/g;
var fnStr = func.toString().replace(STRIP_COMMENTS, '');
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
if(result === null)
result = [];
return result;
}
4 changes: 2 additions & 2 deletions lib/winston/transports/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Console.prototype.name = 'console';
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
Console.prototype.log = function (level, msg, meta, callback) {
Console.prototype.log = function (timestamp, level, msg, meta, callback) {
if (this.silent) {
return callback(null, true);
}
Expand All @@ -71,7 +71,7 @@ Console.prototype.log = function (level, msg, meta, callback) {
message: msg,
meta: meta,
stringify: this.stringify,
timestamp: this.timestamp,
timestamp: timestamp || this.timestamp,
showLevel: this.showLevel,
prettyPrint: this.prettyPrint,
raw: this.raw,
Expand Down
4 changes: 2 additions & 2 deletions lib/winston/transports/daily-rotate-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ DailyRotateFile.prototype.name = 'dailyRotateFile';
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
DailyRotateFile.prototype.log = function (level, msg, meta, callback) {
DailyRotateFile.prototype.log = function (timestamp, level, msg, meta, callback) {
if (this.silent) {
return callback(null, true);
}
Expand All @@ -171,7 +171,7 @@ DailyRotateFile.prototype.log = function (level, msg, meta, callback) {
json: this.json,
colorize: this.colorize,
prettyPrint: this.prettyPrint,
timestamp: this.timestamp,
timestamp: timestamp || this.timestamp,
label: this.label,
stringify: this.stringify,
showLevel: this.showLevel,
Expand Down
4 changes: 2 additions & 2 deletions lib/winston/transports/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ File.prototype.name = 'file';
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
File.prototype.log = function (level, msg, meta, callback) {
File.prototype.log = function (timestamp, level, msg, meta, callback) {
if (this.silent) {
return callback(null, true);
}
Expand Down Expand Up @@ -152,7 +152,7 @@ File.prototype.log = function (level, msg, meta, callback) {
logstash: this.logstash,
colorize: this.colorize,
prettyPrint: this.prettyPrint,
timestamp: this.timestamp,
timestamp: timestamp || this.timestamp,
showLevel: this.showLevel,
stringify: this.stringify,
label: this.label,
Expand Down
4 changes: 2 additions & 2 deletions lib/winston/transports/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Http.prototype._request = function (options, callback) {
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
Http.prototype.log = function (level, msg, meta, callback) {
Http.prototype.log = function (timestamp, level, msg, meta, callback) {
var self = this;

if (typeof meta === 'function') {
Expand Down Expand Up @@ -180,7 +180,7 @@ Http.prototype.query = function (options, callback) {
//
Http.prototype.stream = function (options) {
options = options || {};

var self = this,
stream = new Stream,
req,
Expand Down
4 changes: 2 additions & 2 deletions lib/winston/transports/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Memory.prototype.name = 'memory';
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
Memory.prototype.log = function (level, msg, meta, callback) {
Memory.prototype.log = function (timestamp, level, msg, meta, callback) {
if (this.silent) {
return callback(null, true);
}
Expand All @@ -64,7 +64,7 @@ Memory.prototype.log = function (level, msg, meta, callback) {
message: msg,
meta: meta,
stringify: this.stringify,
timestamp: this.timestamp,
timestamp: timestamp || this.timestamp,
prettyPrint: this.prettyPrint,
raw: this.raw,
label: this.label,
Expand Down
4 changes: 2 additions & 2 deletions lib/winston/transports/webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Webhook.prototype.name = 'webhook';
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
Webhook.prototype.log = function (level, msg, meta, callback) {
Webhook.prototype.log = function (timestamp, level, msg, meta, callback) {
if (this.silent) {
return callback(null, true);
}
Expand Down Expand Up @@ -133,7 +133,7 @@ Webhook.prototype.log = function (level, msg, meta, callback) {
//

var params = common.clone(meta) || {};
params.timestamp = new Date();
params.timestamp = timestamp || new Date();
params.message = msg;
params.level = level;

Expand Down