DRY out some code to make it clearer that these do the same thing.

This commit is contained in:
Josh Wilsdon 2012-10-23 23:06:44 -07:00 committed by Trent Mick
parent 677a485348
commit b450b015b3

View file

@ -695,271 +695,67 @@ Logger.prototype._emit = function (rec) {
};
}
/**
* build a log emitter function for level minLevel.
*
*/
function logEmitter(minLevel) {
return function () {
var fields = null, msgArgs = null;
if (arguments.length === 0) { // `log.<level>()`
return (this._level <= minLevel);
} else if (this._level > minLevel) {
return;
} else if (arguments[0] instanceof Error) {
// `log.<level>(err, ...)`
fields = {err: errSerializer(arguments[0])};
if (arguments.length === 1) {
msgArgs = [fields.err.message];
} else {
msgArgs = Array.prototype.slice.call(arguments, 1);
}
} else if (typeof (arguments[0]) === 'string') { // `log.<level>(msg, ...)`
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
} else if (Buffer.isBuffer(arguments[0])) { // `log.<level>(buf, ...)`
// Almost certainly an error, show `inspect(buf)`. See bunyan issue #35.
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
msgArgs[0] = util.inspect(msgArgs[0]);
} else { // `log.<level>(fields, msg, ...)`
fields = arguments[0];
msgArgs = Array.prototype.slice.call(arguments, 1);
}
var rec = this._mkRecord(fields, minLevel, msgArgs);
this._emit(rec);
}
}
/**
* Log a record at TRACE level.
* The functions below log a record at a specific level.
*
* Usages:
* log.trace() -> boolean is-trace-enabled
* log.trace(<Error> err, [<string> msg, ...])
* log.trace(<string> msg, ...)
* log.trace(<object> fields, <string> msg, ...)
* log.<level>() -> boolean is-trace-enabled
* log.<level>(<Error> err, [<string> msg, ...])
* log.<level>(<string> msg, ...)
* log.<level>(<object> fields, <string> msg, ...)
*
* where <level> is the lowercase version of the log level. Eg:
*
* log.info()
*
* @params fields {Object} Optional set of additional fields to log.
* @params msg {String} Log message. This can be followed by additional
* arguments that are handled like
* [util.format](http://nodejs.org/docs/latest/api/all.html#util.format).
*/
Logger.prototype.trace = function () {
var fields = null, msgArgs = null;
if (arguments.length === 0) { // `log.trace()`
return (this._level <= TRACE);
} else if (this._level > TRACE) {
return;
} else if (arguments[0] instanceof Error) {
// `log.trace(err, ...)`
fields = {err: errSerializer(arguments[0])};
if (arguments.length === 1) {
msgArgs = [fields.err.message];
} else {
msgArgs = Array.prototype.slice.call(arguments, 1);
}
} else if (typeof (arguments[0]) === 'string') { // `log.trace(msg, ...)`
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
} else if (Buffer.isBuffer(arguments[0])) { // `log.trace(buf, ...)`
// Almost certainly an error, show `inspect(buf)`. See bunyan issue #35.
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
msgArgs[0] = util.inspect(msgArgs[0]);
} else { // `log.trace(fields, msg, ...)`
fields = arguments[0];
msgArgs = Array.prototype.slice.call(arguments, 1);
}
var rec = this._mkRecord(fields, TRACE, msgArgs);
this._emit(rec);
}
/**
* Log a record at DEBUG level.
*
* Usages:
* log.debug() -> boolean is-debug-enabled
* log.debug(<Error> err, [<string> msg, ...])
* log.debug(<string> msg, ...)
* log.debug(<object> fields, <string> msg, ...)
*
* @params fields {Object} Optional set of additional fields to log.
* @params msg {String} Log message. This can be followed by additional
* arguments that are handled like
* [util.format](http://nodejs.org/docs/latest/api/all.html#util.format).
*/
Logger.prototype.debug = function () {
var fields = null, msgArgs = null;
if (arguments.length === 0) { // `log.debug()`
return (this._level <= DEBUG);
} else if (this._level > DEBUG) {
return;
} else if (arguments[0] instanceof Error) {
// `log.debug(err, ...)`
fields = {err: errSerializer(arguments[0])};
if (arguments.length === 1) {
msgArgs = [fields.err.message];
} else {
msgArgs = Array.prototype.slice.call(arguments, 1);
}
} else if (typeof (arguments[0]) === 'string') { // `log.debug(msg, ...)`
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
} else if (Buffer.isBuffer(arguments[0])) { // `log.debug(buf, ...)`
// Almost certainly an error, show `inspect(buf)`. See bunyan issue #35.
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
msgArgs[0] = util.inspect(msgArgs[0]);
} else { // `log.debug(fields, msg, ...)`
fields = arguments[0];
msgArgs = Array.prototype.slice.call(arguments, 1);
}
var rec = this._mkRecord(fields, DEBUG, msgArgs);
this._emit(rec);
}
/**
* Log a record at INFO level.
*
* Usages:
* log.info() -> boolean is-info-enabled
* log.info(<Error> err, [<string> msg, ...])
* log.info(<string> msg, ...)
* log.info(<object> fields, <string> msg, ...)
*
* @params fields {Object} Optional set of additional fields to log.
* @params msg {String} Log message. This can be followed by additional
* arguments that are handled like
* [util.format](http://nodejs.org/docs/latest/api/all.html#util.format).
*/
Logger.prototype.info = function () {
var fields = null, msgArgs = null;
if (arguments.length === 0) { // `log.info()`
return (this._level <= INFO);
} else if (this._level > INFO) {
return;
} else if (arguments[0] instanceof Error) {
// `log.info(err, ...)`
fields = {err: errSerializer(arguments[0])};
if (arguments.length === 1) {
msgArgs = [fields.err.message];
} else {
msgArgs = Array.prototype.slice.call(arguments, 1);
}
} else if (typeof (arguments[0]) === 'string') { // `log.info(msg, ...)`
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
} else if (Buffer.isBuffer(arguments[0])) { // `log.info(buf, ...)`
// Almost certainly an error, show `inspect(buf)`. See bunyan issue #35.
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
msgArgs[0] = util.inspect(msgArgs[0]);
} else { // `log.info(fields, msg, ...)`
fields = arguments[0];
msgArgs = Array.prototype.slice.call(arguments, 1);
}
var rec = this._mkRecord(fields, INFO, msgArgs);
this._emit(rec);
}
/**
* Log a record at WARN level.
*
* Usages:
* log.warn() -> boolean is-warn-enabled
* log.warn(<Error> err, [<string> msg, ...])
* log.warn(<string> msg, ...)
* log.warn(<object> fields, <string> msg, ...)
*
* @params fields {Object} Optional set of additional fields to log.
* @params msg {String} Log message. This can be followed by additional
* arguments that are handled like
* [util.format](http://nodejs.org/docs/latest/api/all.html#util.format).
*/
Logger.prototype.warn = function () {
var fields = null, msgArgs = null;
if (arguments.length === 0) { // `log.warn()`
return (this._level <= WARN);
} else if (this._level > WARN) {
return;
} else if (arguments[0] instanceof Error) {
// `log.warn(err, ...)`
fields = {err: errSerializer(arguments[0])};
if (arguments.length === 1) {
msgArgs = [fields.err.message];
} else {
msgArgs = Array.prototype.slice.call(arguments, 1);
}
} else if (typeof (arguments[0]) === 'string') { // `log.warn(msg, ...)`
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
} else if (Buffer.isBuffer(arguments[0])) { // `log.warn(buf, ...)`
// Almost certainly an error, show `inspect(buf)`. See bunyan issue #35.
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
msgArgs[0] = util.inspect(msgArgs[0]);
} else { // `log.warn(fields, msg, ...)`
fields = arguments[0];
msgArgs = Array.prototype.slice.call(arguments, 1);
}
var rec = this._mkRecord(fields, WARN, msgArgs);
this._emit(rec);
}
/**
* Log a record at ERROR level.
*
* Usages:
* log.error() -> boolean is-error-enabled
* log.error(<Error> err, [<string> msg, ...])
* log.error(<string> msg, ...)
* log.error(<object> fields, <string> msg, ...)
*
* @params fields {Object} Optional set of additional fields to log.
* @params msg {String} Log message. This can be followed by additional
* arguments that are handled like
* [util.format](http://nodejs.org/docs/latest/api/all.html#util.format).
*/
Logger.prototype.error = function () {
var fields = null, msgArgs = null;
if (arguments.length === 0) { // `log.error()`
return (this._level <= ERROR);
} else if (this._level > ERROR) {
return;
} else if (arguments[0] instanceof Error) {
// `log.error(err, ...)`
fields = {err: errSerializer(arguments[0])};
if (arguments.length === 1) {
msgArgs = [fields.err.message];
} else {
msgArgs = Array.prototype.slice.call(arguments, 1);
}
} else if (typeof (arguments[0]) === 'string') { // `log.error(msg, ...)`
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
} else if (Buffer.isBuffer(arguments[0])) { // `log.error(buf, ...)`
// Almost certainly an error, show `inspect(buf)`. See bunyan issue #35.
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
msgArgs[0] = util.inspect(msgArgs[0]);
} else { // `log.error(fields, msg, ...)`
fields = arguments[0];
msgArgs = Array.prototype.slice.call(arguments, 1);
}
var rec = this._mkRecord(fields, ERROR, msgArgs);
this._emit(rec);
}
/**
* Log a record at FATAL level.
*
* Usages:
* log.fatal() -> boolean is-fatal-enabled
* log.fatal(<Error> err, [<string> msg, ...])
* log.fatal(<string> msg, ...)
* log.fatal(<object> fields, <string> msg, ...)
*
* @params fields {Object} Optional set of additional fields to log.
* @params msg {String} Log message. This can be followed by additional
* arguments that are handled like
* [util.format](http://nodejs.org/docs/latest/api/all.html#util.format).
*/
Logger.prototype.fatal = function () {
var fields = null, msgArgs = null;
if (arguments.length === 0) { // `log.fatal()`
return (this._level <= FATAL);
} else if (this._level > FATAL) {
return;
} else if (arguments[0] instanceof Error) {
// `log.fatal(err, ...)`
fields = {err: errSerializer(arguments[0])};
if (arguments.length === 1) {
msgArgs = [fields.err.message];
} else {
msgArgs = Array.prototype.slice.call(arguments, 1);
}
} else if (typeof (arguments[0]) === 'string') { // `log.fatal(msg, ...)`
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
} else if (Buffer.isBuffer(arguments[0])) { // `log.fatal(buf, ...)`
// Almost certainly an error, show `inspect(buf)`. See bunyan issue #35.
fields = null;
msgArgs = Array.prototype.slice.call(arguments);
msgArgs[0] = util.inspect(msgArgs[0]);
} else { // `log.fatal(fields, msg, ...)`
fields = arguments[0];
msgArgs = Array.prototype.slice.call(arguments, 1);
}
var rec = this._mkRecord(fields, FATAL, msgArgs);
this._emit(rec);
}
Logger.prototype.trace = logEmitter(TRACE);
Logger.prototype.debug = logEmitter(DEBUG);
Logger.prototype.info = logEmitter(INFO);
Logger.prototype.warn = logEmitter(WARN);
Logger.prototype.error = logEmitter(ERROR);
Logger.prototype.fatal = logEmitter(FATAL);
//---- Standard serializers