pinojs / pino

🌲 super fast, all natural json logger
http://getpino.io
MIT License
14.21k stars 875 forks source link

Help with sending logs to slack on graceful shutdown #1430

Open nenadfilipovic opened 2 years ago

nenadfilipovic commented 2 years ago

Can I get some guidelines how to handle this situation. I need to report errors to my Slack channel and results are very inconsistent. Startup logs works fine but when I hit ctrl+c I dont get any output when using custom transport even if I just console.log obj in code example below. For graceful shutdown I am using https://www.npmjs.com/package/http-graceful-shutdown and transport is simple

export default async function (opts) {
  return build(async function (source) {
    for await (let obj of source) {
      await sendSlackMessage with @slack/webhooks api
    }
  })
}

What I need to do to be able to catch logs before my servers exits? Thanks.

mcollina commented 2 years ago

It's very hard to help you given that we cannot run the code. Could you create an example that is just calling an http endpoint to simulate slack?

nenadfilipovic commented 2 years ago

Ok. lets try

Transport

import build from 'pino-abstract-transport';

export default async function (options) {
  return build(async (source) => {
    for await (let obj of source) {
      console.log(obj);
    }
  });
}

Logger

export const logger = pino(
  pino.transport({
    target: './slackTransport.js',
  }),
);

Server

import http from 'http';
import express from "express";
import { logger } from './logger.js';
import gracefulShutdown from 'http-graceful-shutdown'

const app = express();

const server = http.createServer(app);

server.listen(3002, () => {
    console.log('start');
})

const preShutdown = async () => {
  logger.info(`Stopping server...`);
};

const shutdown = gracefulShutdown(server, {
  preShutdown,
  forceExit: false,
  finally: () => {
    logger.info(`Server stopped!`);
  },
});

Graceful shutdown package will handle SIGINT, SIGTERM signals. Without transport pino will log after ctrl+c and with transport it will exit silently.

nenadfilipovic commented 2 years ago

@mcollina any chance to look at this?

mcollina commented 2 years ago

Unfortunately not yet, it's in the queue after Fastify v4.

things to verify, in this order:

  1. never use console.log in workers. It does not work the way you think.
  2. http-graceful-shutdown might be incompatible with pino transports and a change here or there might be needed.
nenadfilipovic commented 2 years ago

Thanks, I was banging my head so much with inconsistencies with console.log.

Fdawgs commented 1 year ago

@nenadfilipovic Did you manage to resolve this?