'log.info(err)' support

master
Trent Mick 2012-02-06 09:10:11 -08:00
parent 141253c753
commit 543ef15b84
6 changed files with 79 additions and 13 deletions

View File

@ -1,8 +1,19 @@
# bunyan Changelog # bunyan Changelog
## bunyan 0.4.1 (not yet released) ## bunyan 0.5.0 (not yet released)
- Add `log.info(err)` special case for logging an `Error` instance. For
example `log.info(new TypeError("boom")` will produce:
...
"err": {
"message": "boom",
"name": "TypeError",
"stack": "TypeError: boom\n at Object.<anonymous> ..."
},
"msg": "boom",
...
(nothing yet)
## bunyan 0.4.0 ## bunyan 0.4.0

View File

@ -34,10 +34,13 @@ format version) are added for you.
The full `log.{trace|debug|...|fatal}(...)` API is: The full `log.{trace|debug|...|fatal}(...)` API is:
log.info(); // returns a boolean: is the "info" level enabled? log.info(); // Returns a boolean: is the "info" level enabled?
log.info("hi"); // log a simple string message log.info(err); // Log an `Error` instance, adds "err" key with exception details
log.info("hi %s", bob, anotherVar); // uses `util.format` for msg formatting // (including the stack) and sets "msg" to the exception message.
log.info({foo: "bar"}, "hi"); // adds "foo" field to log record // A special case, b/c logging errors should be easy.
log.info("hi"); // Log a simple string message.
log.info("hi %s", bob, anotherVar); // Uses `util.format` for msg formatting.
log.info({foo: "bar"}, "hi"); // Adds "foo" field to log record.
## bunyan tool ## bunyan tool
@ -293,9 +296,21 @@ follow (feedback from actual users welcome).
Recommended/Best Practice Fields: Recommended/Best Practice Fields:
- `err`: Object. A caught JS exception. Log that thing with - `err`: Object. A caught JS exception. Log that thing with `log.info(err)`
`log.error({err: err}, "oops")` and **use the `Logger.stdSerializers.err`** to get:
serializer for it. See "examples/err.js".
...
"err": {
"message": "boom",
"name": "TypeError",
"stack": "TypeError: boom\n at Object.<anonymous> ..."
},
"msg": "boom",
...
Or use the `Logger.stdSerializers.err` serializer in your Logger and
do this `log.error({err: err}, "oops")`. See "examples/err.js".
- `req_id`: String. A request identifier. Including this field in all logging - `req_id`: String. A request identifier. Including this field in all logging
tied to handling a particular request to your server is strongly suggested. tied to handling a particular request to your server is strongly suggested.
This allows post analysis of logs to easily collate all related logging This allows post analysis of logs to easily collate all related logging
@ -303,6 +318,7 @@ Recommended/Best Practice Fields:
and you carry a single request ID from the top API down through all APIs and you carry a single request ID from the top API down through all APIs
(as [node-restify](https://github.com/mcavage/node-restify) facilitates (as [node-restify](https://github.com/mcavage/node-restify) facilitates
with its 'X-Request-Id' header). with its 'X-Request-Id' header).
- `req`: An HTTP server request. Bunyan provides `Logger.stdSerializers.req` - `req`: An HTTP server request. Bunyan provides `Logger.stdSerializers.req`
to serialize a request with a suggested set of keys. Example: to serialize a request with a suggested set of keys. Example:

View File

@ -5,7 +5,7 @@
// See <https://github.com/trentm/node-bunyan>. // See <https://github.com/trentm/node-bunyan>.
// //
var VERSION = "0.4.1"; var VERSION = "0.5.0";
var util = require('util'); var util = require('util');
var pathlib = require('path'); var pathlib = require('path');

View File

