Skip to content

Commit 1c9da90

Browse files
committed
Improve API.md
1 parent d63d998 commit 1c9da90

File tree

2 files changed

+63
-50
lines changed

2 files changed

+63
-50
lines changed

README.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,3 @@ logProcessErrors({
108108
exitOn: ['uncaughtException', 'unhandledRejection'],
109109
})
110110
```
111-
112-
<!-- eslint-enable no-empty-function -->
113-
114-
# Restoring default behavior
115-
116-
Node.js default behavior can be restored by firing the function returned by
117-
`logProcessErrors()`.
118-
119-
```js
120-
const restore = logProcessErrors(options)
121-
restore()
122-
```

docs/options.md

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
# Options
1+
# API
22

3-
Options are passed as an optional object:
3+
## logProcessErrors([options])
4+
5+
Initialize `log-process-errors`. Returns a function that can be fired to restore
6+
Node.js default behavior.
47

58
<!-- eslint-disable-next-line import/newline-after-import -->
69

710
```js
811
const logProcessErrors = require('log-process-errors')
9-
logProcessErrors(options)
12+
const restore = logProcessErrors(options)
13+
restore()
1014
```
1115

12-
## options.log `{function}`
16+
### options
17+
18+
_Type_: `object`
19+
20+
#### log
21+
22+
_Type_: `function(message, level, event)`
1323

1424
By default events will be logged to the console using `console.error()`,
1525
`console.warn()`, etc.
@@ -35,22 +45,25 @@ If logging is asynchronous, the function should return a promise (or use
3545
Duplicate events are only logged once (whether the `log` option is defined or
3646
not).
3747

38-
## options.level `{object}`
48+
#### level
3949

40-
Which log level to use. It should be an object whose:
50+
_Type_: `object`
4151

42-
- keys are the event names
43-
([`uncaughtException`](https://nodejs.org/api/process.html#process_event_uncaughtexception),
44-
[`warning`](https://nodejs.org/api/process.html#process_event_warning),
45-
[`unhandledRejection`](https://nodejs.org/api/process.html#process_event_unhandledrejection),
46-
[`rejectionHandled`](https://nodejs.org/api/process.html#process_event_rejectionhandled),
47-
[`multipleResolves`](https://nodejs.org/api/process.html#process_event_multipleresolves)
48-
or `default`)
49-
- values are the log level (`"debug"`, `"info"`, `"warn"`, `"error"`,
50-
`"silent"` or `"default"`). It can also be a function using
51-
[`event` as argument](#event) and returning one of those log levels.
52+
_Default_: `{ warning: 'warn', multipleResolves: 'info', default: 'error' }`.
53+
54+
Which log level to use.
5255

53-
Default: `{ warning: 'warn', multipleResolves: 'info', default: 'error' }`.
56+
Object keys are the event names:
57+
[`uncaughtException`](https://nodejs.org/api/process.html#process_event_uncaughtexception),
58+
[`warning`](https://nodejs.org/api/process.html#process_event_warning),
59+
[`unhandledRejection`](https://nodejs.org/api/process.html#process_event_unhandledrejection),
60+
[`rejectionHandled`](https://nodejs.org/api/process.html#process_event_rejectionhandled),
61+
[`multipleResolves`](https://nodejs.org/api/process.html#process_event_multipleresolves)
62+
or `default`.
63+
64+
Object values are the log levelL `"debug"`, `"info"`, `"warn"`, `"error"`,
65+
`"silent"` or `"default"`. It can also be a function using
66+
[`event` as argument](#event) and returning one of those log levels.
5467

5568
```js
5669
// Use `debug` log level for `multipleResolves` instead of `info`
@@ -70,40 +83,51 @@ logProcessErrors({
7083
})
7184
```
7285

73-
## options.message `{function}`
86+
#### message
87+
88+
_Type_: `function(level, event, options) => string`
7489

75-
Override the default message generation. It should be a function returning a
76-
string and whose arguments are [`level`](#optionslevel-object),
77-
[`event`](#event) and [`options`](#options).
90+
_Default_: generate a nice-looking and descriptive log message.
7891

79-
By default a nice-looking and descriptive log message is generated.
92+
Override the default message generation. Arguments are
93+
[`level`](#optionslevel-object), [`event`](#event) and [`options`](#options).
8094

81-
## options.colors `{boolean}`
95+
#### colors
8296

83-
Colorize the default [`options.message`](#optionsmessage-function). Default:
84-
`true` if the output is a terminal.
97+
_Type_: `boolean`
8598

86-
## options.exitOn `{string[]}`
99+
_Default_: `true` if the output is a terminal.
100+
101+
Colorize the default [`options.message`](#optionsmessage-function).
102+
103+
#### exitOn
104+
105+
_Type_: `string[]`
106+
107+
_Default_: `["uncaughtException"]`
87108

88109
Which events should trigger `process.exit(1)`:
89110

90-
- the default value is `["uncaughtException"]`. This is the
91-
[default behavior](https://nodejs.org/api/process.html#process_warning_using_uncaughtexception_correctly)
92-
of Node.js.
111+
- `["uncaughtException"]` is Node.js
112+
[default behavior](https://nodejs.org/api/process.html#process_warning_using_uncaughtexception_correctly).
93113
- we recommend using `["uncaughtException", "unhandledRejection"]`
94-
instead since this will be the [future default behavior of Node.js](https://nodejs.org/dist/latest-v8.x/docs/api/deprecations.html#deprecations_dep0018_unhandled_promise_rejections).
114+
instead since this will be [Node.js future default behavior](https://nodejs.org/dist/latest-v8.x/docs/api/deprecations.html#deprecations_dep0018_unhandled_promise_rejections).
95115
- to prevent any `process.exit(1)` use `[]`. Recommended if your process is
96116
long-running and does not automatically restart on exit.
97117

98118
`process.exit(1)` will only be fired after successfully logging the event.
99119

100-
# Event
120+
### event
121+
122+
_Type_: `object`
101123

102124
The [`log`](#optionslog-string), [`level`](#optionslevel-object) and
103125
[`message`](#optionsmessage-function) options all receive as argument an `event`
104126
object.
105127

106-
## event.name
128+
#### event.name
129+
130+
_Type_: `string`
107131

108132
Can be
109133
[`"uncaughtException"`](https://nodejs.org/api/process.html#process_event_uncaughtexception),
@@ -113,7 +137,9 @@ Can be
113137
or
114138
[`"warning"`](https://nodejs.org/api/process.html#process_event_warning).
115139

116-
## event.value
140+
#### event.value
141+
142+
_Type_: `any` (usually an `Error` instance but not always)
117143

118144
Value:
119145

@@ -127,15 +153,14 @@ Value:
127153
- emitted by
128154
[`warning`](https://nodejs.org/api/process.html#process_event_warning).
129155

130-
It is usually an `Error` instance but could be anything.
156+
#### event.rejected
131157

132-
## event.rejected
158+
_Type_: `boolean`
133159

134-
Boolean indicating whether the promise was initially resolved or rejected. Only
135-
defined with
160+
Whether the promise was initially resolved or rejected. Only defined with
136161
[`multipleResolves`](https://nodejs.org/api/process.html#process_event_multipleresolves).
137162

138-
## event.nextValue, event.nextRejected
163+
#### event.nextValue, event.nextRejected
139164

140165
Like [`value`](#eventvalue) and [`rejected`](#eventrejected) but for
141166
the second time the promise was resolved/rejected. Only defined with

0 commit comments

Comments
 (0)