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

serializer that returns string directly instead of object? #1846

Open
ronag opened this issue Nov 4, 2023 · 1 comment
Open

serializer that returns string directly instead of object? #1846

ronag opened this issue Nov 4, 2023 · 1 comment

Comments

@ronag
Copy link

ronag commented Nov 4, 2023

Currently, when providing a custom serializer we need to build an object that is later stringified by pino. This is a bit inefficient in certain cases. It would be nice if a serializer could somehow return a string which then pino uses directly as if it got an object that it then stringifies.

Consider the following example when using undici:

class Handler {
   onHeaders(statusCode, rawHeaders: Array<string>, resume) {
     logger.debug({ statusCode, headers: rawHeaders })
   }
}

slowSerializers = {
  headers: headers => {
      const ret = {}
      for (let n = 0; n < headers.length; n += 2) {
        ret[headers[n + 0].toString()] = headers[n + 1].toString()
      }
      return ret
  }
}

fastSerializers =  {
  headers: headers => {
    const ret = '{'
    for (let n = 0; n < headers.length; n += 2) {
      ret += n > 0 ? ',' : ''
      ret += `"${headers[n + 0]}": "${headers[n + 1]}"`
    }
    return ret + '}'
  }
}
@mcollina
Copy link
Member

This would be an amazing feature to have. A PR would be highly welcomed.

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

No branches or pull requests

2 participants