Skip to content

Commit

Permalink
allowing auto-detection for pino
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Walters committed Sep 15, 2017
1 parent 33747bc commit 007ca3d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ load-test:
time node ./test/load-test.js >> /dev/null

test:
echo "removing optional dependencies"
rm -rf ./node_modules/bunyan ./node_modules/pino
echo "testing debug mode"
./test/test
./test/test | ./node_modules/.bin/bunyan
echo "testing pino mode"
npm i pino node-jq 2> /dev/null && LOG_LEVEL=10 ./test/test | ./node_modules/node-jq/bin/jq
rm -rf ./node_modules/pino
echo "testing bunyan mode"
npm i bunyan 2> /dev/null && LOG_LEVEL=10 ./test/test | ./node_modules/.bin/bunyan

.PHONY: test
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ llog

llog is a simple logging module. llog is intended for use in applications, not libraries. It provides level-based debugging (trace|debug|info|warn|error|fatal). `note: For adding detailed debug statements in libraries, use TJ's `debug` module.`

As an added bonus, llog provides a simple upgrade path from plain text to json logging.
As an added bonus, llog provides a simple upgrade path from plain text to json logging via either pino or bunyan. `llog` will look for `pino` or `bunyan` at load time and automatically use them as a provider if present. This means you can write your apps with non-json logging when starting, and auto-magically all your logs to json logging with a simple `npm i --save pino`.

## level-based debugging

Expand Down Expand Up @@ -36,11 +36,12 @@ DEBUG=trace,info,warn,error node app.js

## Magic upgrade to json logging

llog will automatically detect if `bunyan` is installed as a peer dependency and, if so, automatically upgrade to json logging. Because `bunyan` uses `process.env.LOG_LEVEL` as its level indicator (as opposed to `debug`'s `DEBUG` variable, the steps for moving to json logs are:
llog will automatically detect if `bunyan` or `pino` are installed as a peer dependency and, if so, automatically upgrade to json logging. Because `bunyan` and `pino` use `process.env.LOG_LEVEL` as a level indicator (as opposed to `debug`'s `DEBUG` variable, the steps for moving to json logs are:

1. `npm install --save buynan`
1. `npm install --save buynan` or `npm install --save pino`
2. execute your application using `LOG_LEVEL=10 node app` instead of using `DEBUG`. Higher levels are always included when specifying a level.

Bunyan log levels can be found at https://github.com/trentm/node-bunyan#levels.
Pino log levels can be found at https://github.com/pinojs/pino

The following two steps will instantly cause all uses of llog to log json using `bunyan` instead of plain text via `debug`. This is particularly useful when moving from early stages of application development to having unified logging via Logstash or Splunk.
This is particularly useful when moving from early stages of application development to having unified logging via Logstash or Splunk.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
var bunyan;
var bunyan, pino;

try {
pino = require('pino');
} catch (err) {
pino = null;
}

try {
bunyan = require('bunyan');
} catch (err) {
bunyan = null;
}

if (bunyan) {
module.exports = require('./lib/bunyan');
if (pino) {
module.exports = require('./lib/pino');
} else if (bunyan) {
module.exports = require('./lib/bunyan');
} else {
module.exports = require('./lib/debug');
}
12 changes: 6 additions & 6 deletions lib/bunyan.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ var path = require('path');
findPkgJson = function (dir) {

var files = fs.readdirSync(dir);

if (~files.indexOf('package.json')) {
return path.join(dir, 'package.json');
}

if (dir === '/') {
throw new Error('Could not find package.json up from: ' + dir);
}
else if (!dir || dir === '.') {
throw new Error('Cannot find package.json from unspecified directory');
}

return findPkgJson(path.dirname(dir));
};

function setLogger (name) {
var logger = bunyan.createLogger({ name: name });

logger.level(0);

var levels = {
Expand Down Expand Up @@ -70,7 +70,7 @@ function setLogger (name) {
}

var title;

if (process.env.METEOR_SETTINGS) {
try {
var meteorSettings = JSON.parse(process.env.METEOR_SETTINGS);
Expand All @@ -91,7 +91,7 @@ try {
if (isNaN(LOG_LEVEL)) LOG_LEVEL = 0;
if (LOG_LEVEL === undefined || LOG_LEVEL === null) LOG_LEVEL = 0;
} catch (e) {
LOG_LEVEL = 0;
LOG_LEVEL = 0;
}

var baseDir = path.join(__dirname, '..', '..');
Expand Down
1 change: 1 addition & 0 deletions lib/pino.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('pino')();
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "llog",
"version": "0.0.12",
"version": "0.0.13",
"description": "simple multi-level application logging module",
"main": "index.js",
"scripts": {
"perf-test": "time node ./test/load-test.js >> /dev/null",
"pre-test": "npm install bunyan pino",
"test": "make test"
},
"repository": {
Expand All @@ -15,7 +17,11 @@
"dependencies": {
"debug": "~0.8.1"
},
"peerDependencies": {
"bunyan": "*"
"optionalDependencies": {
"bunyan": "*",
"pino": "^4.7.1"
},
"devDependencies": {
"mocha": "^3.5.3"
}
}

0 comments on commit 007ca3d

Please sign in to comment.