pinojs/pino

serializer that returns string directly instead of object?

Ouverte

#1 846 ouverte le 4 nov. 2023

 (3 commentaires) (0 réaction) (0 personne assignée)JavaScript (839 forks)batch import
enhancementhelp wanted

Métriques du dépôt

Stars
 (13 157 étoiles)
Métriques de merge PR
 (Métriques PR en attente)

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

Guide contributeur