fastify / fastify

Fast and low overhead web framework, for Node.js
https://www.fastify.dev
Other
32.18k stars 2.28k forks source link

Support pino logger customAttributeKeys to replace existing req res reqId log record fields #4413

Open p-fedyukovich opened 1 year ago

p-fedyukovich commented 1 year ago

Prerequisites

🚀 Feature Proposal

It would be good to support customAttributeKeys as pino-http does. Currently there is no elegant way to change for example req key to httpRequest

Motivation

Google cloud log entry has own logging structure, including httpRequest. By adding custom attribute key req: 'httpRequest' and own custom request serializer it would be possible easily configure logger for google cloud log structure.

Example

Example for google cloud:

private readonly server: FastifyInstance = fastify({
        trustProxy: true,
        ignoreTrailingSlash: true,
        logger: {
            customAttributeKeys: {
                req: 'httpRequest',
                reqId: 'logging.googleapis.com/trace',
            },
            serializers: {
                req: function (req): any {
                    return {
                        requestMethod: req.method,
                        requestUrl: req.url,
                        userAgent: req.headers['user-agent'],
                        remoteIp: req.headers['x-forwarded-for']
                    }
                }
            }
       }
})
mcollina commented 1 year ago

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

p-fedyukovich commented 1 year ago

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

Thanks for your reply. Yea, sure

minhtriet359 commented 1 week ago

I'm working on this.

climba03003 commented 1 week ago

Shouldn't it be live inside pino instead of here? If it add too much overhead into pino, then it should be a custom transportor?