From 6d94985f2d8f60df6a80e9a69d40e68b10e223e6 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Mon, 30 Jan 2012 12:40:57 -0800 Subject: [PATCH] info's sibling APIs --- TODO.md | 4 +- hi.js | 2 +- lib/bunyan.js | 131 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 133 insertions(+), 4 deletions(-) diff --git a/TODO.md b/TODO.md index 6ebaf4f..e983a90 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,6 @@ -- info's siblings +- mark wants pretty output for debug output + - not sure if 'bunyan --pretty' or whatever would suffice +- mark suggested a list of streams. This is what ring could be. - `bunyan` cli - expand set of fields diff --git a/hi.js b/hi.js index ec5d726..93af3de 100644 --- a/hi.js +++ b/hi.js @@ -16,7 +16,7 @@ function Wuzzle(options) { } Wuzzle.prototype.woos = function () { - this.log.info("This wuzzle is woosey.") + this.log.warn("This wuzzle is woosey.") } var wuzzle = new Wuzzle({log: log.clone({component: "wuzzle"})}); diff --git a/lib/bunyan.js b/lib/bunyan.js index 8d933ff..0e4f87d 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -204,10 +204,47 @@ Logger.prototype._emit = function (rec) { /** + * Log a record at DEBUG level. + * + * Usages: + * log.debug() -> boolean is-debug-enabled + * 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 (typeof arguments[0] === 'string') { // `log.debug(msg, ...)` + fields = null; + msgArgs = Array.prototype.slice.call(arguments); + } 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( fields, msg, ...) - * log.info( msg, ...) * log.info() -> boolean is-info-enabled + * 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; @@ -226,6 +263,96 @@ Logger.prototype.info = function () { this._emit(rec); } +/** + * Log a record at WARN level. + * + * Usages: + * log.warn() -> boolean is-warn-enabled + * 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 (typeof arguments[0] === 'string') { // `log.warn(msg, ...)` + fields = null; + msgArgs = Array.prototype.slice.call(arguments); + } 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( 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 (typeof arguments[0] === 'string') { // `log.error(msg, ...)` + fields = null; + msgArgs = Array.prototype.slice.call(arguments); + } 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( 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 (typeof arguments[0] === 'string') { // `log.fatal(msg, ...)` + fields = null; + msgArgs = Array.prototype.slice.call(arguments); + } 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); +} + module.exports = Logger;