@ -17,6 +17,15 @@ try {
log.warn({err: err}, "operation went boom: %s", err) // <--- here log.warn({err: err}, "operation went boom: %s", err) // <--- here
} }
log.info(new TypeError("how about this?")) // <--- alternatively this
try {
throw "boom string";
} catch (err) {
log.error(err)
}
/* /*
$ node err.js | ../bin/bunyan -j $ node err.js | ../bin/bunyan -j

View File

@ -2,7 +2,7 @@
* Copyright 2012 (c) Trent Mick. All rights reserved. * Copyright 2012 (c) Trent Mick. All rights reserved.
*/ */
var VERSION = "0.4.1"; var VERSION = "0.5.0";
// Bunyan log format version. This becomes the 'v' field on all log records. // Bunyan log format version. This becomes the 'v' field on all log records.
// `0` is until I release a version "1.0.0" of node-bunyan. Thereafter, // `0` is until I release a version "1.0.0" of node-bunyan. Thereafter,
@ -501,6 +501,7 @@ Logger.prototype._emit = function (rec) {
* *
* Usages: * Usages:
* log.trace() -> boolean is-trace-enabled * log.trace() -> boolean is-trace-enabled
* log.trace(<Error> err)
* log.trace(<string> msg, ...) * log.trace(<string> msg, ...)
* log.trace(<object> fields, <string> msg, ...) * log.trace(<object> fields, <string> msg, ...)
* *
@ -515,6 +516,10 @@ Logger.prototype.trace = function () {
return (this.level <= TRACE); return (this.level <= TRACE);
} else if (this.level > TRACE) { } else if (this.level > TRACE) {
return; return;
} else if (arguments.length === 1 && arguments[0] instanceof Error) {
// `log.trace(err)`
fields = {err: errSerializer(arguments[0])};
msgArgs = [fields.err.message];
} else if (typeof arguments[0] === 'string') { // `log.trace(msg, ...)` } else if (typeof arguments[0] === 'string') { // `log.trace(msg, ...)`
fields = null; fields = null;
msgArgs = Array.prototype.slice.call(arguments); msgArgs = Array.prototype.slice.call(arguments);
@ -531,6 +536,7 @@ Logger.prototype.trace = function () {
* *
* Usages: * Usages:
* log.debug() -> boolean is-debug-enabled * log.debug() -> boolean is-debug-enabled
* log.debug(<Error> err)
* log.debug(<string> msg, ...) * log.debug(<string> msg, ...)
* log.debug(<object> fields, <string> msg, ...) * log.debug(<object> fields, <string> msg, ...)
* *
@ -545,6 +551,10 @@ Logger.prototype.debug = function () {
return (this.level <= DEBUG); return (this.level <= DEBUG);
} else if (this.level > DEBUG) { } else if (this.level > DEBUG) {
return; return;
} else if (arguments.length === 1 && arguments[0] instanceof Error) {
// `log.debug(err)`
fields = {err: errSerializer(arguments[0])};
msgArgs = [fields.err.message];
} else if (typeof arguments[0] === 'string') { // `log.debug(msg, ...)` } else if (typeof arguments[0] === 'string') { // `log.debug(msg, ...)`
fields = null; fields = null;
msgArgs = Array.prototype.slice.call(arguments); msgArgs = Array.prototype.slice.call(arguments);
@ -561,6 +571,7 @@ Logger.prototype.debug = function () {
* *
* Usages: * Usages:
* log.info() -> boolean is-info-enabled * log.info() -> boolean is-info-enabled
* log.info(<Error> err)
* log.info(<string> msg, ...) * log.info(<string> msg, ...)
* log.info(<object> fields, <string> msg, ...) * log.info(<object> fields, <string> msg, ...)
* *
@ -575,6 +586,10 @@ Logger.prototype.info = function () {
return (this.level <= INFO); return (this.level <= INFO);
} else if (this.level > INFO) { } else if (this.level > INFO) {
return; return;
} else if (arguments.length === 1 && arguments[0] instanceof Error) {
// `log.info(err)`
fields = {err: errSerializer(arguments[0])};
msgArgs = [fields.err.message];
} else if (typeof arguments[0] === 'string') { // `log.info(msg, ...)` } else if (typeof arguments[0] === 'string') { // `log.info(msg, ...)`
fields = null; fields = null;
msgArgs = Array.prototype.slice.call(arguments); msgArgs = Array.prototype.slice.call(arguments);
@ -591,6 +606,7 @@ Logger.prototype.info = function () {
* *
* Usages: * Usages:
* log.warn() -> boolean is-warn-enabled * log.warn() -> boolean is-warn-enabled
* log.warn(<Error> err)
* log.warn(<string> msg, ...) * log.warn(<string> msg, ...)
* log.warn(<object> fields, <string> msg, ...) * log.warn(<object> fields, <string> msg, ...)
* *
@ -605,6 +621,10 @@ Logger.prototype.warn = function () {
return (this.level <= WARN); return (this.level <= WARN);
} else if (this.level > WARN) { } else if (this.level > WARN) {
return; return;
} else if (arguments.length === 1 && arguments[0] instanceof Error) {
// `log.warn(err)`
fields = {err: errSerializer(arguments[0])};
msgArgs = [fields.err.message];
} else if (typeof arguments[0] === 'string') { // `log.warn(msg, ...)` } else if (typeof arguments[0] === 'string') { // `log.warn(msg, ...)`
fields = null; fields = null;
msgArgs = Array.prototype.slice.call(arguments); msgArgs = Array.prototype.slice.call(arguments);
@ -621,6 +641,7 @@ Logger.prototype.warn = function () {
* *
* Usages: * Usages:
* log.error() -> boolean is-error-enabled * log.error() -> boolean is-error-enabled
* log.error(<Error> err)
* log.error(<string> msg, ...) * log.error(<string> msg, ...)
* log.error(<object> fields, <string> msg, ...) * log.error(<object> fields, <string> msg, ...)
* *
@ -635,6 +656,10 @@ Logger.prototype.error = function () {
return (this.level <= ERROR); return (this.level <= ERROR);
} else if (this.level > ERROR) { } else if (this.level > ERROR) {
return; return;
} else if (arguments.length === 1 && arguments[0] instanceof Error) {
// `log.error(err)`
fields = {err: errSerializer(arguments[0])};
msgArgs = [fields.err.message];
} else if (typeof arguments[0] === 'string') { // `log.error(msg, ...)` } else if (typeof arguments[0] === 'string') { // `log.error(msg, ...)`
fields = null; fields = null;
msgArgs = Array.prototype.slice.call(arguments); msgArgs = Array.prototype.slice.call(arguments);
@ -651,6 +676,7 @@ Logger.prototype.error = function () {
* *
* Usages: * Usages:
* log.fatal() -> boolean is-fatal-enabled * log.fatal() -> boolean is-fatal-enabled
* log.fatal(<Error> err)
* log.fatal(<string> msg, ...) * log.fatal(<string> msg, ...)
* log.fatal(<object> fields, <string> msg, ...) * log.fatal(<object> fields, <string> msg, ...)
* *
@ -665,6 +691,10 @@ Logger.prototype.fatal = function () {
return (this.level <= FATAL); return (this.level <= FATAL);
} else if (this.level > FATAL) { } else if (this.level > FATAL) {
return; return;
} else if (arguments.length === 1 && arguments[0] instanceof Error) {
// `log.fatal(err)`
fields = {err: errSerializer(arguments[0])};
msgArgs = [fields.err.message];
} else if (typeof arguments[0] === 'string') { // `log.fatal(msg, ...)` } else if (typeof arguments[0] === 'string') { // `log.fatal(msg, ...)`
fields = null; fields = null;
msgArgs = Array.prototype.slice.call(arguments); msgArgs = Array.prototype.slice.call(arguments);
@ -711,7 +741,7 @@ Logger.stdSerializers.res = function res(res) {
// Serialize an Error object // Serialize an Error object
// (Core error properties are enumerable in node 0.4, not in 0.6). // (Core error properties are enumerable in node 0.4, not in 0.6).
Logger.stdSerializers.err = function err(err) { var errSerializer = Logger.stdSerializers.err = function err(err) {
var obj = { var obj = {
message: err.message, message: err.message,
name: err.name, name: err.name,

View File

@ -1,6 +1,6 @@
{ {
"name": "bunyan", "name": "bunyan",
"version": "0.4.1", "version": "0.5.0",
"description": "a JSON Logger library for node.js servers", "description": "a JSON Logger library for node.js servers",
"main": "./lib/bunyan.js", "main": "./lib/bunyan.js",
"bin": { "bin": {