hemerajs / fastify-graceful-shutdown

Gracefully shutdown fastify
MIT License
70 stars 13 forks source link

fastify.log not functional in app.gracefulShutdown() #21

Closed aguegu closed 2 years ago

aguegu commented 2 years ago

With simple demo like below.

const app = Fastify({ logger: true });

app.register(FastifyGracefulShutdown).after(() => {
  app.gracefulShutdown((code, cb) => { 
    console.log(code); // this line works
    app.log.info(code); // this line not working
    cb();
  });
});

In logs I could find line like

{"level":30,"time": (time),"pid":(pid),"hostname":(hostname),"plugin":"fastify-graceful-shutdown","signal":"SIGINT","msg":"triggering close hook"}

But in the graceful shutdown handler, it could not log into app.log. I think it would make more sense to log some shutdown process within it.

StarpTech commented 2 years ago

Hi, I tried your example with fastify v4 and it worked. I exited the process via SIGINT.

'use strict'

const fastify = require('fastify')({
  logger: {
    level: 'info',
  },
})

fastify.register(require('./')).after((err) => {
  fastify.log.error(err)
  // Register custom clean up handler
  fastify.gracefulShutdown((code, cb) => {
    fastify.log.info("dddddddddddd")
    cb()
  })
})

const schema = {
  schema: {
    response: {
      200: {
        type: 'object',
        properties: {
          hello: {
            type: 'string',
          },
        },
      },
    },
  },
}

fastify.get('/', schema, function (req, reply) {
  reply.send({ hello: 'world' })
})

fastify.listen(3000, (err) => {
  if (err) throw err
  console.log(`server listening on ${fastify.server.address().port}`)
})
^C{"level":30,"time":1655750151076,"pid":72447,"hostname":"pop-os","plugin":"fastify-graceful-shutdown","signal":"SIGINT","msg":"received signal"}
{"level":30,"time":1655750151076,"pid":72447,"hostname":"pop-os","plugin":"fastify-graceful-shutdown","signal":"SIGINT","msg":"triggering close hook"}
{"level":30,"time":1655750151076,"pid":72447,"hostname":"pop-os","msg":"dddddddddddd"}
{"level":30,"time":1655750151077,"pid":72447,"hostname":"pop-os","plugin":"fastify-graceful-shutdown","signal":"SIGINT","msg":"process terminated"}
aguegu commented 2 years ago

I'm glad in works in fastify@4.

When I created this issue, it is around fastify@3.28.0.