pinojs/pino

serializer that returns string directly instead of object?

Open

#1846 opened on Nov 4, 2023

View on GitHub
 (3 comments) (0 reactions) (0 assignees)JavaScript (13,157 stars) (839 forks)batch import
enhancementhelp wanted

Description

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 + '}'
  }
}

Contributor guide