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

Added enable option and support the level 'disable' #53

Merged
merged 5 commits into from
Jun 6, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,24 @@ var levels = {
debug: 20,
trace: 10
}

// private property
Object.defineProperty(levels, 'silent', {
value: 100,
enumerable: false
})

var nums = Object.keys(levels).reduce(function (o, k) {
o[levels[k]] = k
return o
}, {})

// private property
Object.defineProperty(nums, '100', {
value: 'silent',
enumerable: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enumerable: false is default, doesn't need to be set

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, I've left it there for increased readability.

})

function pino (opts, stream) {
if (opts && opts._writableState) {
stream = opts
Expand All @@ -44,6 +57,10 @@ function pino (opts, stream) {
buf: ''
}

if (opts.enable === false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

....maybe.. opts.silent ? so then if (opts.silent) { }

also - should there be enable and disable methods, so logging can be turned back on

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to prefer positive logic, I find this easier to reason about but harder to work with in code.

Basically enable: false is for convenience, and just an alias over level: 'silent'. It's useful because you can do things like `pino({ level: 'info', enable: !!process.env.NO_LOG }),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok sure but.. it should be enabled rather than enable since enabled is a state and enable is action and options configure state rather than trigger actions (.... I'm sooooo picky today)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated 👍

level = 'silent'
}

var logger = new Pino(level, stream, serializers, stringify, end, name, hostname, slowtime, '', cache, formatOpts)
if (cache) {
onExit(function (code, evt) {
Expand Down
64 changes: 64 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,70 @@ test('pino transform', function (t) {
instance.info('hello world')
})

test('enable', function (t) {
var instance = pino({
level: 'trace',
enable: false
}, sink(function (chunk, enc, cb) {
t.fail('no data should be logged')
}))

Object.keys(pino.levels.values).forEach(function (level) {
instance[level]('hello world')
})
t.end()
})

test('silent level', function (t) {
var instance = pino({
level: 'silent'
}, sink(function (chunk, enc, cb) {
t.fail('no data should be logged')
}))

Object.keys(pino.levels.values).forEach(function (level) {
instance[level]('hello world')
})
t.end()
})

test('setting level to 100', function (t) {
var instance = pino({
level: 100
}, sink(function (chunk, enc, cb) {
t.fail('no data should be logged')
}))

Object.keys(pino.levels.values).forEach(function (level) {
instance[level]('hello world')
})
t.end()
})

test('exposed levels', function (t) {
t.plan(1)
t.deepEqual(Object.keys(pino.levels.values), [
'fatal',
'error',
'warn',
'info',
'debug',
'trace'
])
})

test('exposed labels', function (t) {
t.plan(1)
t.deepEqual(Object.keys(pino.levels.labels), [
'10',
'20',
'30',
'40',
'50',
'60'
])
})

test('level-change event', function (t) {
var instance = pino()
var handle = function (lvl, val, prevLvl, prevVal) {
Expand Down