Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making pino-debug compatible with debug v3 #11

Merged
merged 11 commits into from
Jan 3, 2018
2 changes: 1 addition & 1 deletion benchmarks/basic.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var wrap = require('module').wrap
var bench = require('fastbench')
var pino = require('pino')
var fs = require('fs')
var dest = fs.createWriteStream('/dev/null')
var dest = process.platform === 'win32' ? fs.createWriteStream('\\\\.\\NUL') : fs.createWriteStream('/dev/null')
var plog = pino(dest)

process.env.DEBUG = 'dlog'
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/deep-object.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var wrap = require('module').wrap
var bench = require('fastbench')
var pino = require('pino')
var fs = require('fs')
var dest = fs.createWriteStream('/dev/null')
var dest = process.platform === 'win32' ? fs.createWriteStream('\\\\.\\NUL') : fs.createWriteStream('/dev/null')
var plog = pino(dest)

process.env.DEBUG = 'dlog'
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/object.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var wrap = require('module').wrap
var bench = require('fastbench')
var pino = require('pino')
var fs = require('fs')
var dest = fs.createWriteStream('/dev/null')
var dest = process.platform === 'win32' ? fs.createWriteStream('\\\\.\\NUL') : fs.createWriteStream('/dev/null')
var plog = pino(dest)

process.env.DEBUG = 'dlog'
Expand Down
8 changes: 4 additions & 4 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"scripts": {
"start": "node ./bin/www",
"start-preload": "DEBUG=* node -r ../ ./bin/www",
"start-programmatic": "./bin/www-programmatic",
"start-programmatic-debug": "LEVEL=debug ./bin/www-programmatic",
"start-programmatic-trace": "LEVEL=trace ./bin/www-programmatic"
"start-programmatic": "node ./bin/www-programmatic",
"start-programmatic-debug": "LEVEL=debug node ./bin/www-programmatic",
"start-programmatic-trace": "LEVEL=trace node ./bin/www-programmatic"
},
"dependencies": {
"body-parser": "~1.15.2",
"cookie-parser": "~1.4.3",
"debug": "^2.6.1",
"debug": "^3.1.0",
"express": "~4.14.0",
"jade": "~1.11.0",
"morgan": "~1.7.0"
Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function pinoDebug (logger, opts) {
opts = opts || {}
var auto = 'auto' in opts ? opts.auto : true
var map = opts.map || {}
var namespaces = (process.env.DEBUG || '').split(/[\s,]+/)
var namespaces = getNamespaces()
debug.map = Object.keys(map).sort(byPrecision).reduce(function (m, k) {
if (auto) namespaces.push(k)
m.set(RegExp('^' + k.replace(/[\\^$+?.()|[\]{}]/g, '\\$&').replace(/\*/g, '.*?') + '$'), map[k])
Expand All @@ -30,6 +30,14 @@ function pinoDebug (logger, opts) {
debug.enable(namespaces.join(','))
}

function getNamespaces () {
var namespaces = process.env.DEBUG
if (namespaces != null) {
return (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/)
}
return []
}

function byPrecision (a, b) {
var aix = a.indexOf('*')
var bix = b.indexOf('*')
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test-2.4": "npm i debug@2.4 && npm run test",
"test-2.5": "npm i debug@2.5 && npm run test",
"test-2.6": "npm i debug@2.6 && npm run test",
"test-3.1": "npm i debug@3.1 && npm run test",
"test:cov": "standard && npm run deps && NODE_ENV=test tap --cov test/*.js",
"ci": "npm test -- --coverage-report=lcov",
"test:cov:html": "standard && npm run deps && NODE_ENV=test tap --coverage-report=html test",
Expand All @@ -30,7 +31,7 @@
],
"license": "MIT",
"dependencies": {
"debug": "^2.6.9",
"debug": "^3.1.0",
"pino": "^4.0.2"
},
"devDependencies": {
Expand Down
14 changes: 14 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,17 @@ test('results in valid syntax when source has trailing comment', (t) => {
t.doesNotThrow(() => require('./fixtures/trailing-comment'))
t.end()
})

test('preserves DEBUG env independently from debug module', (t) => {
process.env.DEBUG = 'ns1'
var pinoDebug = require('../')
var stream = through((line, _, cb) => {
var obj = JSON.parse(line)
t.is(obj.msg, 'test')
t.is(obj.ns, 'ns1')
t.end()
})
pinoDebug(require('pino')({level: 'debug'}, stream))
var debug = require('debug')
debug('ns1')('test')
})