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
Log all process errors on the console (or using a custom logger):
uncaughtException
: an exception was thrown and not caughtunhandledRejection
: a promise was rejected and not handledrejectionHandled
: a promise was rejected and handled too latemultipleResolves
: a promise was resolved/rejected multiple timeswarning
: a warning was produced usingprocess.emitWarning()
Usage
const logProcessErrors = require('log-process-errors')
logProcessErrors(options)
Options
options
is an optional object with the following properties:
log
{function}
exitOnExceptions
{boolean}
(default:true
)getLevel
{function}
getMessage
{function}
colors
{boolean}
(default:false
)skipEvent
{function}
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 thegetMessage
option.level
{string}
: log level. Can be customized with thegetLevel
option.info
{object}
:- information about the event
- has the following properties:
eventName
{string}
: can beuncaughtException
,unhandledRejection
,rejectionHandled
,multipleResolves
orwarning
error
{any}
:- either the value thrown by
uncaughtException
- or the error emitted by
warning
.error.code
anderror.detail
might be defined. - it is usually an
Error
instance but could technically be anything
- either the value thrown by
promiseState
{string}
: whether the promise wasresolved
orrejected
promiseValue
{any}
: value resolved/rejected by the promisesecondPromiseState
,secondPromiseValue
: likepromiseState
andpromiseValue
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 presenterror
: only onuncaughtException
andwarning
promiseState
,promiseValue
: only onunhandledRejection
,rejectionHandled
andmultipleResolves
secondPromiseState
,secondPromiseValue
: only onmultipleResolves
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 output does not support colors
- the option
colors
is set tofalse
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 thecolors
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()