Skip to content

serializeErrorFunction

Grant Carthew edited this page Oct 25, 2024 · 10 revisions

perj serializeErrorFunction Option

The serializeErrorFunction option enables you supply a custom Error serializer function.

Type: Function

Default: Internal Error serializer function.

Valid: function (value) { return // Plain JavaScript Object }

Description:

One of the choices made within the perj logger is to be very conservative when it comes to logging Error objects. You never know what additional data has been added to the error object. It is also possible the package throwing an error is using a custom error object.

The internal error serializer function is very thorough. If an Error object is logged the default serializer will:

  • Crawl up the prototype chain to the base object looking for any extra data to log.
  • Crawls through extra Objects found attached to the Error object.
  • Checks for circular references.

Because of this, logging an Error object is slow compared to other processes within perj. This performance hit should not be an issue on a typical application because exceptions should not happen often or in great frequency.

The serializeErrorFunction option enables you to change this function to any other that you like. If you believe the approach taken by perj is a little extreme or slow, feel free to supply your own function.

Example

This example changes the serializeErrorFunction option to a very simple function:

import Perj from 'perj'

const log = new Perj({ serializeErrorFunction })
const err = new Error('Example error')
log.error(err) 

function serializeErrorFunction (value) {
  const { name, message, stack } = value
  return { name, message, stack }
}

/*

Standard out will be:

{"level":"error","lvl":50,"time":1528267122234,"msg":"Example error","data":{"name":"Error","message":"Example error","stack":"Error: Example error\n    at repl:1:11\n    at Script.runInThisContext (vm.js:91:20)\n    at REPLServer.defaultEval (repl.js:329:29)\n    at bound (domain.js:396:14)\n    at REPLServer.runBound [as eval] (domain.js:409:12)\n    at REPLServer.onLine (repl.js:625:10)\n    at REPLServer.emit (events.js:187:15)\n    at REPLServer.EventEmitter.emit (domain.js:442:20)\n    at REPLServer.Interface._onLine (readline.js:290:10)\n    at REPLServer.Interface._line (readline.js:638:8)"}}


*/

Here is the above output in human readable format:

{
  "level": "error",
  "lvl": 50,
  "time": 1528267122234,
  "msg": "Example error",
  "data": {
    "name": "Error",
    "message": "Example error",
    "stack": "Error: Example error\n    at repl:1:11\n    at Script.runInThisContext (vm.js:91:20)\n    at REPLServer.defaultEval (repl.js:329:29)\n    at bound (domain.js:396:14)\n    at REPLServer.runBound [as eval] (domain.js:409:12)\n    at REPLServer.onLine (repl.js:625:10)\n    at REPLServer.emit (events.js:187:15)\n    at REPLServer.EventEmitter.emit (domain.js:442:20)\n    at REPLServer.Interface._onLine (readline.js:290:10)\n    at REPLServer.Interface._line (readline.js:638:8)"
  },
  "error": true
}

As an comparison here is the same error using the default Error serializer:

{"level":"error","lvl":50,"time":1528267516154,"msg":"Example error","data":{"constructor":"Error","name":"Error","stack":"Error: Example error\n    at repl:1:11\n    at Script.runInThisContext (vm.js:91:20)\n    at REPLServer.defaultEval (repl.js:329:29)\n    at bound (domain.js:396:14)\n    at REPLServer.runBound [as eval] (domain.js:409:12)\n    at REPLServer.onLine (repl.js:625:10)\n    at REPLServer.emit (events.js:187:15)\n    at REPLServer.EventEmitter.emit (domain.js:442:20)\n    at REPLServer.Interface._onLine (readline.js:290:10)\n    at REPLServer.Interface._line (readline.js:638:8)","message":"Example error","Error":{"constructor":"Error","name":"Error","message":"","Object":{"constructor":"Object","name":""}}},"error":true}

Again, in human readable format:

{
  "level": "error",
  "lvl": 50,
  "time": 1528267516154,
  "msg": "Example error",
  "data": {
    "constructor": "Error",
    "name": "Error",
    "stack": "Error: Example error\n    at repl:1:11\n    at Script.runInThisContext (vm.js:91:20)\n    at REPLServer.defaultEval (repl.js:329:29)\n    at bound (domain.js:396:14)\n    at REPLServer.runBound [as eval] (domain.js:409:12)\n    at REPLServer.onLine (repl.js:625:10)\n    at REPLServer.emit (events.js:187:15)\n    at REPLServer.EventEmitter.emit (domain.js:442:20)\n    at REPLServer.Interface._onLine (readline.js:290:10)\n    at REPLServer.Interface._line (readline.js:638:8)",
    "message": "Example error",
    "Error": {
      "constructor": "Error",
      "name": "Error",
      "message": "",
      "Object": {
        "constructor": "Object",
        "name": ""
      }
    }
  },
  "error": true
}
Clone this wiki locally