JSPM

  • Created
  • Published
  • Downloads 179808
  • Score
    100M100P100Q171078F
  • License Apache-2.0

Show some ❤️ to process errors

Package Exports

  • log-process-errors

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (log-process-errors) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Codecov Travis Node Gitter

Show some ❤️ to process errors in Node.js.

By default Node.js prints uncaughtException, warning and mishandled promises (unhandledRejection, rejectionHandled, multipleResolves) on the console which is very useful. Unfortunately those process errors:

log-process-errors fixes all those issues.

Without log-process-errors:

Screenshot before

With log-process-errors:

Screenshot after

Install

Production code (e.g. a web server) can install this either as a production or development dependency:

npm install log-process-errors

However libraries should install this as a development dependency:

npm install -D log-process-errors

This is because logging is modified globally and libraries users might not expect this side-effect. Also this might lead to conflicts between libraries.

Usage

There are two ways to load this library. The first is to use the node -r CLI flag:

node -r log-process-errors/register ...

The second is:

const logProcessErrors = require('log-process-errors')
logProcessErrors(options)

logProcessErrors() should be called as early as possible in the code.

Options

options is an optional object with the following properties:

  • log {function}: override how events are logged. Default: use console.warn(), console.error(), etc.
  • level {object}: which log level to use. Default: { warning: 'warn', multipleResolves: 'info', default: 'error' }.
  • message {function}: override the default message generation.
  • colors {boolean}: colorize the default message. Default: true.
  • exitOn {string[]}: which events should trigger process.exit(1). Default: ['uncaughtException'].

Please see the options full documentation.

Full example:

logProcessErrors({
  log(message, level, info) {
    winstonLogger[level](message)
  },

  level: { multipleResolves: 'debug' },

  message(info) {},

  colors: false,

  exitOn: ['uncaughtException', 'unhandledRejection'],
})

Restoring default behavior

Node.js default behavior can be restored by firing the function returned by logProcessErrors().

const restore = logProcessErrors(options)
restore()