user1274878
user1274878

Reputation: 1405

How to avoid the entire request being logged every time using pino?

I am new to nodejs and decided to use the pino logger in my application. Here is the code snippet showing how I am using it:

const expressPino = require('express-pino-logger');
const { LOGLEVEL } = require('../config.js');

// Constructs a Pino Instance
const logger = pino({
  level: LOGLEVEL || 'trace',
  prettyPrint: { levelFirst: true, colorize: true, ignore: 'pid' },
});
// Construct an express middleware using the above pino instance
const expressLogger = expressPino({ logger });

module.exports = { logger, expressLogger };

Now, every time I do req.log.debug(config['abc']), the entire request body gets logged, thus making the logs very cumbersome to read. For every such log statement, the output looks like this:

DEBUG [1610445271782] (on blserver-org1): sku=FIN01  Query String
req: {
  "id": 1,
  "method": "POST",
  "url": "/ifo_params?sku=FIN01",
  "headers": {
    "channel-name": "mychannel",
    "content-type": "application/json",
    "authorization": "Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IjVmOTcxMmEwODczMTcyMGQ2NmZkNGEyYTU5MmU0ZGZjMmI1ZGU1OTUiLCJ0eXAiOiJKV1QifQ.eyJvcmdJZCI6ImJ1eWVyMiIsImFkbWluIjp0cnVlLCJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vbWFya2V0c24tZGV2IiwiYXVkIjoibWFya2V0c24tZGV2IiwiYXV0aF90aW1lIjoxNjEwNDQ0ODU5LCJ1c2VyX2lkIjoibzBWZWl4VnJmZFJqbEdWZXlNS1p4Q052TkZSMiIsInN1YiI6Im8wVmVpeFZyZmRSamxHVmV5TUtaeENOdk5GUjIiLCJpYXQiOjE2MTA0NDQ4NTksImV4cCI6MTYxMDQ0ODQ1OSwiZW1haWwiOiJidXllcjJfYWRtaW5AbWFpbGluYXRvci5jb20iLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsImZpcmViYXNlIjp7ImlkZW50aXRpZXMiOnsiZW1haWwiOlsiYnV5ZXIyX2FkbWluQG1haWxpbmF0b3IuY29tIl19LCJzaWduX2luX3Byb3ZpZGVyIjoicGFzc3dvcmQifX0.QlHxLrxcfxUmGK_Q193RdQSIBsoHly66CZDTWdRdyfZNAn-wH13el41ILnAj3YqVWgAFJY8u4BcFgZYePCujIO2gKFsM0WUs2M-a7CsMcDaWpqrQyVsfalRoYMDsk1DJypaevuKV8O4IbmdkgCbS8HmYO-dk99LRcwwYKs_vVnNBO4bTv5FFzMLh-DXrVfVBKk23Qem7JN5lX9UU6RZ-4WAVN-pN-TR5uRuF2koWjCoa90CKNhYOzKW1zeN904f1rCeuY0a4R4faFYMNAlkxslBpVSKaXCkSBXOSCjVykp611Ay6EQBkrpqbD1zHvv6eEfaaMyOzjlH3DzDye-vfMQ",
    "user-agent": "PostmanRuntime/7.26.8",
    "accept": "*/*",
    "cache-control": "no-cache",
    "postman-token": "aab26357-78ff-4899-8509-7918ca9a65c4",
    "host": "localhost:3000",
    "accept-encoding": "gzip, deflate, br",
    "connection": "keep-alive",
    "content-length": "114"
  }
}

I tried changing the log levels but that doesn't help. Can someone please tell me how to tell pino to not log the entire request every time.

Upvotes: 4

Views: 13807

Answers (6)

William Magnabosco
William Magnabosco

Reputation: 1

If you are using multiple targets in a separate thread, you can proceed as follows:

export default pino(
  {
    level: 'debug',
    redact: {
      paths: ['req', 'res'],
      remove: true,
    },
  },
  transport,
)

Upvotes: 0

Moein Moeinnia
Moein Moeinnia

Reputation: 2341

I did the same in Nest js with this approach:

pinoHttp: {
        autoLogging: false,
        transport: {
          target: 'pino-pretty',
          options: {
            singleLine: true,
          },
        },
      },

Upvotes: 1

S.j. Sakib
S.j. Sakib

Reputation: 466

I was trying to do something similar. I wanted to log the whole request info in the automatic logs like request completed, but not when I am doing other logs in the application. All I had to do was pass the quietReqLogger: true to the pinoHttp options, it still keeps the reqId property, which can be used to track the other info if needed.

LoggerModule.forRoot({
    pinoHttp: {
        genReqId: () => randomUUID(),
        quietReqLogger: true,
    },
})

Upvotes: 3

Humam
Humam

Reputation: 31

Add them to ignore key when constructing your Pino instance.Here:

// Constructs a Pino Instance
const logger = pino({
  level: LOGLEVEL || 'trace',
  prettyPrint: {
    levelFirst: true,
    colorize: true,
    ignore: 'pid,req,res',
  },
});

Take a look: https://github.com/pinojs/pino-pretty#options

Upvotes: 3

Brian Bauman
Brian Bauman

Reputation: 1140

I was able to do it using information in this bug report on pino-http

The gist is to override the serializer for the request by including the following configuration option when initializing pino

serializers: {
  req: (req) => ({
    id: req.id,
    method: req.method,
    url: req.url
  })
}

Of course you can add or remove additional fields from the req. You can also do the same thing with the response if you use the res property.

Upvotes: 9

Shubham Negi
Shubham Negi

Reputation: 84

This is how I solved it.

export function getChildLogger(req: Request) {
const id = req.id;
const logger = pino({
    level: 'info',  // what ever log level is required here

})
return logger.child({ reqId: id })
}

Then use it like this

app.get("/", (req, res) => {    
const logger = getChildLogger(req);
logger.info("This is test log")
res.json({ status: "ok" })
});

May be not the best solution. But works :D

Upvotes: 2

Related Questions