Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ignoring certain log properties #167

Open
priyank-R opened this issue May 30, 2023 · 1 comment
Open

Add support for ignoring certain log properties #167

priyank-R opened this issue May 30, 2023 · 1 comment

Comments

@priyank-R
Copy link

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": "[email protected]"
		},
		"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": "[email protected]"
		},
		"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
Copy link
Author

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
      }
    }
  }),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant