strong-tie/inbound-calls
0
1# pino-std-serializers [](https://github.com/pinojs/pino-std-serializers/actions?query=workflow%3ACI)2 3This module provides a set of standard object serializers for the4[Pino](https://getpino.io) logger.5 6## Serializers7 8### `exports.err(error)`9Serializes an `Error` like object. Returns an object:10 11```js12{13 type: 'string', // The name of the object's constructor.14 message: 'string', // The supplied error message.15 stack: 'string', // The stack when the error was generated.16 raw: Error // Non-enumerable, i.e. will not be in the output, original17 // Error object. This is available for subsequent serializers18 // to use.19 [...any additional Enumerable property the original Error had]20}21```22 23Any other extra properties, e.g. `statusCode`, that have been attached to the24object will also be present on the serialized object.25 26If the error object has a [`cause`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) property, the `cause`'s `message` and `stack` will be appended to the top-level `message` and `stack`. All other parameters that belong to the `error.cause` object will be omitted.27 28Example:29 30```js31const serializer = require('pino-std-serializers').err;32 33const innerError = new Error("inner error");34innerError.isInner = true;35const outerError = new Error("outer error", { cause: innerError });36outerError.isInner = false;37 38const serialized = serializer(outerError);39/* Result:40{41 "type": "Error",42 "message": "outer error: inner error",43 "isInner": false,44 "stack": "Error: outer error45 at <...omitted..>46 caused by: Error: inner error47 at <...omitted..>48}49 */50```51 52### `exports.errWithCause(error)`53Serializes an `Error` like object, including any `error.cause`. Returns an object:54 55```js56{57 type: 'string', // The name of the object's constructor.58 message: 'string', // The supplied error message.59 stack: 'string', // The stack when the error was generated.60 cause?: Error, // If the original error had an error.cause, it will be serialized here61 raw: Error // Non-enumerable, i.e. will not be in the output, original62 // Error object. This is available for subsequent serializers63 // to use.64 [...any additional Enumerable property the original Error had]65}66```67 68Any other extra properties, e.g. `statusCode`, that have been attached to the object will also be present on the serialized object.69 70Example:71```javascript72const serializer = require('pino-std-serializers').errWithCause;73 74const innerError = new Error("inner error");75innerError.isInner = true;76const outerError = new Error("outer error", { cause: innerError });77outerError.isInner = false;78 79const serialized = serializer(outerError);80/* Result:81{82 "type": "Error",83 "message": "outer error",84 "isInner": false,85 "stack": "Error: outer error86 at <...omitted..>",87 "cause": {88 "type": "Error",89 "message": "inner error",90 "isInner": true,91 "stack": "Error: inner error92 at <...omitted..>"93 },94}95 */96```97 98### `exports.mapHttpResponse(response)`99Used internally by Pino for general response logging. Returns an object:100 101```js102{103 res: {}104}105```106 107Where `res` is the `response` as serialized by the standard response serializer.108 109### `exports.mapHttpRequest(request)`110Used internall by Pino for general request logging. Returns an object:111 112```js113{114 req: {}115}116```117 118Where `req` is the `request` as serialized by the standard request serializer.119 120### `exports.req(request)`121The default `request` serializer. Returns an object:122 123```js124{125 id: 'string', // Defaults to `undefined`, unless there is an `id` property126 // already attached to the `request` object or to the `request.info`127 // object. Attach a synchronous function128 // to the `request.id` that returns an identifier to have129 // the value filled.130 method: 'string',131 url: 'string', // the request pathname (as per req.url in core HTTP)132 query: 'object', // the request query (as per req.query in express or hapi)133 params: 'object', // the request params (as per req.params in express or hapi)134 headers: Object, // a reference to the `headers` object from the request135 // (as per req.headers in core HTTP)136 remoteAddress: 'string',137 remotePort: Number,138 raw: Object // Non-enumerable, i.e. will not be in the output, original139 // request object. This is available for subsequent serializers140 // to use. In cases where the `request` input already has141 // a `raw` property this will replace the original `request.raw`142 // property143}144```145 146### `exports.res(response)`147The default `response` serializer. Returns an object:148 149```js150{151 statusCode: Number, // Response status code, will be null before headers are flushed152 headers: Object, // The headers to be sent in the response.153 raw: Object // Non-enumerable, i.e. will not be in the output, original154 // response object. This is available for subsequent serializers155 // to use.156}157```158 159### `exports.wrapErrorSerializer(customSerializer)`160A utility method for wrapping the default error serializer. This allows161custom serializers to work with the already serialized object.162 163The `customSerializer` accepts one parameter — the newly serialized error164object — and returns the new (or updated) error object.165 166### `exports.wrapRequestSerializer(customSerializer)`167A utility method for wrapping the default request serializer. This allows168custom serializers to work with the already serialized object.169 170The `customSerializer` accepts one parameter — the newly serialized request171object — and returns the new (or updated) request object.172 173### `exports.wrapResponseSerializer(customSerializer)`174A utility method for wrapping the default response serializer. This allows175custom serializers to work with the already serialized object.176 177The `customSerializer` accepts one parameter — the newly serialized response178object — and returns the new (or updated) response object.179 180## License181 182MIT License183 