diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..0cfb49a --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# bookrc + +automatically load book logging configuration from your environment or valid bookrc.js files. + +bookrc allows you to configure your book logging in one place for your project. It follows a simple lookup pattern and returns either your `bookrc.js` file or the default `book` module. + +## use + +Lets say we have application code which wants to do some logging and we want the logging to be sent to various locations and processed through the book middleware stack. + +In the root of our app, we would create a `bookrc.js` file which can setup additional middleware and logging features. + +```javascript +var log = require('book').default(); +var raven = require('book-raven'); + +// we want to add a .commit field to our log entry with the git commit hash +log.use(git(__dirname)); + +// we want to log errors to raven +log.use(raven(process.env.SENTRY_DSN)); + +// now we just expose the logging object +module.exports = log; +``` + +Now we just `require('bookrc')` and use it like we used `book` before. Except now our logs will run through our middleware stack and be tagged with our commit id and send to sentry for alerting. + +Our app code does not have to change much at all. Simply update `require('book')` to `require('bookrc')` and you are set. + +```javascript +// instead of require('book'), use require('bookrc'); +var log = require('bookrc'); + +// now you can just log as before, no other changes +log.info('foo'); +log.error('bar'); +``` + +## install + +Bookrc does not depend on book and vice versa. You must install both to have logging functionality. Bookrc is simply a dynamic loader for the bookrc.js file in your project. + +```shell +npm install bookrc +npm install book +``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..5c03eeb --- /dev/null +++ b/index.js @@ -0,0 +1,68 @@ +// builtin +var path = require('path'); +var fs = require('fs'); + +// return a list of possible paths for bookrc.js files from `start` +function bookrc_paths(start) { + var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/; + var parts = start.split(splitRe); + + var dirs = []; + for (var i = parts.length - 1; i >= 0; i--) { + if (parts[i] === 'bookrc.js') { + continue; + } + var dir = path.join( + path.join.apply(path, parts.slice(0, i + 1)), + 'bookrc.js' + ); + if (!parts[0].match(/([A-Za-z]:)/)) { + dir = '/' + dir; + } + dirs.push(dir); + } + return dirs; +} + +function use_bookrc() { + var bookrc = process.env.BOOK_RC; + return !(bookrc === 'false' || bookrc === '0') +} + +// if bookrc loading is disabled +// just return regular book module +if (!use_bookrc()) { + module.exports = require('book'); + return; +} + +var bookrc_path = process.env.BOOK_RC; + +// just load the bookrc path user specified +if (bookrc_path) { + module.exports = require(bookrc_path); + return; +} + +// try to find a suitable bookrc +var parent = module.parent; +while (parent.parent) { + parent = parent.parent; +} +var start_path = parent.paths.shift(); +var paths = bookrc_paths(start_path); + +for (var i=0 ; i", + "license": "MIT", + "readmeFilename": "README.md" +}