Skip to content

Commit

Permalink
fix: support customLevels on browser (#1707)
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusvellone authored May 4, 2023
1 parent c1c8eb4 commit 4ede1c1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
42 changes: 34 additions & 8 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ function pino (opts) {
opts.browser.serialize.indexOf('!stdSerializers.err') > -1
) stdErrSerialize = false

const levels = ['error', 'fatal', 'warn', 'info', 'debug', 'trace']
const customLevels = Object.keys(opts.customLevels || {})
const levels = ['error', 'fatal', 'warn', 'info', 'debug', 'trace'].concat(customLevels)

if (typeof proto === 'function') {
proto.error = proto.fatal = proto.warn =
proto.info = proto.debug = proto.trace = proto
levels.forEach(function (level) {
proto[level] = proto
})
}
if (opts.enabled === false || opts.browser.disabled) opts.level = 'silent'
const level = opts.level || 'info'
Expand All @@ -74,7 +76,7 @@ function pino (opts) {
levels,
timestamp: getTimeFunction(opts)
}
logger.levels = pino.levels
logger.levels = getLevels(opts)
logger.level = level

logger.setMaxListeners = logger.getMaxListeners =
Expand Down Expand Up @@ -112,6 +114,10 @@ function pino (opts) {
set(setOpts, logger, 'info', 'log')
set(setOpts, logger, 'debug', 'log')
set(setOpts, logger, 'trace', 'log')

customLevels.forEach(function (level) {
set(setOpts, logger, level, 'log')
})
}

function child (bindings, childOptions) {
Expand Down Expand Up @@ -155,6 +161,26 @@ function pino (opts) {
return logger
}

function getLevels (opts) {
const customLevels = opts.customLevels || {}

const values = Object.assign({}, pino.levels.values, customLevels)
const labels = Object.assign({}, pino.levels.labels, invertObject(customLevels))

return {
values,
labels
}
}

function invertObject (obj) {
const inverted = {}
Object.keys(obj).forEach(function (key) {
inverted[obj[key]] = key
})
return inverted
}

pino.levels = {
values: {
fatal: 60,
Expand Down Expand Up @@ -204,15 +230,15 @@ function wrap (opts, logger, level) {

if (opts.transmit) {
const transmitLevel = opts.transmit.level || logger.level
const transmitValue = pino.levels.values[transmitLevel]
const methodValue = pino.levels.values[level]
const transmitValue = logger.levels.values[transmitLevel]
const methodValue = logger.levels.values[level]
if (methodValue < transmitValue) return
transmit(this, {
ts,
methodLevel: level,
methodValue,
transmitLevel,
transmitValue: pino.levels.values[opts.transmit.level || logger.level],
transmitValue: logger.levels.values[opts.transmit.level || logger.level],
send: opts.transmit.send,
val: logger.levelVal
}, args)
Expand All @@ -229,7 +255,7 @@ function asObject (logger, level, args, ts) {
if (ts) {
o.time = ts
}
o.level = pino.levels.values[level]
o.level = logger.levels.values[level]
let lvl = (logger._childLevel | 0) + 1
if (lvl < 1) lvl = 1
// deliberate, catching objects, arrays
Expand Down
23 changes: 23 additions & 0 deletions test/browser-levels.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,29 @@ test('set the level via constructor', ({ end, same, is }) => {
end()
})

test('set custom level and use it', ({ end, same, is }) => {
const expected = [
{
level: 31,
msg: 'this is a custom level'
}
]
const instance = pino({
customLevels: {
success: 31
},
browser: {
write (actual) {
checkLogObjects(is, same, actual, expected.shift())
}
}
})

instance.success('this is a custom level')

end()
})

test('the wrong level throws', ({ end, throws }) => {
const instance = pino()
throws(() => {
Expand Down

0 comments on commit 4ede1c1

Please # to comment.