4

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 ?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey Georgios, any chance you could share a snippet of how you achieved this? I'm trying to do the exact same thing and it's not immediately obvious how to do so while still getting all the nestjs-pino features.
You can create a configuration like this and pass it to nestjs-pino options. gist.github.com/gkampitakis/b36819f38f8886598c20ed1af7245e3a
One comment is the Error passed to "formatError" should be "err" and not "error", though I'm not sure if this my setup vs. yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.