UNPKG

4.88 kBMarkdownView Raw
1<h1 align="center">Fastify</h1>
2
3## Logging
4
5Logging is disabled by default, and you can enable it by passing
6`{ logger: true }` or `{ logger: { level: 'info' } }` when you create
7the fastify instance. Note that if the logger is disabled, it is impossible to
8enable it at runtime. We use
9[abstract-logging](https://www.npmjs.com/package/abstract-logging) for
10this purpose.
11
12Since Fastify is really focused on performances, it uses [pino](https://github.com/pinojs/pino) as its logger, with the default log level, when enabled, set to `'info'`.
13
14Enabling the logger is extremely easy:
15
16```js
17const fastify = require('fastify')({
18 logger: true
19})
20
21fastify.get('/', options, function (request, reply) {
22 request.log.info('Some info about the current request')
23 reply.send({ hello: 'world' })
24})
25```
26
27If you want to pass some options to the logger, just pass the logger option to Fastify.
28You can find all the options in the [Pino documentation](https://github.com/pinojs/pino/blob/master/docs/api.md#pinooptions-stream). If you want to specify a file destination, use:
29
30```js
31const fastify = require('fastify')({
32 logger: {
33 level: 'info',
34 file: '/path/to/file' // will use pino.destination()
35 }
36})
37
38fastify.get('/', options, function (request, reply) {
39 request.log.info('Some info about the current request')
40 reply.send({ hello: 'world' })
41})
42```
43
44If you want to pass a custom stream to the Pino instance, just add the stream field to the logger object.
45
46```js
47const split = require('split2')
48const stream = split(JSON.parse)
49
50const fastify = require('fastify')({
51 logger: {
52 level: 'info',
53 stream: stream
54 }
55})
56```
57
58<a name="logging-request-id" />
59
60By default fastify adds an id to every request for easier tracking. If the "request-id" header is present its value is used, otherwise a new incremental id is generated. See Fastify Factory [`requestIdHeader`](https://github.com/fastify/fastify/blob/master/docs/Server.md#factory-request-id-header) and Fastify Factory [`genReqId`](https://github.com/fastify/fastify/blob/master/docs/Server.md#gen-request-id) for customization options.
61
62The default logger is configured with a set of standard serializers that serialize objects with `req`, `res`, and `err` properties. This behavior can be customized by specifying custom serializers.
63```js
64const fastify = require('fastify')({
65 logger: {
66 serializers: {
67 req: function (req) {
68 return { url: req.url }
69 }
70 }
71 }
72})
73```
74For example, the response payload and headers could be logged using the approach below (even if it is *not* recommended*):
75
76```js
77const fastify = require('fastify')({
78 logger: {
79 prettyPrint: true,
80 serializers: {
81 res(res) {
82 // the default
83 return {
84 statusCode: res.statusCode
85 }
86 },
87 req(req) {
88 return {
89 method: req.method,
90 url: req.url,
91 path: req.path,
92 parameters: req.parameters,
93 // Including the body and headers in the log could be in violation
94 // of privacy laws, e.g. GDPR. You should use the "redact" option to
95 // remove sensitive fields. It could also leak authentication data in
96 // the logs.
97 body: req.body,
98 headers: req.headers
99 };
100 }
101 }
102 }
103});
104```
105
106*This option will be ignored by any logger other than Pino.*
107
108You can also supply your own logger instance. Instead of passing configuration options, simply pass the instance.
109The logger you supply must conform to the Pino interface; that is, it must have the following methods:
110`info`, `error`, `debug`, `fatal`, `warn`, `trace`, `child`.
111
112Example:
113
114```js
115const log = require('pino')({ level: 'info' })
116const fastify = require('fastify')({ logger: log })
117
118log.info('does not have request information')
119
120fastify.get('/', function (request, reply) {
121 request.log.info('includes request information, but is the same logger instance as `log`')
122 reply.send({ hello: 'world' })
123})
124```
125
126*The logger instance for the current request is available in every part of the [lifecycle](https://github.com/fastify/fastify/blob/master/docs/Lifecycle.md).*
127
128## Log Redaction
129
130[Pino](https://getpino.io) supports low-overhead log redaction for
131obscuring values of specific properties in recorded logs.
132As an example, we might want to log all the HTTP headers minus the
133`Authorization` header for security concerns:
134
135```js
136const fastify = Fastify({
137 logger: {
138 stream: stream,
139 redact: ['req.headers.authorization'],
140 level: 'info',
141 serializers: {
142 req (req) {
143 return {
144 method: req.method,
145 url: req.url,
146 headers: req.headers,
147 hostname: req.hostname,
148 remoteAddress: req.ip,
149 remotePort: req.connection.remotePort
150 }
151 }
152 }
153 }
154})
155```
156
157See https://getpino.io/#/docs/redaction for more details.