1 | <a id="intro"></a>
|
2 | # pino-pretty
|
3 |
|
4 | [](https://www.npmjs.com/package/pino-pretty)
|
5 | [](https://github.com/pinojs/pino-pretty/actions?query=workflow%3ACI)
|
6 | [](https://codecov.io/gh/pinojs/pino-pretty)
|
7 |
|
8 | This module provides a basic [ndjson](http://ndjson.org/) formatter. If an
|
9 | incoming line looks like it could be a log line from an ndjson logger, in
|
10 | particular the [Pino](https://getpino.io/) logging library, then it will apply
|
11 | extra formatting by considering things like the log level and timestamp.
|
12 |
|
13 | A standard Pino log line like:
|
14 |
|
15 | ```
|
16 | {"level":30,"time":1522431328992,"msg":"hello world","pid":42,"hostname":"foo","v":1}
|
17 | ```
|
18 |
|
19 | Will format to:
|
20 |
|
21 | ```
|
22 | [1522431328992] INFO (42 on foo): hello world
|
23 | ```
|
24 |
|
25 | <a id="example"></a>
|
26 | ## Example
|
27 |
|
28 | Using the [example script][exscript] from the Pino module, and specifying
|
29 | that logs should be colored and the time translated, we can see what the
|
30 | prettified logs will look like:
|
31 |
|
32 | 
|
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 |
|
46 | It's recommended to use `pino-pretty` with `pino`
|
47 | by piping output to the CLI tool:
|
48 |
|
49 | ```sh
|
50 | node 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 |
|
91 | We recommend against using `pino-pretty` in production, and highly
|
92 | recommend installing `pino-pretty` as a development dependency.
|
93 |
|
94 | When installed, `pino-pretty` will be used by `pino` as the default
|
95 | prettifier.
|
96 |
|
97 | Install `pino-pretty` alongside `pino` and set the
|
98 | `prettyPrint` option to `true`:
|
99 |
|
100 | ```js
|
101 | const pino = require('pino')
|
102 | const logger = pino({
|
103 | prettyPrint: true
|
104 | })
|
105 |
|
106 | logger.info('hi')
|
107 | ```
|
108 |
|
109 | The `prettyPrint` option can also be an object containing `pretty-print`
|
110 | options:
|
111 |
|
112 | ```js
|
113 | const pino = require('pino')
|
114 | const logger = pino({
|
115 | prettyPrint: { colorize: true }
|
116 | })
|
117 |
|
118 | logger.info('hi')
|
119 | ```
|
120 |
|
121 | See 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.
|
127 | This factory function is used internally by Pino, and accepts an options argument
|
128 | with 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 |
|
149 | The `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
|
153 | for specific log properties. `customPrettifiers` is an object, where keys are
|
154 | log properties which will be prettified and value is the prettify function itself.
|
155 | For example, if a log line contains a `query` property,
|
156 | you can specify a prettifier for it:
|
157 | ```js
|
158 | {
|
159 | customPrettifiers: {
|
160 | query: prettifyQuery
|
161 | }
|
162 | }
|
163 | //...
|
164 | const prettifyQuery = value => {
|
165 | // do some prettify magic
|
166 | }
|
167 | ```
|
168 |
|
169 | `messageFormat` option allows you to customize the message output. The format can be defined by a template `string` like this:
|
170 | ```js
|
171 | {
|
172 | messageFormat: '{levelLabel} - {pid} - url:{request.url}'
|
173 | }
|
174 | ```
|
175 | But this option can also be defined as a `function` with this prototype:
|
176 | ```js
|
177 | {
|
178 | messageFormat: (log, messageKey, levelLabel) => {
|
179 | // do some log message customization
|
180 | return customized_message;
|
181 | }
|
182 | }
|
183 | ```
|
184 |
|
185 | <a id="license"><a>
|
186 | ## License
|
187 |
|
188 | MIT License
|