How to send logs to logstash from nestjs application using nestjs-pino?

382 Views Asked by At

I have the following configuration code in my AppModule in nestjs:

LoggerModule.forRoot({
      pinoHttp: {
        base: undefined,
        autoLogging: false,
        genReqId: () => uuidv4(),
        messageKey: 'message',
        level: 'info',
        formatters: {
          level: (label: string) => ({ level: label.toUpperCase() })
        },
        customProps: (req: IncomingMessage) => {
          return {
            request_uuid: req.id,
            request_method: req.method,
            request_uri: req.url
          };
        },
        serializers: {
          req: () => {
            return undefined;
          },
          res: () => {
            return undefined;
          }
        }
      },
      renameContext: 'logger_name'
    }),

I want to send these logs to logstash and have them eventually in elastic search and query through kibana. I am running elk stack in docker. When I log messages in the application, I want to send them to logstash?

I tried pino-socket.

1

There are 1 best solutions below

0
Sardor Taylakov On

Found a way to do it with pino-socket like this:

transport: {
              target: 'pino-socket',
              options: {
                address: 'localhost', // pass host dynamically with app properties
                port: 5044, // pass port dynamically with app properties
                mode: 'tcp',
                reconnect: true,
                recovery: true
              }
            }