As Fastify is focused on performance, it uses pino as its logger, with the default log level, when enabled, set to 'info'
.
Enabling the logger is extremely easy:
You can trigger new logs outside route handlers by using the Pino instance from the Fastify instance:
fastify.log.info('Something important happened!');
If you want to pass some options to the logger, just pass them to Fastify. You can find all available options in the . If you want to specify a file destination, use:
const fastify = require('fastify')({
logger: {
level: 'info',
file: '/path/to/file' // Will use pino.destination()
}
})
fastify.get('/', options, function (request, reply) {
request.log.info('Some info about the current request')
reply.send({ hello: 'world' })
})
By 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 and Fastify Factory for customization options.
The default logger is configured with a set of standard serializers that serialize objects with req
, res
, and err
properties. The object received by req
is the Fastify Request object, while the object received by res
is the Fastify object. This behaviour can be customized by specifying custom serializers.
const fastify = require('fastify')({
logger: {
serializers: {
req (request) {
return { url: request.url }
}
}
}
})
For example, the response payload and headers could be logged using the approach below (even if it is not recommended):
const fastify = require('fastify')({
logger: {
serializers: {
res (reply) {
// The default
return {
statusCode: reply.statusCode
}
},
req (request) {
return {
method: request.method,
url: request.url,
path: request.path,
parameters: request.parameters,
// Including the headers in the log could be in violation
// of privacy laws, e.g. GDPR. You should use the "redact" option to
// remove sensitive fields. It could also leak authentication data in
// the logs.
headers: request.headers
};
}
}
}
});
Note: The body cannot be serialized inside a req
method because the request is serialized when we create the child logger. At that time, the body is not yet parsed.
Any logger other than Pino will ignore this option.
You can also supply your own logger instance. Instead of passing configuration options, pass the instance. The logger you supply must conform to the Pino interface; that is, it must have the following methods: info
, error
, debug
, fatal
, warn
, , child
.
Example:
const log = require('pino')({ level: 'info' })
log.info('does not have request information')
fastify.get('/', function (request, reply) {
request.log.info('includes request information, but is the same logger instance as `log`')
reply.send({ hello: 'world' })
})
The logger instance for the current request is available in every part of the lifecycle.
Log Redaction
const fastify = Fastify({
logger: {
stream: stream,
redact: ['req.headers.authorization'],
level: 'info',
serializers: {
req (request) {
return {
method: request.method,
url: request.url,
headers: request.headers,
hostname: request.hostname,
remoteAddress: request.ip,
remotePort: request.socket.remotePort
}
}
}
})
See https://getpino.io/#/docs/redaction for more details.