By default Node.js prints
on the console which is very useful. Unfortunately those process errors:
- show neither stack traces nor promise values for
multipleResolvesmaking it hard to debug.
- are inconvenient to log to an external service.
- are printed each time an error is repeated (except for
- are not human-friendly.
log-process-errors fixes those issues:
npm install -D log-process-errors
This should only be installed as a production dependency for production code (e.g. a web server), not for libraries. This is because logging is modified globally and libraries users might not expect this side-effect. Also this might lead to conflicts between libraries.
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 =
logProcessErrors() should be called as early as possible in the code.
options is an optional object with the following properties:
By default events will be logged to the console using
This behavior can be overridden with the
log option. For example to log events
with Winston instead:
If logging is asynchronous, the function should return a promise (or use
await). This is not necessary if logging is using streams (like
Duplicate events are only logged once (whether the
log option is defined or
This can be overridden by using the
level option. It should be an
- keys are the event names among
- values are the log level which can be:
- a string among
undefinedto use the default value.
- a function using
infoas argument and returning a string or
- a string among
A nice-looking and descriptive log message is generated by default.
The message will be colorized unless the option
colors is set to
The message generation can be overridden by using the
message option. It
should be a function using
info as argument and
returning a string.
- thrown by
- resolved/rejected by the promise with
- emitted by
It is usually an
Error instance but could be anything.
Boolean indicating whether the promise was initially resolved or rejected. Only
exitOn option specifies which event should trigger
- the default value is
["uncaughtException"]. This is the default behavior of Node.js.
- we recommend using
["uncaughtException", "unhandledRejection"]instead since this will be the future default behavior of Node.js.
- to prevent any
. Recommended if your process is long-running and does not automatically restart on exit.
process.exit(1) will only be fired after successfully logging the event.
Logging can be stopped by firing the function returned by
const logProcessErrors =const stopLogging =