JSPM

  • Created
  • Published
  • Downloads 142293
  • Score
    100M100P100Q165548F
  • License Apache-2.0

Log all process errors: uncaught exceptions, warnings and improperly handled promises

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

downloads last commit license npm node JavaScript Style Guide eslint-config-standard-prettier-fp

Log all process errors on the console (or using a custom logger):

Screenshot

Usage

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

logProcessErrors(options)

Options

options is an optional object with the following properties:

Custom logging

By default events will be logged to the console (e.g. console.error()).

This behavior can be overriden with the log option. For example to log events with Winston instead:

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

The function's arguments are:

  • message {string}: nice and detailed description of the event. Can be customized with the getMessage option.
  • level {string}: log level. Can be customized with the getLevel option.
  • info {object}:
    • information about the event
    • has the following properties:
      • eventName {string}: can be uncaughtException, unhandledRejection, rejectionHandled, multipleResolves or warning
      • error {any}:
        • either the value thrown by uncaughtException
        • or the error emitted by warning. error.code and error.detail might be defined.
        • it is usually an Error instance but could technically be anything
      • promiseState {string}: whether the promise was resolved or rejected
      • promiseValue {any}: value resolved/rejected by the promise
      • secondPromiseState, secondPromiseValue: like promiseState and promiseValue but for the second time the promise was resolved/rejected
    • whether the properties above are defined or not depends on the event name:
      • eventName: always present
      • error: only on uncaughtException and warning
      • promiseState, promiseValue: only on unhandledRejection, rejectionHandled and multipleResolves
      • secondPromiseState, secondPromiseValue: only on multipleResolves

Exiting on uncaught exceptions

By default uncaughtException will fire process.exit(1). This is the recommended behavior according to the Node.js documentation.

This can disabled by setting the exitOnExceptions option to false:

logProcessErrors({ exitOnExceptions: false })

Log level

By default the log level will be warn for warning events and error for the other events.

This can be overriden by using the getLevel option. It should be a function function using info as argument and returning a string among error, warn, info or debug.

logProcessErrors({
  getLevel({ eventName }) {
    return eventName === 'uncaughtException' ? 'error' : 'warn'
  },
})

Log message

A nice-looking and descriptive log message is generated by default.

The message will be colorized unless either:

The message generation can be overriden by using the getMessage option. It should be a function function using info as argument and returning a string. The info argument also has the following properties:

  • level {string}
  • colors {object}: Chalk instance to colorize strings. Colors will be disabled if the colors option is false.

Skipping events

Events can be skipped with the skipEvent option. It should be a function using info as argument and returning true or false.

For example to skip warning events:

logProcessErrors({ skipEvent: ({ eventName }) => eventName === 'warning' })

Stop logging

Logging can be stopped by firing the function returned by logProcessErrors()

const stopLogging = logProcessErrors()
stopLogging()