Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 6 additions & 2 deletions splat.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const escapedPercent = /%%/g;

class Splatter {
constructor(opts) {
this.options = opts;
this.options = opts || {};
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
25 changes: 22 additions & 3 deletions test/splat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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'
));
Expand Down Expand Up @@ -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(),
Expand Down