Skip to content

serializer that returns string directly instead of object? #1846

@ronag

Description

@ronag

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions