diff --git a/CHANGES.md b/CHANGES.md index f5eb7ea..6878f64 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,8 +1,9 @@ # bunyan Changelog -## bunyan 0.6.10 (not yet released) +## bunyan 0.7.0 (not yet released) -(nothing yet) +- [issue #12] Add `bunyan.createLogger(OPTIONS)` form, as is more typical in + node.js APIs. This'll eventually become the preferred form. ## bunyan 0.6.9 diff --git a/README.md b/README.md index ef3d121..24a32d2 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,12 @@ to log4j logger "name", but Bunyan doesn't do hierarchical logger names. var log = new Logger({name: "myapp"}); log.info("hi"); +Alternatively, bunyan 0.7.0 and up supports a more node.js-land typical +style (which might become the preferred form over time): + + var bunyan = require('bunyan'); + var log = bunyan.createLogger({name: "myapp"}); + **Log records are JSON.** "hostname", "time" and "v" (the Bunyan log format version) are added for you. diff --git a/bin/bunyan b/bin/bunyan index 3f465a9..358ac73 100755 --- a/bin/bunyan +++ b/bin/bunyan @@ -5,7 +5,7 @@ // See . // -var VERSION = "0.6.10"; +var VERSION = "0.7.0"; var util = require('util'); var pathlib = require('path'); diff --git a/lib/bunyan.js b/lib/bunyan.js index baa364a..4e7dafa 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -4,7 +4,7 @@ * The bunyan logging library for node.js. */ -var VERSION = "0.6.10"; +var VERSION = "0.7.0"; // 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, @@ -955,3 +955,7 @@ module.exports.FATAL = FATAL; module.exports.VERSION = VERSION; module.exports.LOG_VERSION = LOG_VERSION; + +module.exports.createLogger = function createLogger(options) { + return new Logger(options); +}; diff --git a/package.json b/package.json index 567b52e..5eb9cc7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bunyan", - "version": "0.6.10", + "version": "0.7.0", "description": "a JSON Logger library for node.js servers", "author": "Trent Mick (http://trentm.com)", "main": "./lib/bunyan.js", diff --git a/test/ctor.test.js b/test/ctor.test.js index 700158e..abbfc20 100644 --- a/test/ctor.test.js +++ b/test/ctor.test.js @@ -5,7 +5,9 @@ */ var test = require('tap').test; -var Logger = require('../lib/bunyan'); +var bunyan = require('../lib/bunyan'), + Logger = bunyan; + test('ensure Logger creation options', function (t) { @@ -48,6 +50,46 @@ test('ensure Logger creation options', function (t) { }); +test('ensure Logger creation options (createLogger)', function (t) { + t.throws(function () { bunyan.createLogger(); }, + {name: 'TypeError', message: 'options (object) is required'}, + 'no options should throw'); + + t.throws(function () { bunyan.createLogger({}); }, + {name: 'TypeError', message: 'options.name (string) is required'}, + 'no options.name should throw'); + + t.doesNotThrow(function () { bunyan.createLogger({name: 'foo'}); }, + 'just options.name should be sufficient'); + + var options = {name: 'foo', stream: process.stdout, streams: []}; + t.throws(function () { bunyan.createLogger(options); }, + 'cannot use "stream" and "streams"'); + + options = {name: 'foo', level: 'info', streams: []}; + t.throws(function () { bunyan.createLogger(options); }, + 'cannot use "level" and "streams"'); + + // https://github.com/trentm/node-bunyan/issues/3 + options = {name: 'foo', streams: {}}; + t.throws(function () { bunyan.createLogger(options); }, + {name: 'TypeError', message: 'invalid options.streams: must be an array'}, + '"streams" must be an array'); + + options = {name: 'foo', serializers: 'a string'}; + t.throws(function () { bunyan.createLogger(options); }, + {name: 'TypeError', message: 'invalid options.serializers: must be an object'}, + '"serializers" cannot be a string'); + + options = {name: 'foo', serializers: [1,2,3]}; + t.throws(function () { bunyan.createLogger(options); }, + {name: 'TypeError', message: 'invalid options.serializers: must be an object'}, + '"serializers" cannot be an array'); + + t.end(); +}); + + test('ensure Logger child() options', function (t) { var log = new Logger({name: 'foo'});