Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pinojs/pino-pretty
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v7.1.0
Choose a base ref
...
head repository: pinojs/pino-pretty
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v7.2.0
Choose a head ref
  • 9 commits
  • 7 files changed
  • 5 contributors

Commits on Oct 14, 2021

  1. Update docs to v7 (#247)

    PabloSzx authored Oct 14, 2021
    Copy the full SHA
    ea6352b View commit details

Commits on Oct 18, 2021

  1. Bump actions/checkout from 2.3.4 to 2.3.5 (#250)

    Bumps [actions/checkout](https://github.com/actions/checkout) from 2.3.4 to 2.3.5.
    - [Release notes](https://github.com/actions/checkout/releases)
    - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
    - [Commits](actions/checkout@v2.3.4...v2.3.5)
    
    ---
    updated-dependencies:
    - dependency-name: actions/checkout
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Oct 18, 2021
    Copy the full SHA
    4b93241 View commit details

Commits on Oct 23, 2021

  1. Copy the full SHA
    cc0902a View commit details

Commits on Oct 25, 2021

  1. Copy the full SHA
    cd44cdd View commit details

Commits on Oct 31, 2021

  1. Copy the full SHA
    64672ee View commit details

Commits on Nov 3, 2021

  1. Bump actions/checkout from 2.3.5 to 2.4.0 (#258)

    Bumps [actions/checkout](https://github.com/actions/checkout) from 2.3.5 to 2.4.0.
    - [Release notes](https://github.com/actions/checkout/releases)
    - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
    - [Commits](actions/checkout@v2.3.5...v2.4.0)
    
    ---
    updated-dependencies:
    - dependency-name: actions/checkout
      dependency-type: direct:production
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Nov 3, 2021
    Copy the full SHA
    40261a5 View commit details
  2. Bump pino-abstract-transport from 0.4.0 to 0.5.0 (#259)

    Bumps [pino-abstract-transport](https://github.com/pinojs/pino-abstract-transport) from 0.4.0 to 0.5.0.
    - [Release notes](https://github.com/pinojs/pino-abstract-transport/releases)
    - [Commits](pinojs/pino-abstract-transport@v0.4.0...v0.5.0)
    
    ---
    updated-dependencies:
    - dependency-name: pino-abstract-transport
      dependency-type: direct:production
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Nov 3, 2021
    Copy the full SHA
    6037ddc View commit details
  3. Copy the full SHA
    c3a1348 View commit details
  4. Bumped v7.2.0

    mcollina committed Nov 3, 2021
    Copy the full SHA
    2945f23 View commit details
Showing with 154 additions and 16 deletions.
  1. +1 −1 .github/workflows/ci.yml
  2. +1 −0 .gitignore
  3. +78 −11 Readme.md
  4. +12 −1 index.js
  5. +2 −2 package.json
  6. +59 −0 test/basic.test.js
  7. +1 −1 test/types/pino-pretty.test-d.ts
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ jobs:
os: [ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout@v2.3.4
uses: actions/checkout@v2.4.0

- name: Use Node.js
uses: actions/setup-node@v2.4.1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
shrinkwrap.yaml
package-lock.json
yarn.lock
pnpm-lock.yaml

# Logs
logs
89 changes: 78 additions & 11 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -24,6 +24,9 @@ Will format to:
[1522431328992] INFO (42 on foo): hello world
```

If you landed on this page due to the deprecation of the `prettyPrint` option
of `pino`, read the [Programmatic Integration](#integration) section.

<a id="example"></a>
## Example

@@ -96,41 +99,98 @@ node app.js | pino-pretty
We recommend against using `pino-pretty` in production and highly
recommend installing `pino-pretty` as a development dependency.

When installed, `pino-pretty` will be used by `pino` as the default
prettifier.

Install `pino-pretty` alongside `pino` and set the
`prettyPrint` option to `true`:
Install `pino-pretty` alongside `pino` and set the transport target to `'pino-pretty'`:

```js
const pino = require('pino')
const logger = pino({
prettyPrint: true
transport: {
target: 'pino-pretty'
},
})

logger.info('hi')
```

The `prettyPrint` option can also be an object containing `pretty-print`
options:
The transport option can also have an options object containing `pino-pretty` options:

```js
const pino = require('pino')
const logger = pino({
transport: {
target: 'pino-pretty',
options: {
colorize: true
}
}
})

logger.info('hi')
```

Use it as a stream:

```js
const pino = require('pino')
const pretty = require('pino-pretty')
const logger = pino(pretty())

logger.info('hi')
```

Options are also supported:

```js
const pino = require('pino')
const pretty = require('pino-pretty')
const stream = pretty({
prettyPrint: { colorize: true }
})
const logger = pino(stream)

logger.info('hi')
```

See the [Options](#options) section for all possible options.

### Handling non-serializable options

Using the new [pino v7+
transports](https://getpino.io/#/docs/transports?id=v7-transports) not all
options are serializable, for example if you want to use `messageFormat` as a
function you will need to wrap `pino-pretty` in a custom module.

Executing `main.js` below will log a colorized `hello world` message using a
custom function `messageFormat`:

```js
// main.js
const pino = require('pino')

const logger = pino({
transport: {
target: './pino-pretty-transport',
options: {
colorize: true
}
},
})

logger.info('world')
```

```js
// pino-pretty-transport.js
module.exports = opts => require('pino-pretty')({
...opts,
messageFormat: (log, messageKey) => `hello ${log[messageKey]}`
})
```

<a id="options"></a>
### Options

`pino-pretty` exports a factory function that can be used to format log strings.
This factory function is used internally by Pino, and accepts an options argument
with keys corresponding to the options described in [CLI Arguments](#cliargs):
The options accepted have keys corresponding to the options described in [CLI Arguments](#cliargs):

```js
{
@@ -147,6 +207,13 @@ with keys corresponding to the options described in [CLI Arguments](#cliargs):
ignore: 'pid,hostname', // --ignore
hideObject: false, // --hideObject
singleLine: false, // --singleLine

// The file or file descriptor (1 is stdout) to write to
destination: 1,

// Alternatively, pass a `sonic-boom` instance (allowing more flexibility):
// destination: new SonicBoom({ dest: 'a/file', mkdir: true })

customPrettifiers: {}
}
```
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -183,7 +183,18 @@ function build (opts = {}) {
}
})

const destination = sonic({ dest: opts.destination || 1, sync: false })
let destination

if (typeof opts.destination === 'object' && typeof opts.destination.write === 'function') {
destination = opts.destination
} else {
destination = sonic({
dest: opts.destination || 1,
append: opts.append,
mkdir: opts.mkdir,
sync: false
})
}
/* istanbul ignore else */
if (destination.fd === 1) {
// We cannot close the output
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pino-pretty",
"version": "7.1.0",
"version": "7.2.0",
"description": "Prettifier for Pino log lines",
"type": "commonjs",
"main": "index.js",
@@ -37,7 +37,7 @@
"dateformat": "^4.6.3",
"fast-safe-stringify": "^2.0.7",
"joycon": "^3.0.0",
"pino-abstract-transport": "^0.4.0",
"pino-abstract-transport": "^0.5.0",
"pump": "^3.0.0",
"readable-stream": "^3.6.0",
"rfdc": "^1.3.0",
59 changes: 59 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
@@ -5,9 +5,17 @@ const os = require('os')
const test = require('tap').test
const pino = require('pino')
const dateformat = require('dateformat')
const path = require('path')
const rimraf = require('rimraf')
const { join } = require('path')
const fs = require('fs')
const pinoPretty = require('..')
const SonicBoom = require('sonic-boom')
const _prettyFactory = pinoPretty.prettyFactory

// Disable pino warnings
process.removeAllListeners('warning')

function prettyFactory (opts) {
if (!opts) {
opts = { colorize: false }
@@ -748,5 +756,56 @@ test('basic prettifier tests', (t) => {
t.doesNotThrow(pinoPretty)
})

t.test('stream usage', async (t) => {
t.plan(1)
const tmpDir = path.join(__dirname, '.tmp_' + Date.now())
t.teardown(() => rimraf(tmpDir, noop))

const destination = join(tmpDir, 'output')

const pretty = pinoPretty({
singleLine: true,
colorize: false,
mkdir: true,
append: false,
destination: new SonicBoom({ dest: destination, async: false, mkdir: true, append: true }),
customPrettifiers: {
upper: val => val.toUpperCase(),
undef: () => undefined
}
})
const log = pino(pretty)
log.info({ msg: 'message', extra: { foo: 'bar', number: 42 }, upper: 'foobar', undef: 'this will not show up' })

await watchFileCreated(destination)

const formatted = fs.readFileSync(destination, 'utf8')

t.equal(formatted, `[${epoch}] INFO (${pid} on ${hostname}): message {"extra":{"foo":"bar","number":42},"upper":"FOOBAR"}\n`)
})

t.end()
})

function watchFileCreated (filename) {
return new Promise((resolve, reject) => {
const TIMEOUT = 2000
const INTERVAL = 100
const threshold = TIMEOUT / INTERVAL
let counter = 0
const interval = setInterval(() => {
// On some CI runs file is created but not filled
if (fs.existsSync(filename) && fs.statSync(filename).size !== 0) {
clearInterval(interval)
resolve()
} else if (counter <= threshold) {
counter++
} else {
clearInterval(interval)
reject(new Error(`${filename} was not created.`))
}
}, INTERVAL)
})
}

function noop () {}
2 changes: 1 addition & 1 deletion test/types/pino-pretty.test-d.ts
Original file line number Diff line number Diff line change
@@ -53,7 +53,7 @@ const options2: PrettyOptions = {
};

const pretty = prettyFactory(options);
expectType<Prettifier>(pretty)
expectType<Prettifier>(pretty);

expectType<Prettifier>(PinoPrettyNamed(options));
expectType<Prettifier>(PinoPrettyDefault(options));