Skip to content

Commit

Permalink
Add a colorizeObjects option to disable object colorization (#403)
Browse files Browse the repository at this point in the history
This allows log parsers that can detect JSON at the end of a log line to
do so without being thrown off by ANSI colors.
  • Loading branch information
Avaq authored Feb 24, 2023
1 parent 3bada4f commit 2f1af9e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ node app.js | pino-pretty
### CLI Arguments

- `--colorize` (`-c`): Adds terminal color escape sequences to the output.
- `--colorizeObjects` (`-C`): Allows suppressing colorization of objects when set to `false`. In combination with `--singleLine`, this ensures that the end of each line is parsable JSON.
- `--crlf` (`-f`): Appends carriage return and line feed, instead of just a line
feed, to the formatted log line.
- `--errorProps` (`-e`): When formatting an error object, display this list
Expand Down Expand Up @@ -235,6 +236,7 @@ The options accepted have keys corresponding to the options described in [CLI Ar
```js
{
colorize: colorette.isColorSupported, // --colorize
colorizeObjects: true, //--colorizeObjects
crlf: false, // --crlf
errorLikeObjectKeys: ['err', 'error'], // --errorLikeObjectKeys
errorProps: '', // --errorProps
Expand Down
1 change: 1 addition & 0 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const DEFAULT_VALUE = '\0default'
let opts = minimist(process.argv, {
alias: {
colorize: 'c',
colorizeObjects: 'C',
crlf: 'f',
errorProps: 'e',
levelFirst: 'l',
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ interface PrettyOptions_ {
* @default false
*/
colorize?: boolean;
/**
* If set to false while `colorize` is `true`, will output JSON objects without color.
* @default true
*/
colorizeObjects?: boolean;
/**
* Appends carriage return and line feed, instead of just a line feed, to the formatted log line.
* @default false
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const jsonParser = input => {

const defaultOptions = {
colorize: isColorSupported,
colorizeObjects: true,
crlf: false,
errorLikeObjectKeys: ERROR_LIKE_KEYS,
errorProps: '',
Expand Down Expand Up @@ -96,6 +97,7 @@ function prettyFactory (options) {
const hideObject = opts.hideObject
const singleLine = opts.singleLine
const colorizer = colors(opts.colorize, customColors, useOnlyCustomProps)
const objectColorizer = opts.colorizeObjects ? colorizer : colors(false, [], false)

return pretty

Expand Down Expand Up @@ -193,7 +195,7 @@ function prettyFactory (options) {
eol: EOL,
ident: IDENT,
singleLine,
colorizer
colorizer: objectColorizer
})

// In single line mode, include a space only if prettified version isn't empty
Expand Down
16 changes: 16 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ test('basic prettifier tests', (t) => {
log.info('foo')
})

t.test('will omit color codes from objects when colorizeObjects = false', (t) => {
t.plan(1)
const pretty = prettyFactory({ colorize: true, singleLine: true, colorizeObjects: false })
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.equal(
formatted,
`[${formattedEpoch}] \u001B[32mINFO\u001B[39m (${pid}): \u001B[36mfoo\u001B[39m {"foo":"bar"}\n`
)
cb()
}
}))
log.info({ foo: 'bar' }, 'foo')
})

t.test('can swap date and level position', (t) => {
t.plan(1)
const destination = new Writable({
Expand Down

0 comments on commit 2f1af9e

Please sign in to comment.