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..e307083 100644 --- a/test/splat.test.js +++ b/test/splat.test.js @@ -4,15 +4,16 @@ 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 * 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 +30,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 +107,24 @@ 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 => { + const deepPrinted = util.inspect({ a: { b: { c: { d: { e: { f: 1 }}}}}}, { depth: null }); + assume(info.message).equals(`printing object ${deepPrinted}`); + }, + { inspectOptions: { depth: null }} + )); + it('No [SPLAT] does not crash', () => { return helpers.assumeFormatted( splat(),