pinojs / pino-elasticsearch

🌲 load pino logs into Elasticsearch
MIT License
179 stars 67 forks source link

Add support for ignoring certain log properties #167

Open priyank-R opened 1 year ago

priyank-R commented 1 year ago

Currently, when I try to integrate pino-http on my express application with pino-elasticsearch, everything is being forwarded to elasticsearch. My elasticsearch object looks like this:

    "_source": {
        "level": 30,
        "time": "2023-05-30T06:58:23.533Z",
        "pid": 21392,
        "hostname": "XYZ",
        "req": {
            "id": 11,
            "method": "GET",
            "url": "/api/v1/public/getbooks",
            "query": {},
            "params": {},
            "headers": {
                "host": "localhost:8083",
                "connection": "keep-alive",
                "sec-ch-ua": "\"Google Chrome\";v=\"113\", \"Chromium\";v=\"113\", \"Not-A.Brand\";v=\"24\"",
                "accept": "application/json, text/plain, */*",
                "sec-ch-ua-mobile": "?0",
                "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
                "sec-ch-ua-platform": "\"Windows\"",
                "origin": "http://localhost:4200",
                "sec-fetch-site": "same-site",
                "sec-fetch-mode": "cors",
                "sec-fetch-dest": "empty",
                "referer": "http://localhost:4200/",
                "accept-encoding": "gzip, deflate, br",
                "accept-language": "en-US,en;q=0.9",
                "if-none-match": "W/\"4bb-xKV3xzrbE2tNS3d+ukFNe4yK9mM\""
            },
            "remoteAddress": "::1",
            "remotePort": 59737
        },
        "user": {
            "_id": -1,
            "name": "Demo User",
            "email": "priyank.rupareliya@developer.com"
        },
        "res": {
            "statusCode": 304,
            "headers": {
                "x-powered-by": "Express",
                "access-control-allow-origin": "*",
                "etag": "W/\"4bb-xKV3xzrbE2tNS3d+ukFNe4yK9mM\""
            }
        },
        "responseTime": 1,
        "msg": "request completed"
    }

However, I just want to forward some of the properties, and ignore the rest. I want the object to look like this:

    "_source": {
        "level": 30,
        "time": "2023-05-30T06:58:23.533Z",
        "hostname": "XYZ",
        "req": {
            "id": 11,
            "method": "GET",
            "url": "/api/v1/public/getbooks",
            "query": {},
            "params": {},
            "remoteAddress": "::1",
            "remotePort": 59737
        },
        "user": {
            "_id": -1,
            "name": "Demo User",
            "email": "priyank.rupareliya@developer.com"
        },
        "res": {
            "statusCode": 304,
        },
        "responseTime": 1,
        "msg": "request completed"
    }

Currently, no option available to achieve this in pino-elasticsearch. We need something available to ignore option available in pino-pretty.

priyank-R commented 1 year ago

As a solution for now, I'm using the serializers options available in pino-http to manually delete req.headers and res.headers from the log object.

  logger({
    logger: customPinoLogger,
    autoLogging: true,
    customProps: (req, res) => {
      return {
        user: req.user
          ? { _id: req.user?._id, name: req.user?.name, email: req.user?.email }
          : undefined,
      };
    },
    serializers: {
      req (req) {
          delete req['headers']
          return req
      },
      res (res){
        delete res?.['headers']
        return res
      }
    }
  }),