browserify support, bump ver to 1.1.0

This commit is contained in:
Trent Mick 2014-09-21 21:20:43 -07:00
parent cb703ef474
commit 414f33563d
6 changed files with 117 additions and 13 deletions

View file

@ -6,9 +6,10 @@ Known issues:
bug](https://github.com/TooTallNate/node-gyp/issues/65). 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 ## bunyan 1.0.1

View file

@ -62,6 +62,8 @@ facilities in node 0.12 to which bunyan can switch.
- lightweight specialization of Logger instances with [`log.child`](#logchild) - lightweight specialization of Logger instances with [`log.child`](#logchild)
- custom rendering of logged objects with ["serializers"](#serializers) - custom rendering of logged objects with ["serializers"](#serializers)
- [Runtime log snooping via Dtrace support](#runtime-log-snooping-via-dtrace) - [Runtime log snooping via Dtrace support](#runtime-log-snooping-via-dtrace)
- Support for [browserify](http://browserify.org/). See [Browserify
section](#browserify) below.
# Introduction # Introduction
@ -939,6 +941,67 @@ Output of the above might be:
node`_start+0x83 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:
<script src="foo.browser.js"></script>
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":
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="foo.browser.js"></script>
</head>
<body>
<div>hi</div>
</body>
</html>
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 # Versioning
The scheme I follow is most succintly described by the bootstrap guys The scheme I follow is most succintly described by the bootstrap guys

View file

@ -11,7 +11,7 @@
* vim: expandtab:ts=4:sw=4 * vim: expandtab:ts=4:sw=4
*/ */
var VERSION = '1.0.2'; var VERSION = '1.1.0';
var p = console.log; var p = console.log;
var util = require('util'); var util = require('util');

Binary file not shown.

After

Width:  |  Height:  |  Size: 194 KiB

View file

@ -8,7 +8,7 @@
* vim: expandtab:ts=4:sw=4 * 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. // 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,
@ -31,7 +31,8 @@ var fs = require('fs');
var util = require('util'); var util = require('util');
var assert = require('assert'); var assert = require('assert');
try { try {
var dtrace = require('dtrace-provider'); /* Use `+ ''` to hide this import from browserify. */
var dtrace = require('dtrace-provider' + '');
} catch (e) { } catch (e) {
dtrace = null; dtrace = null;
} }
@ -39,11 +40,21 @@ var EventEmitter = require('events').EventEmitter;
// The 'mv' module is required for rotating-file stream support. // The 'mv' module is required for rotating-file stream support.
try { try {
var mv = require('mv'); /* Use `+ ''` to hide this import from browserify. */
var mv = require('mv' + '');
} catch (e) { } catch (e) {
mv = null; 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 //---- Internal support stuff
@ -154,6 +165,19 @@ function _warn(msg, file, line) {
var _warned = {}; 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 //---- Levels
@ -356,6 +380,21 @@ function Logger(options, _childOptions, _childSimple) {
} else if (parent && options.level) { } else if (parent && options.level) {
this.level(options.level); this.level(options.level);
} else if (!parent) { } else if (!parent) {
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({ self.addStream({
type: 'stream', type: 'stream',
stream: process.stdout, stream: process.stdout,
@ -363,6 +402,7 @@ function Logger(options, _childOptions, _childSimple) {
level: (options.level ? resolveLevel(options.level) : INFO) level: (options.level ? resolveLevel(options.level) : INFO)
}); });
} }
}
if (options.serializers) { if (options.serializers) {
self.addSerializers(options.serializers); self.addSerializers(options.serializers);
} }

View file

@ -1,6 +1,6 @@
{ {
"name": "bunyan", "name": "bunyan",
"version": "1.0.2", "version": "1.1.0",
"description": "a JSON logging library for node.js services", "description": "a JSON logging library for node.js services",
"author": "Trent Mick <trentm@gmail.com> (http://trentm.com)", "author": "Trent Mick <trentm@gmail.com> (http://trentm.com)",
"main": "./lib/bunyan.js", "main": "./lib/bunyan.js",