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

fix: Ensure silent log level is respected with browser.transmit option #2052

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ function set (self, opts, rootLogger, level) {
if (!opts.transmit) return

const transmitLevel = opts.transmit.level || self.level
const transmitValue = rootLogger.levels.values[transmitLevel]
const methodValue = rootLogger.levels.values[level]
const transmitValue = levelToValue(transmitLevel, rootLogger)
const methodValue = levelToValue(level, rootLogger)
if (methodValue < transmitValue) return
}

Expand Down Expand Up @@ -322,8 +322,8 @@ function createWrap (self, opts, rootLogger, level) {

if (opts.transmit) {
const transmitLevel = opts.transmit.level || self._level
const transmitValue = rootLogger.levels.values[transmitLevel]
const methodValue = rootLogger.levels.values[level]
const transmitValue = levelToValue(transmitLevel, rootLogger)
const methodValue = levelToValue(level, rootLogger)
if (methodValue < transmitValue) return
transmit(this, {
ts,
Expand Down
48 changes: 48 additions & 0 deletions test/browser-transmit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,51 @@ test('does not log below configured level', ({ end, is }) => {

end()
})

test('silent level prevents logging even with transmit', ({ end, fail }) => {
const logger = pino({
level: 'silent',
browser: {
write () {
fail('no data should be logged by the write method')
},
transmit: {
send () {
fail('no data should be logged by the send method')
}
}
}
})

Object.keys(pino.levels.values).forEach((level) => {
logger[level]('ignored')
})

end()
})

test('does not call send when transmit.level is set to silent', ({ end, fail, is }) => {
let c = 0
const logger = pino({
level: 'trace',
browser: {
write () {
c++
},
transmit: {
level: 'silent',
send () {
fail('no data should be logged by the transmit method')
}
}
}
})

const levels = Object.keys(pino.levels.values)
levels.forEach((level) => {
logger[level]('message')
})

is(c, levels.length, 'write must be called exactly once per level')
end()
})