Skip to content

Commit

Permalink
use boolean flag to ensure serializers are applied once (#1976)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmyakin authored May 15, 2024
1 parent 2af5abd commit 0109b15
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
22 changes: 13 additions & 9 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,10 @@ function createWrap (self, opts, rootLogger, level) {
const proto = (Object.getPrototypeOf && Object.getPrototypeOf(this) === _console) ? _console : this
for (var i = 0; i < args.length; i++) args[i] = arguments[i]

if (opts.serialize && !opts.transmit) {
var argsIsSerialized = false
if (opts.serialize) {
applySerializers(args, this._serialize, this.serializers, this._stdErrSerialize)
argsIsSerialized = true
}
if (opts.asObject || opts.formatters) {
write.call(proto, asObject(this, level, args, ts, opts.formatters))
Expand All @@ -322,7 +324,7 @@ function createWrap (self, opts, rootLogger, level) {
transmitValue: rootLogger.levels.values[opts.transmit.level || self._level],
send: opts.transmit.send,
val: levelToValue(self._level, rootLogger)
}, args)
}, args, argsIsSerialized)
}
}
})(self[baseLogFunctionSymbol][level])
Expand Down Expand Up @@ -376,20 +378,22 @@ function applySerializers (args, serialize, serializers, stdErrSerialize) {
}
}

function transmit (logger, opts, args) {
function transmit (logger, opts, args, argsIsSerialized = false) {
const send = opts.send
const ts = opts.ts
const methodLevel = opts.methodLevel
const methodValue = opts.methodValue
const val = opts.val
const bindings = logger._logEvent.bindings

applySerializers(
args,
logger._serialize || Object.keys(logger.serializers),
logger.serializers,
logger._stdErrSerialize === undefined ? true : logger._stdErrSerialize
)
if (!argsIsSerialized) {
applySerializers(
args,
logger._serialize || Object.keys(logger.serializers),
logger.serializers,
logger._stdErrSerialize === undefined ? true : logger._stdErrSerialize
)
}

logger._logEvent.ts = ts
logger._logEvent.messages = args.filter(function (arg) {
Expand Down
22 changes: 22 additions & 0 deletions test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,28 @@ test('opts.browser.serialize and opts.asObject only serializes log data once', (
end()
})

test('opts.browser.serialize, opts.asObject and opts.browser.transmit only serializes log data once', ({ end, ok, is }) => {
const instance = require('../browser')({
serializers: {
extras (data) {
return { serializedExtras: data }
}
},
browser: {
serialize: ['extras'],
asObject: true,
transmit: {
send (level, o) {
is(o.messages[0].extras.serializedExtras, 'world')
}
}
}
})

instance.info({ extras: 'world' }, 'test')
end()
})

test('opts.browser.write func log single string', ({ end, ok, is }) => {
const instance = pino({
browser: {
Expand Down

0 comments on commit 0109b15

Please sign in to comment.