Skip to content

Commit

Permalink
[issue #12] Add bunyan.createLogger(OPTIONS) form, as is more typic…
Browse files Browse the repository at this point in the history
…al in node.js APIs.

This'll eventually become the preferred form.
  • Loading branch information
trentm committed Apr 27, 2012
1 parent 6e0d1ec commit c7d5f8b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
5 changes: 3 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion bin/bunyan
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// See <https://github.com/trentm/node-bunyan>.
//

var VERSION = "0.6.10";
var VERSION = "0.7.0";

var util = require('util');
var pathlib = require('path');
Expand Down
6 changes: 5 additions & 1 deletion lib/bunyan.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <trentm@gmail.com> (http://trentm.com)",
"main": "./lib/bunyan.js",
Expand Down
44 changes: 43 additions & 1 deletion test/ctor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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'});

Expand Down

0 comments on commit c7d5f8b

Please sign in to comment.