11[ ![ downloads] ( https://img.shields.io/npm/dt/log-process-errors.svg?logo=npm )] ( https://www.npmjs.com/package/log-process-errors ) [ ![ last commit] ( https://img.shields.io/github/last-commit/autoserver-org/log-process-errors.svg?logo=github )] ( https://github.com/autoserver-org/log-process-errors/graphs/contributors ) [ ![ license] ( https://img.shields.io/github/license/autoserver-org/log-process-errors.svg?logo=github )] ( https://www.apache.org/licenses/LICENSE-2.0 ) [ ![ npm] ( https://img.shields.io/npm/v/log-process-errors.svg?logo=npm )] ( https://www.npmjs.com/package/log-process-errors ) [ ![ node] ( https://img.shields.io/node/v/log-process-errors.svg?logo=node.js )] ( # ) [ ![ JavaScript Style Guide] ( https://img.shields.io/badge/code_style-standard-brightgreen.svg?logo=javascript )] ( https://standardjs.com ) [ ![ eslint-config-standard-prettier-fp] ( https://img.shields.io/badge/eslint-config--standard--prettier--fp-green.svg?logo=eslint )] ( https://github.com/autoserver-org/eslint-config-standard-prettier-fp )
22
3- Log all process errors on the console (or using your own logger):
3+ Log all process errors on the console (or using a custom logger):
44
55- [ ` uncaughtException ` ] ( https://nodejs.org/api/process.html#process_event_uncaughtexception ) : an exception was thrown and not caught
66- [ ` unhandledRejection ` ] ( https://nodejs.org/api/process.html#process_event_unhandledrejection ) : a promise was rejected and not handled
77- [ ` rejectionHandled ` ] ( https://nodejs.org/api/process.html#process_event_rejectionhandled ) : a promise was rejected and handled too late
88- [ ` multipleResolves ` ] ( https://nodejs.org/api/process.html#process_event_multipleresolves ) : a promise was resolved/rejected multiple times
99- [ ` warning ` ] ( https://nodejs.org/api/process.html#process_event_warning ) : a warning was produced using [ ` process.emitWarning() ` ] ( https://nodejs.org/api/process.html#process_process_emitwarning_warning_options )
1010
11- The message will include nice and detailed information about the error.
12-
1311![ Screenshot] ( docs/screenshot.png )
1412
1513# Usage
@@ -25,48 +23,54 @@ logProcessErrors()
2523
2624# Custom logging
2725
28- By default errors will be logged to the console using ` console.error() `
29- (or ` console.warn() ` for ` warning ` ).
26+ By default events will be logged to the console (e.g. ` console.error() ` ).
3027
31- You can use the ` handle ` option to override this default behavior. For example
32- to log errors with [ Winston] ( https://github.com/winstonjs/winston ) instead of
33- the console:
28+ This default behavior can be overriden with the ` log ` option. For example
29+ to log events with [ Winston] ( https://github.com/winstonjs/winston ) instead:
3430
3531<!-- eslint-disable no-empty-function, no-unused-vars, node/no-missing-require,
3632import/no-unresolved, unicorn/filename-case, strict, no-undef -->
3733
3834``` js
3935logProcessErrors ({
40- handle (message , level ) {
36+ log (message , level , info ) {
4137 winstonLogger[level](message)
4238 },
4339})
4440```
4541
4642The function's arguments are:
4743
48- - ` message ` ` {string} ` : nice and detailed description of the error
49- - ` level ` ` {string} ` : logging level
50- - ` info ` ` {object} ` : information about the error, with the following properties:
51- - ` eventName ` ` {string} ` : can be ` uncaughtException ` , ` unhandledRejection ` ,
52- ` rejectionHandled ` , ` multipleResolves ` or ` warning `
53- - ` error ` ` {any} ` is either:
54- - value thrown by ` uncaughtException ` . Usually an ` Error ` instance, but not
55- always.
56- - ` Error ` instance emitted by ` warning ` .
57- [ ` error.name ` , ` error.code ` and ` error.detail ` ] ( https://nodejs.org/api/process.html#process_event_warning )
58- might be defined.
59- - ` promiseState ` ` {string} ` : whether promise was ` resolved ` or ` rejected ` .
60- For ` unhandledRejection ` , ` rejectionHandled ` and ` multipleResolves ` .
61- - ` promiseValue ` ` {any} ` : value resolved/rejected by the promise.
62- For ` unhandledRejection ` , ` rejectionHandled ` and ` multipleResolves ` .
44+ - ` message ` ` {string} ` : nice and detailed description of the event
45+ - ` level ` ` {string} ` : log level
46+ - ` info ` ` {object} ` :
47+ - information about the event:
48+ - ` eventName ` ` {string} ` : can be ` uncaughtException ` , ` unhandledRejection ` ,
49+ ` rejectionHandled ` , ` multipleResolves ` or ` warning `
50+ - ` error ` ` {any} ` :
51+ - either the value thrown by ` uncaughtException ` . Usually an ` Error `
52+ instance, but not always.
53+ - or the error emitted by ` warning ` .
54+ [ ` error.code ` and ` error.detail ` ] ( https://nodejs.org/api/process.html#process_event_warning )
55+ might be defined. Always an ` Error ` instance.
56+ - ` promiseState ` ` {string} ` : whether promise was ` resolved ` or ` rejected ` .
57+ - ` promiseValue ` ` {any} ` : value resolved/rejected by the promise.
58+ - ` secondPromiseState ` , ` secondPromiseValue ` : like ` promiseState ` and
59+ ` promiseValue ` but for the second time the promise was resolved/rejected.
60+ - whether the properties above are defined or not depends on the event name:
61+ - ` eventName ` : always present
62+ - ` error ` : only on ` uncaughtException ` and ` warning `
63+ - ` promiseState ` , ` promiseValue ` : only on ` unhandledRejection ` ,
64+ ` rejectionHandled ` and ` multipleResolves `
65+ - ` secondPromiseState ` , ` secondPromiseValue ` : only on ` multipleResolves `
6366
6467# Exiting on uncaught exceptions
6568
66- By default, ` uncaughtException ` will fire ` process.exit(1) ` . This is the recommended behavior according to the
69+ By default ` uncaughtException ` will fire ` process.exit(1) ` . This is the
70+ recommended behavior according to the
6771[ Node.js documentation] ( https://nodejs.org/api/process.html#process_warning_using_uncaughtexception_correctly ) .
6872
69- You can disable this by setting the ` exitOnExceptions ` option to ` false ` :
73+ This can disabled by setting the ` exitOnExceptions ` option to ` false ` :
7074
7175<!-- eslint-disable no-empty-function, no-unused-vars, node/no-missing-require,
7276import/no-unresolved, unicorn/filename-case, strict, no-undef -->
@@ -75,15 +79,56 @@ import/no-unresolved, unicorn/filename-case, strict, no-undef -->
7579logProcessErrors ({ exitOnExceptions: false })
7680```
7781
78- # Remove logging
82+ # Log level
83+
84+ By default the log level will be ` warn ` for ` warning ` events and ` error ` for
85+ the other events.
86+
87+ This can be overriden by using the ` getLevel ` option. It should be a function
88+ function using [ ` info ` as argument] ( #custom-logging ) and returning a string
89+ among ` error ` , ` warn ` , ` info ` or ` debug ` .
90+
91+ # Log message
92+
93+ A nice-looking and descriptive log message is generated by default.
94+
95+ The message will be colorized unless either:
96+
97+ - the output [ does not support colors] ( https://github.com/chalk/supports-color )
98+ - the option ` colors ` is set to ` false `
99+
100+ The message generation can be overriden by using the ` getMessage ` option. It
101+ should be a function function using [ ` info ` as argument] ( #custom-logging ) and
102+ returning a string. The ` info ` argument also has the following properties:
103+
104+ - ` level ` ` {string} `
105+ - ` colors ` ` {object} ` : [ Chalk instance] ( https://github.com/chalk/chalk#api )
106+ to colorize strings. Colors will be disabled if the ` colors ` option is
107+ false.
108+
109+ # Filtering
110+
111+ Some events can be ignored by using the ` filter ` option. It should be a
112+ function using [ ` info ` as argument] ( #custom-logging ) and returning a boolean
113+ (` false ` to ignore the event).
114+
115+ For example to ignore ` warning ` events:
116+
117+ <!-- eslint-disable no-empty-function, no-unused-vars, node/no-missing-require,
118+ import/no-unresolved, unicorn/filename-case, strict, no-undef -->
119+
120+ ``` js
121+ logProcessErrors ({ filter : ({ eventName }) => eventName !== ' warning' })
122+ ```
123+
124+ # Stop logging
79125
80- You can undo everything by firing the function returned by
81- ` logProcessErrors() `
126+ Logging can be stopped by firing the function returned by ` logProcessErrors() `
82127
83128<!-- eslint-disable no-empty-function, no-unused-vars, node/no-missing-require,
84129import/no-unresolved, unicorn/filename-case, strict, no-undef -->
85130
86131``` js
87- const removeLogging = logProcessErrors ()
88- removeLogging ()
132+ const stopLogging = logProcessErrors ()
133+ stopLogging ()
89134```
0 commit comments