Since Fastify is really focused on performances, it uses pino as its logger, with the default log level, when enabled, set to 'info'
.
Enabling the logger is extremely easy:
If you want to pass some options to the logger, just pass the logger option to Fastify. You can find all the 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' })
})
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 properties. This behavior can be customized by specifying custom serializers.
logger: {
serializers: {
req: function (req) {
return { url: req.url }
}
}
}
})
This option will be ignored by any logger other than Pino.
Example:
The logger instance for the current request is available in every part of the lifecycle.
Log Redaction
Pino supports low-overhead log redaction for obscuring values of specific properties in recorded logs. As an example, we might want to log all the HTTP headers minus the Authorization
header for security concerns:
logger: {
stream: stream,
redact: ['req.headers.authorization'],
serializers: {
req (req) {
return {
method: req.method,
url: req.url,
headers: req.headers,
hostname: req.hostname,
remoteAddress: req.ip,
remotePort: req.connection.remotePort
}
}
}