UNPKG

3.06 kBMarkdownView Raw
1# Pretty Printing
2
3By default, Pino log lines are newline delimited JSON (NDJSON). This is perfect
4for production usage and long term storage. It's not so great for development
5environments. Thus, Pino logs can be prettified by using a Pino prettifier
6module like [`pino-pretty`][pp]:
7
8```sh
9$ cat app.log | pino-pretty
10```
11
12For almost all situations, this is the recommended way to prettify logs. The
13programmatic API, described in the next section, is primarily for integration
14purposes with other CLI based prettifiers.
15
16## Prettifier API
17
18Pino prettifier modules are extra modules that provide a CLI for parsing NDJSON
19log lines piped via `stdin` and expose an API which conforms to the Pino
20[metadata streams](api.md#metadata) API.
21
22The API requires modules provide a factory function which returns a prettifier
23function. This prettifier function must accept either a string of NDJSON or
24a Pino log object. A psuedo-example of such a prettifier is:
25
26The uninitialized Pino instance is passed as `this` into prettifier factory function,
27so it can be accessed via closure by the returned prettifier function.
28
29```js
30module.exports = function myPrettifier (options) {
31 // `this` is bound to the pino instance
32 // Deal with whatever options are supplied.
33 return function prettifier (inputData) {
34 let logObject
35 if (typeof inputData === 'string') {
36 const parsedData = someJsonParser(inputData)
37 logObject = (isPinoLog(parsedData)) ? parsedData : undefined
38 } else if (isObject(inputData) && isPinoLog(inputData)) {
39 logObject = inputData
40 }
41 if (!logObject) return inputData
42 // implement prettification
43 }
44
45 function isObject (input) {
46 return Object.prototype.toString.apply(input) === '[object Object]'
47 }
48
49 function isPinoLog (log) {
50 return log && (log.hasOwnProperty('v') && log.v === 1)
51 }
52}
53```
54
55The reference implementation of such a module is the [`pino-pretty`][pp] module.
56To learn more about creating a custom prettifier module, refer to the
57`pino-pretty` source code.
58
59Note: if the prettifier returns `undefined`, instead of a formatted line, nothing
60will be written to the destination stream.
61
62### API Example
63
64> #### NOTE:
65> For general usage, it is highly recommended that logs are piped into
66> the prettifier instead. Prettified logs are not easily parsed and cannot
67> be easily investigated at a later date.
68
691. Install a prettifier module as a separate dependency, e.g. `npm install pino-pretty`.
701. Instantiate the logger with pretty printing enabled:
71 ```js
72 const pino = require('pino')
73 const log = pino({
74 prettyPrint: {
75 levelFirst: true
76 },
77 prettifier: require('pino-pretty')
78 })
79 ```
80 Note: the default prettifier module is `pino-pretty`, so the preceding
81 example could be:
82 ```js
83 const pino = require('pino')
84 const log = pino({
85 prettyPrint: {
86 levelFirst: true
87 }
88 })
89 ```
90 See the [`pino-pretty` documentation][pp] for more information on the options
91 that can be passed via `prettyPrint`.
92
93 [pp]: https://github.com/pinojs/pino-pretty
94
\No newline at end of file