node-bunyan
node-bunyan copied to clipboard
Stringify BigInts
> const log = require('bunyan').createLogger({ name: "bigints" });
undefined
> log.info({ bignumber: 11111n }, 'here is a bigint');
Uncaught TypeError: Do not know how to serialize a BigInt
at JSON.stringify (<anonymous>)
at module.exports (/home/ntr/synth/synth-library/api-ts/node_modules/safe-json-stringify/index.js:66:14)
at fastAndSafeJsonStringify (/home/ntr/synth/synth-library/api-ts/node_modules/bunyan/lib/bunyan.js:1220:24)
at Logger._emit (/home/ntr/synth/synth-library/api-ts/node_modules/bunyan/lib/bunyan.js:911:15)
at Logger.info (/home/ntr/synth/synth-library/api-ts/node_modules/bunyan/lib/bunyan.js:1045:24)
>
I have submitted a PR to match so this issue will be fixable simply by a version upgrade. https://github.com/debitoor/safe-json-stringify/pull/12
Just noting we are running into the same issue. Doesn't look like the PR is getting much (any) attention.
Does anyone have a good work around in the meantime? Is it possible to add a serializer to bunyan that can target BigInts without having to define every key that might contain a BigInt?
I took a stab at fixing this using the second arg to JSON.stringify
rather than an upstream change to safeJsonStringify
, this has the added benefit of being able to natively work with JSON.stringify
which might be a bit faster.
@trentm I know this is an old issue and it's been a while, but I was hoping you could take a look at this PR (which has unit tests and follows the contributor guidelines). This is a pretty impactful fix, since the bug results in the default serializer throwing an error. I'd really appreciate it! I can work around by patching my own version of this, but I would love for others doing similar things to get the benefit too.