Logging

Logging is disabled by default, and you can enable it by passing { logger: true } or { logger: { level: 'info' } } when you create the fastify instance. Note that if the logger is disabled, it is impossible to enable it at runtime. We use abstract-logging for this purpose.

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:

  1. const fastify = require('fastify')({
  2. logger: true
  3. })
  4. fastify.get('/', options, function (request, reply) {
  5. request.log.info('Some info about the current request')
  6. reply.send({ hello: 'world' })
  7. })

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 Pino documentation. If you want to specify a file destination, use:

  1. const fastify = require('fastify')({
  2. logger: {
  3. level: 'info',
  4. file: '/path/to/file' // will use pino.destination()
  5. }
  6. })
  7. fastify.get('/', options, function (request, reply) {
  8. request.log.info('Some info about the current request')
  9. reply.send({ hello: 'world' })
  10. })

If you want to pass a custom stream to the Pino instance, just add the stream field to the logger object.

  1. const split = require('split2')
  2. const stream = split(JSON.parse)
  3. const fastify = require('fastify')({
  4. logger: {
  5. level: 'info',
  6. stream: stream
  7. }
  8. })

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 genReqId for customization options.

The 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.

  1. const fastify = require('fastify')({
  2. logger: {
  3. serializers: {
  4. req: function (req) {
  5. return { url: req.url }
  6. }
  7. }
  8. }
  9. })

For example, the response payload and headers could be logged using the approach below (even if it is not recommended):

  1. const fastify = require('fastify')({
  2. logger: {
  3. prettyPrint: true,
  4. serializers: {
  5. res(res) {
  6. // the default
  7. return {
  8. statusCode: res.statusCode
  9. }
  10. },
  11. req(req) {
  12. return {
  13. method: req.method,
  14. url: req.url,
  15. path: req.path,
  16. parameters: req.parameters,
  17. // Including the headers in the log could be in violation
  18. // of privacy laws, e.g. GDPR. You should use the "redact" option to
  19. // remove sensitive fields. It could also leak authentication data in
  20. // the logs.
  21. headers: req.headers
  22. };
  23. }
  24. }
  25. }
  26. });

Note: The body not can serialize inside req method, because the request is serialized when we create the child logger. At that time, the body is not parsed yet.

See a approach to log req.body

  1. app.addHook('preHandler', function (req, reply, done) {
  2. if (req.body) {
  3. req.log.info({ body: req.body }, 'parsed body')
  4. }
  5. done()
  6. })

This option will be ignored by any logger other than Pino.

You can also supply your own logger instance. Instead of passing configuration options, simply 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, trace, child.

Example:

  1. const log = require('pino')({ level: 'info' })
  2. const fastify = require('fastify')({ logger: log })
  3. log.info('does not have request information')
  4. fastify.get('/', function (request, reply) {
  5. request.log.info('includes request information, but is the same logger instance as `log`')
  6. reply.send({ hello: 'world' })
  7. })

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:

  1. const fastify = Fastify({
  2. logger: {
  3. stream: stream,
  4. redact: ['req.headers.authorization'],
  5. level: 'info',
  6. serializers: {
  7. req (req) {
  8. return {
  9. method: req.method,
  10. url: req.url,
  11. headers: req.headers,
  12. hostname: req.hostname,
  13. remoteAddress: req.ip,
  14. remotePort: req.connection.remotePort
  15. }
  16. }
  17. }
  18. }
  19. })

See https://getpino.io/#/docs/redaction for more details.