From b450b015b371a41a00a0cc26059faef6aa3a7c8e Mon Sep 17 00:00:00 2001 From: Josh Wilsdon Date: Tue, 23 Oct 2012 23:06:44 -0700 Subject: [PATCH] DRY out some code to make it clearer that these do the same thing. --- lib/bunyan.js | 304 +++++++++----------------------------------------- 1 file changed, 50 insertions(+), 254 deletions(-) diff --git a/lib/bunyan.js b/lib/bunyan.js index f2cfd81..dbc52ed 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -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.()` + return (this._level <= minLevel); + } else if (this._level > minLevel) { + return; + } else if (arguments[0] instanceof Error) { + // `log.(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.(msg, ...)` + fields = null; + msgArgs = Array.prototype.slice.call(arguments); + } else if (Buffer.isBuffer(arguments[0])) { // `log.(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.(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( err, [ msg, ...]) - * log.trace( msg, ...) - * log.trace( fields, msg, ...) + * log.() -> boolean is-trace-enabled + * log.( err, [ msg, ...]) + * log.( msg, ...) + * log.( fields, msg, ...) + * + * where 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( err, [ msg, ...]) - * log.debug( msg, ...) - * log.debug( fields, 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( err, [ msg, ...]) - * log.info( msg, ...) - * log.info( fields, 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( err, [ msg, ...]) - * log.warn( msg, ...) - * log.warn( fields, 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( err, [ msg, ...]) - * log.error( msg, ...) - * log.error( fields, 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( err, [ msg, ...]) - * log.fatal( msg, ...) - * log.fatal( fields, 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