node.jsnestjselkpinojselastic-common-schema

HTTP Pino logger and Elastic Common Schema (ecs) format in NestJS


I am trying to apply @elastic/ecs-pino-format to nestjs-pino. Under the good nestjs-pino is using http-pino. I have noticed that http-pino adds the request object inside [Symbol(pino.chindings)] and I am assuming it's using a child logger.So I tried to write a custom formatter for extracting the req by calling obj.res.log.bindings() and putting at http.request to be compliant with Elastic Common Schema (ecs). The problem I face is now my log contains duplicate the req and the http.request and can't find a way to remove it. Not sure if I am looking in the wrong direction but I have found a lot of issues trying to make nestjs-pino print Elastic Common Schema (ecs) format logs. Also I have noticed issues where @elastic/ecs-pino-format can't handle fastify. Has anyone had similar issues ?


Solution

  • So I was confused with pino and pino-http and how they were integrated in nestjs-pino. From the looks of it @elastic/ecs-pino-format doesn't handle all the specifics for Elastic Common Schema (ecs) in pino-http. So what I ended up doing was passing in nestjs-pino as a configuration a merge of the format object from @elastic/ecs-pino-format and configuring pino-http.

    pino-http supports passing

    customAttributeKeys: {
    req: 'http.request',
    res: 'http.response',
    }
    

    for changing the keys of req and res. As for the data that req and res contain, http-pino has another option for transforming them

    serializers: {
    req: (log) => {... return transform },
    res: (log) => {... return transform },
    } 
    

    For more information about pino HTTP options.

    Hope this helps anyone facing the same problem or want nestjs with Elastic Common Schema (ecs) logs.

    An example configuration passed to http-pino for having ECS format.