Skip to content

Commit

Permalink
Add "silent" property with "noop" value to the pino instance (pinojs#802
Browse files Browse the repository at this point in the history
)

* Add "silent" property with "noop" value to the pino instance

* Add tests for "silent" level

* Update docs on silent level
  • Loading branch information
baterson authored Apr 10, 2020
1 parent 0bb7a06 commit 5585804
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
7 changes: 6 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [logger.warn()](#warn)
* [logger.error()](#error)
* [logger.fatal()](#fatal)
* [logger.silent()](#silent)
* [logger.child()](#child)
* [logger.bindings()](#bindings)
* [logger.flush()](#flush)
Expand Down Expand Up @@ -560,6 +561,10 @@ the process crashes or exits.
* See [`message` log method parameter](#message)
* See [`...interpolationValues` log method parameter](#interpolationvalues)

<a id="silent"><a>
### `logger.silent()`

Noop function.

<a id="child"></a>
### `logger.child(bindings) => logger`
Expand Down Expand Up @@ -669,7 +674,7 @@ The logging level is a *minimum* level based on the associated value of that lev
For instance if `logger.level` is `info` *(30)* then `info` *(30)*, `warn` *(40)*, `error` *(50)* and `fatal` *(60)* log methods will be enabled but the `trace` *(10)* and `debug` *(20)* methods, being less than 30, will not.

The `silent` logging level is a specialized level which will disable all logging,
there is no `silent` log method.
the `silent` log method is a noop function.

<a id="islevelenabled"></a>
### `logger.isLevelEnabled(level)`
Expand Down
6 changes: 4 additions & 2 deletions pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const {
final,
stringify,
buildSafeSonicBoom,
buildFormatters
buildFormatters,
noop
} = require('./lib/tools')
const { version } = require('./lib/meta')
const {
Expand Down Expand Up @@ -165,7 +166,8 @@ function pino (...args) {
[mixinSym]: mixin,
[chindingsSym]: chindings,
[formattersSym]: allFormatters,
[hooksSym]: hooks
[hooksSym]: hooks,
silent: noop
})
Object.setPrototypeOf(instance, proto)

Expand Down
52 changes: 52 additions & 0 deletions test/levels.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,55 @@ test('fatal method should call async when sync-flushing fails', ({ equal, fail,
const instance = pino(stream)
doesNotThrow(() => instance.fatal(messages[0]))
})

test('calling silent method on logger instance', async ({ fail }) => {
const instance = pino({ level: 'silent' }, sink((result, enc) => {
fail('no data should be logged')
}))
instance.silent('hello world')
})

test('calling silent method on child logger', async ({ fail }) => {
const child = pino({ level: 'silent' }, sink((result, enc) => {
fail('no data should be logged')
})).child({})
child.silent('hello world')
})

test('changing level from info to silent and back to info', async ({ is }) => {
const expected = {
level: 30,
msg: 'hello world'
}
const stream = sink()
const instance = pino({ level: 'info' }, stream)

instance.level = 'silent'
instance.info('hello world')
let result = stream.read()
is(result, null)

instance.level = 'info'
instance.info('hello world')
result = await once(stream, 'data')
check(is, result, expected.level, expected.msg)
})

test('changing level from info to silent and back to info in child logger', async ({ is }) => {
const expected = {
level: 30,
msg: 'hello world'
}
const stream = sink()
const child = pino({ level: 'info' }, stream).child({})

child.level = 'silent'
child.info('hello world')
let result = stream.read()
is(result, null)

child.level = 'info'
child.info('hello world')
result = await once(stream, 'data')
check(is, result, expected.level, expected.msg)
})

0 comments on commit 5585804

Please sign in to comment.