From 7230e1350cc8571c15cdad3e4e620a5a3cc7946b Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Wed, 11 Nov 2020 18:17:14 +0100 Subject: [PATCH 1/2] allow to configure util.inspect in splat --- README.md | 3 +++ splat.js | 8 ++++++-- test/splat.test.js | 23 ++++++++++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f58fe8b..26d7501 100644 --- a/README.md +++ b/README.md @@ -563,6 +563,9 @@ console.log(info[MESSAGE]); ### Splat The `splat` format transforms the message by using `util.format` to complete any `info.message` provided it has string interpolation tokens. +It accepts the following options: + +* **inspectOptions**: Option object that is passed to `util.inspect` when finalizing the message. ```js const { format } = require('logform'); diff --git a/splat.js b/splat.js index 08ff6f7..5e20a49 100644 --- a/splat.js +++ b/splat.js @@ -19,7 +19,7 @@ const escapedPercent = /%%/g; class Splatter { constructor(opts) { - this.options = opts; + this.options = opts || {}; } /** @@ -68,7 +68,11 @@ class Splatter { } } - info.message = util.format(msg, ...splat); + if (this.options.inspectOptions) { + info.message = util.formatWithOptions(this.options.inspectOptions, msg, ...splat); + } else { + info.message = util.format(msg, ...splat); + } return info; } diff --git a/test/splat.test.js b/test/splat.test.js index b039628..6a61bed 100644 --- a/test/splat.test.js +++ b/test/splat.test.js @@ -10,9 +10,9 @@ const helpers = require('./helpers'); * with the given { message, splat: spread } is properly interoplated * by the splat format into the `expected` value. */ -function assumeSplat(message, spread, expected) { +function assumeSplat(message, spread, expected, options) { return helpers.assumeFormatted( - splat(), + splat(options), { level: 'info', message, [SPLAT]: spread }, info => { assume(info.level).is.a('string'); @@ -29,7 +29,7 @@ function assumeSplat(message, spread, expected) { ); } -describe('splat', () => { +describe('splat', () => { // eslint-disable-line max-statements it('basic string', assumeSplat( 'just a string', [], 'just a string' )); @@ -106,6 +106,23 @@ describe('splat', () => { } )); + it('%O | object placeholder formats object', assumeSplat( + 'printing object %O', + [{ hello: 'world' }], + info => { + assume(info.message).equals('printing object { hello: \'world\' }'); + } + )); + + it('%O | object placeholder formats deep object', assumeSplat( + 'printing object %O', + [{ a: { b: { c: { d: { e: { f: 1 }}}}}}], + info => { + assume(info.message).equals(`printing object {\n a: {\n b: {\n c: { d: { e: { f: 1 } } }\n }\n }\n}`); + }, + { inspectOptions: { depth: null }} + )); + it('No [SPLAT] does not crash', () => { return helpers.assumeFormatted( splat(), From 38957c4a6e4a7800ee7e23b54f7213f4161aad9b Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Thu, 12 Nov 2020 10:53:02 +0100 Subject: [PATCH 2/2] fixed test: assertion to output from util.inspect --- test/splat.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/splat.test.js b/test/splat.test.js index 6a61bed..e307083 100644 --- a/test/splat.test.js +++ b/test/splat.test.js @@ -4,6 +4,7 @@ const { SPLAT } = require('triple-beam'); const assume = require('assume'); const splat = require('../splat'); const helpers = require('./helpers'); +const util = require('util'); /* * Helper function for asserting that an info object @@ -118,7 +119,8 @@ describe('splat', () => { // eslint-disable-line max-statements 'printing object %O', [{ a: { b: { c: { d: { e: { f: 1 }}}}}}], info => { - assume(info.message).equals(`printing object {\n a: {\n b: {\n c: { d: { e: { f: 1 } } }\n }\n }\n}`); + const deepPrinted = util.inspect({ a: { b: { c: { d: { e: { f: 1 }}}}}}, { depth: null }); + assume(info.message).equals(`printing object ${deepPrinted}`); }, { inspectOptions: { depth: null }} ));