Skip to content
Grant Carthew edited this page Oct 25, 2024 · 7 revisions

perj stringifyFunction Option

The stringifyFunction option enables you supply a custom stringify function.

Type: Function

Default: stringify

Valid: function (value) { return // valid JSON }

Description:

The stringifyFunction option is used to change the function used to create JSON output.

By exposing this option you have the flexibility of changing the default stringify function to any other stringify function.

WARNING: Changing this options will customize the JSON output from perj. It does not change the JavaScript object you have access to if you use the passThrough option with a custom write function.

Example

This example changes the stringifyFunction to the fast-redact function:

import Perj from 'perj'
import fastRedact from 'fast-redact'
const mockReq = {
  headers: {
    host: 'http://example.com',
    cookie: `secret`,
    referer: `http://wikipedia.com`
  }
}
const stringifyFunction = fastRedact({
  paths: ['headers.cookie', 'headers.referer']
})

const log = new Perj({ stringifyFunction })
log.info(mockReq) 

/*

Standard out will be:

{"level":"info","lvl":30,"time":1525643291716,"msg":"","data":{"headers":{"host":"http://example.com","cookie":"[REDACTED]","referer":"[REDACTED]"}}}

*/