UNPKG

5.81 kBMarkdownView Raw
1<a id="intro"></a>
2# pino-pretty
3
4[![NPM Package Version](https://img.shields.io/npm/v/pino-pretty)](https://www.npmjs.com/package/pino-pretty)
5[![Build Status](https://img.shields.io/github/workflow/status/pinojs/pino-pretty/CI)](https://github.com/pinojs/pino-pretty/actions?query=workflow%3ACI)
6[![Coverage Status](https://img.shields.io/coveralls/github/pinojs/pino-pretty)](https://codecov.io/gh/pinojs/pino-pretty)
7
8This module provides a basic [ndjson](http://ndjson.org/) formatter. If an
9incoming line looks like it could be a log line from an ndjson logger, in
10particular the [Pino](https://getpino.io/) logging library, then it will apply
11extra formatting by considering things like the log level and timestamp.
12
13A standard Pino log line like:
14
15```
16{"level":30,"time":1522431328992,"msg":"hello world","pid":42,"hostname":"foo","v":1}
17```
18
19Will format to:
20
21```
22[1522431328992] INFO (42 on foo): hello world
23```
24
25<a id="example"></a>
26## Example
27
28Using the [example script][exscript] from the Pino module, and specifying
29that logs should be colored and the time translated, we can see what the
30prettified logs will look like:
31
32![demo](demo.png)
33
34[exscript]: https://github.com/pinojs/pino/blob/fc4c83b/example.js
35
36<a id="install"></a>
37## Install
38
39```sh
40$ npm install -g pino-pretty
41```
42
43<a id="usage"></a>
44## Usage
45
46It's recommended to use `pino-pretty` with `pino`
47by piping output to the CLI tool:
48
49```sh
50node app.js | pino-pretty
51```
52
53<a id="cliargs"></a>
54### CLI Arguments
55
56- `--colorize` (`-c`): Adds terminal color escape sequences to the output.
57- `--crlf` (`-f`): Appends carriage return and line feed, instead of just a line
58 feed, to the formatted log line.
59- `--errorProps` (`-e`): When formatting an error object, display this list
60 of properties. The list should be a comma separated list of properties Default: `''`.
61- `--levelFirst` (`-l`): Display the log level name before the logged date and time.
62- `--errorLikeObjectKeys` (`-k`): Define the log keys that are associated with
63 error like objects. Default: `err,error`.
64- `--messageKey` (`-m`): Define the key that contains the main log message.
65 Default: `msg`.
66- `--levelKey` (`--levelKey`): Define the key that contains the level of the log.
67 Default: `level`.
68- `--levelLabel` (`-b`): Output the log level using the specified label.
69 Default: `levelLabel`.
70- `--messageFormat` (`-o`): Format output of message, e.g. `{levelLabel} - {pid} - url:{request.url}` will output message: `INFO - 1123 - url:localhost:3000/test`
71 Default: `false`
72- `--timestampKey` (`-a`): Define the key that contains the log timestamp.
73 Default: `time`.
74- `--translateTime` (`-t`): Translate the epoch time value into a human readable
75 date and time string. This flag also can set the format string to apply when
76 translating the date to human readable format. For a list of available pattern
77 letters see the [`dateformat` documentation](https://www.npmjs.com/package/dateformat).
78 - The default format is `yyyy-mm-dd HH:MM:ss.l o` in UTC.
79 - Require a `SYS:` prefix to translate time to the local system's timezone. A
80 shortcut `SYS:standard` to translate time to `yyyy-mm-dd HH:MM:ss.l o` in
81 system timezone.
82- `--search` (`-s`): Specify a search pattern according to
83 [jmespath](http://jmespath.org/).
84- `--ignore` (`-i`): Ignore one or several keys: (`-i time,hostname`)
85- `--hideObject` (`-H`): Hide objects from output (but not error object)
86- `--config`: Specify a path to a config file containing the pino-pretty options. pino-pretty will attempt to read from a `.pino-prettyrc` in your current directory (`process.cwd`) if not specified
87
88<a id="integration"></a>
89## Programmatic Integration
90
91We recommend against using `pino-pretty` in production, and highly
92recommend installing `pino-pretty` as a development dependency.
93
94When installed, `pino-pretty` will be used by `pino` as the default
95prettifier.
96
97Install `pino-pretty` alongside `pino` and set the
98`prettyPrint` option to `true`:
99
100```js
101const pino = require('pino')
102const logger = pino({
103 prettyPrint: true
104})
105
106logger.info('hi')
107```
108
109The `prettyPrint` option can also be an object containing `pretty-print`
110options:
111
112```js
113const pino = require('pino')
114const logger = pino({
115 prettyPrint: { colorize: true }
116})
117
118logger.info('hi')
119```
120
121See the [Options](#options) section for all possible options.
122
123<a id="options"></a>
124### Options
125
126`pino-pretty` exports a factory function that can be used to format log strings.
127This factory function is used internally by Pino, and accepts an options argument
128with keys corresponding to the options described in [CLI Arguments](#cliargs):
129
130```js
131{
132 colorize: chalk.supportsColor, // --colorize
133 crlf: false, // --crlf
134 errorLikeObjectKeys: ['err', 'error'], // --errorLikeObjectKeys
135 errorProps: '', // --errorProps
136 levelFirst: false, // --levelFirst
137 messageKey: 'msg', // --messageKey
138 levelKey: 'level', // --levelKey
139 messageFormat: false // --messageFormat
140 timestampKey: 'time', // --timestampKey
141 translateTime: false, // --translateTime
142 search: 'foo == `bar`', // --search
143 ignore: 'pid,hostname', // --ignore,
144 hideObject: false // --hideObject
145 customPrettifiers: {}
146}
147```
148
149The `colorize` default follows
150[`chalk.supportsColor`](https://www.npmjs.com/package/chalk#chalksupportscolor).
151
152`customPrettifiers` option provides the ability to add a custom prettify function
153for specific log properties. `customPrettifiers` is an object, where keys are
154log properties which will be prettified and value is the prettify function itself.
155For example, if a log line contains a `query` property,
156you can specify a prettifier for it:
157```js
158{
159 customPrettifiers: {
160 query: prettifyQuery
161 }
162}
163//...
164const prettifyQuery = value => {
165 // do some prettify magic
166}
167```
168
169<a id="license"><a>
170## License
171
172MIT License