Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
defunctzombie committed Jan 24, 2013
0 parents commit a552922
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
47 changes: 47 additions & 0 deletions 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
```
68 changes: 68 additions & 0 deletions 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<paths.length ; ++i) {
var pth = paths[i];
if (!fs.existsSync(pth)) {
continue;
}

// load and return the bookrc
module.exports = require(pth);
return;
}

// if no bookrc, then just return book
module.exports = require('book');

21 changes: 21 additions & 0 deletions package.json
@@ -0,0 +1,21 @@
{
"name": "bookrc",
"version": "0.0.0",
"description": "automatic config loading for the book logging framework",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git://github.com/shtylman/node-bookrc.git"
},
"keywords": [
"logging",
"book",
"log"
],
"author": "Roman Shtylman <shtylman@gmail.com>",
"license": "MIT",
"readmeFilename": "README.md"
}

0 comments on commit a552922

Please sign in to comment.