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:

    1. 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:

    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. })

    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.

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

    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:

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

    The logger instance for the current request is available in every part of the lifecycle.

    Log Redaction

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

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