diff --git a/Makefile b/Makefile index e87b553..1c07985 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index f1ea213..0fd5f20 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/index.js b/index.js index 2beb71c..81d332c 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,10 @@ -var bunyan; +var bunyan, pino; + +try { + pino = require('pino'); +} catch (err) { + pino = null; +} try { bunyan = require('bunyan'); @@ -6,8 +12,10 @@ try { 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'); } \ No newline at end of file diff --git a/lib/bunyan.js b/lib/bunyan.js index caf71a2..69ca477 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -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 = { @@ -70,7 +70,7 @@ function setLogger (name) { } var title; - + if (process.env.METEOR_SETTINGS) { try { var meteorSettings = JSON.parse(process.env.METEOR_SETTINGS); @@ -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, '..', '..'); diff --git a/lib/pino.js b/lib/pino.js new file mode 100644 index 0000000..6d1294f --- /dev/null +++ b/lib/pino.js @@ -0,0 +1 @@ +module.exports = require('pino')(); \ No newline at end of file diff --git a/package.json b/package.json index 98214af..42b08ba 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -15,7 +17,11 @@ "dependencies": { "debug": "~0.8.1" }, - "peerDependencies": { - "bunyan": "*" + "optionalDependencies": { + "bunyan": "*", + "pino": "^4.7.1" + }, + "devDependencies": { + "mocha": "^3.5.3" } }