axiomhq/next-axiom

[Question] Adding custom fields to request logs

b0o opened this issue · 2 comments

b0o commented

Is it possible to add a custom field to all request logs?

We are running behind Cloudflare and would like to include the value of cf-connecting-ip in our logs.

We could do this in our middleware by manually logging the value, but I'm wondering if there's a way to get the IP included in the logs that are automatically output by Next/Vercel on request.

hi @b0o , you can create a child of a logger with the args you want, and the args will be attached to every log record. for example:

func handler(req) {
  const log = req.log.with({cloudflareIP: 'someValue'})
  
  log.info('something')
  ...
}

export default withAxiom(handler)

the output of this will be something like:

{'message': 'something', 'fields': {'cloudflareIP': 'someValue'}}
bahlo commented

@b0o Another option would be to wrap withAxiom in a custom function that adds these fields to the logger. Please re-open if your issue remains unsolved 😌