diff --git a/CHANGES.md b/CHANGES.md index a850752..24fb0c9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,9 +6,10 @@ Known issues: bug](https://github.com/TooTallNate/node-gyp/issues/65). -## bunyan 1.0.2 (not yet released) +## bunyan 1.1.0 (not yet released) -(nothing yet) +- [issue #162] Preliminary support for [browserify](http://browserify.org/). + See [the section in the README](../README.md#browserify). ## bunyan 1.0.1 diff --git a/README.md b/README.md index 5c0b8f8..9951206 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ facilities in node 0.12 to which bunyan can switch. - lightweight specialization of Logger instances with [`log.child`](#logchild) - custom rendering of logged objects with ["serializers"](#serializers) - [Runtime log snooping via Dtrace support](#runtime-log-snooping-via-dtrace) +- Support for [browserify](http://browserify.org/). See [Browserify + section](#browserify) below. # Introduction @@ -939,6 +941,67 @@ Output of the above might be: node`_start+0x83 +# Browserify + +XXX explain + +As the [Browserify](http://browserify.org/) site says it "lets you +`require('modules')` in the browser by bundling up all of your dependencies." +It is a build tool to run on your node.js script to bundle up your script and +all its node.js dependencies into a single file that is runnable in the +browser via: + + + +As of version 1.1.0, node-bunyan supports being run via Browserify. The +default [stream](#streams) when running in the browser is one that emits +raw log records to `console.log/info/warn/error`. + +Here is a quick example showing you how you can get this working for your +script. + +1. Get browserify and bunyan installed in your module: + + + $ npm install browserify bunyan + +2. An example script using Bunyan, "foo.js": + + var bunyan = require('bunyan'); + var log = bunyan.createLogger({name: 'play', level: 'debug'}); + log.trace('this one does not emit'); + log.debug('hi on debug'); // console.log + log.info('hi on info'); // console.info + log.warn('hi on warn'); // console.warn + log.error('hi on error'); // console.error + +3. Build this into a bundle to run in the browser, "foo.browser.js": + + $ ./node_modules/.bin/browserify foo.js -o foo.browser.js + +4. Put that into an HTML file, "foo.html": + + + + + + + + +
hi
+ + + +5. Open that in your browser and open your browser console: + + $ open foo.html + + +Here is what it looks like in Firefox's console: ![Bunyan + Browserify in the +Firefox console](./docs/img/bunyan.browserify.png) + + + # Versioning The scheme I follow is most succintly described by the bootstrap guys diff --git a/bin/bunyan b/bin/bunyan index 2bbc72e..5a2b975 100755 --- a/bin/bunyan +++ b/bin/bunyan @@ -11,7 +11,7 @@ * vim: expandtab:ts=4:sw=4 */ -var VERSION = '1.0.2'; +var VERSION = '1.1.0'; var p = console.log; var util = require('util'); diff --git a/docs/img/bunyan.browserify.png b/docs/img/bunyan.browserify.png new file mode 100644 index 0000000..411aa1f Binary files /dev/null and b/docs/img/bunyan.browserify.png differ diff --git a/lib/bunyan.js b/lib/bunyan.js index 31a67fa..326c1b4 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -8,7 +8,7 @@ * vim: expandtab:ts=4:sw=4 */ -var VERSION = '1.0.2'; +var VERSION = '1.1.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, @@ -31,7 +31,8 @@ var fs = require('fs'); var util = require('util'); var assert = require('assert'); try { - var dtrace = require('dtrace-provider'); + /* Use `+ ''` to hide this import from browserify. */ + var dtrace = require('dtrace-provider' + ''); } catch (e) { dtrace = null; } @@ -39,11 +40,21 @@ var EventEmitter = require('events').EventEmitter; // The 'mv' module is required for rotating-file stream support. try { - var mv = require('mv'); + /* Use `+ ''` to hide this import from browserify. */ + var mv = require('mv' + ''); } catch (e) { mv = null; } +// Are we in the browser (e.g. running via browserify)? +var browser; +try { + window + browser = true; +} catch (e) { + browser = false; +} + //---- Internal support stuff @@ -154,6 +165,19 @@ function _warn(msg, file, line) { var _warned = {}; +function ConsoleRawStream() {} +ConsoleRawStream.prototype.write = function (rec) { + if (rec.level < INFO) { + console.log(rec); + } else if (rec.level < WARN) { + console.info(rec); + } else if (rec.level < ERROR) { + console.warn(rec); + } else { + console.error(rec); + } +}; + //---- Levels @@ -356,12 +380,28 @@ function Logger(options, _childOptions, _childSimple) { } else if (parent && options.level) { this.level(options.level); } else if (!parent) { - self.addStream({ - type: 'stream', - stream: process.stdout, - closeOnExit: false, - level: (options.level ? resolveLevel(options.level) : INFO) - }); + if (browser) { + /* + * In the browser we'll be emitting to console.log by default. + * Any console.log worth its salt these days can nicely render + * and introspect objects (e.g. the Firefox and Chrome console) + * so let's emit the raw log record. Are there browsers for which + * that breaks things? + */ + self.addStream({ + type: 'raw', + stream: new ConsoleRawStream(), + closeOnExit: false, + level: (options.level ? resolveLevel(options.level) : INFO) + }); + } else { + self.addStream({ + type: 'stream', + stream: process.stdout, + closeOnExit: false, + level: (options.level ? resolveLevel(options.level) : INFO) + }); + } } if (options.serializers) { self.addSerializers(options.serializers); diff --git a/package.json b/package.json index 9976670..f24a63d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bunyan", - "version": "1.0.2", + "version": "1.1.0", "description": "a JSON logging library for node.js services", "author": "Trent Mick (http://trentm.com)", "main": "./lib/bunyan.js",