diff --git a/lib/tools.js b/lib/tools.js index 1bebeed6a..296e13558 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -5,7 +5,7 @@ const format = require('quick-format-unescaped') const { mapHttpRequest, mapHttpResponse } = require('pino-std-serializers') const SonicBoom = require('sonic-boom') -const stringifySafe = require('fast-safe-stringify') +const stringifySafe = require('json-stringify-safe') const { lsCacheSym, chindingsSym, @@ -437,7 +437,11 @@ function stringify (obj) { try { return JSON.stringify(obj) } catch (_) { - return stringifySafe(obj) + try { + return stringifySafe(obj) + } catch (_) { + return '"[unable to serialize, circular reference is too complex to analyze]"' + } } } diff --git a/package.json b/package.json index 2f2534e97..b19aed828 100644 --- a/package.json +++ b/package.json @@ -104,6 +104,7 @@ "on-exit-leak-free": "^0.1.0", "pino-abstract-transport": "^0.2.0", "pino-std-serializers": "^4.0.0", + "json-stringify-safe": "^5.0.1", "quick-format-unescaped": "^4.0.3", "sonic-boom": "^2.1.0", "thread-stream": "^0.11.0" diff --git a/test/basic.test.js b/test/basic.test.js index 8828edcf7..27c4b377c 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -593,8 +593,7 @@ test('children with same names render in correct order', async ({ equal }) => { equal(a, 3, 'last logged object takes precedence') }) -// https://github.com/pinojs/pino/pull/251 - use this.stringify -test('use `fast-safe-stringify` to avoid circular dependencies', async ({ same }) => { +test('use `json-stringify-safe` to avoid circular dependencies', async ({ same }) => { const stream = sink() const root = pino(stream) // circular depth @@ -602,10 +601,10 @@ test('use `fast-safe-stringify` to avoid circular dependencies', async ({ same } obj.a = obj root.info(obj) const { a } = await once(stream, 'data') - same(a, { a: '[Circular]' }) + same(a, { a: '[Circular ~]' }) }) -test('fast-safe-stringify must be used when interpolating', async (t) => { +test('json-stringify-safe must be used when interpolating', async (t) => { const stream = sink() const instance = pino(stream) @@ -614,7 +613,7 @@ test('fast-safe-stringify must be used when interpolating', async (t) => { instance.info('test %j', o) const { msg } = await once(stream, 'data') - t.equal(msg, 'test {"a":{"b":{"c":"[Circular]"}}}') + t.equal(msg, 'test {"a":{"b":{"c":"[Circular ~.a.b]"}}}') }) test('throws when setting useOnlyCustomLevels without customLevels', async ({ throws }) => {