Skip to content

Commit f4a19f5

Browse files
committed
Refactor opts.level
1 parent 6e1dec5 commit f4a19f5

File tree

11 files changed

+43
-50
lines changed

11 files changed

+43
-50
lines changed

docs/options.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,32 @@ not).
4040

4141
Which log level to use. It should be an object whose:
4242

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

5654
Default: `{ warning: 'warn', multipleResolves: 'info', default: 'error' }`.
5755

5856
```js
57+
// Use `debug` log level for `multipleResolves` instead of `info`
5958
logProcessErrors({
60-
// Use `debug` log level for `multipleResolves` instead of `info`
6159
level: { multipleResolves: 'debug' },
6260
})
6361
```
6462

6563
```js
64+
// Skip some logs based on a condition
6665
logProcessErrors({
67-
// Skip some logs based on a condition
6866
level: {
6967
default() {
70-
if (shouldSkip()) {
71-
return 'silent'
72-
}
68+
return shouldSkip() ? 'silent' : 'default'
7369
},
7470
},
7571
})

src/constants.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ LEVELS:
99
- info
1010
- warn
1111
- error
12-
ALL_LEVELS:
13-
- debug
14-
- info
15-
- warn
16-
- error
1712
- silent
13+
- default
1814
DEFAULT_LEVEL:
1915
default: error
2016
uncaughtException: error

src/level.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,27 @@ const { emitWarning } = require('process')
55
const { multipleValidOptions } = require('jest-validate')
66

77
const { result, mapValues, pickBy } = require('./utils')
8-
const { DEFAULT_LEVEL, ALL_LEVELS } = require('./constants')
8+
const { DEFAULT_LEVEL, LEVELS } = require('./constants')
99

1010
// Retrieve event's log level
1111
const getLevel = function({ opts, info, info: { eventName } }) {
1212
const level = result(opts.level[eventName], info)
1313

14-
if (ALL_LEVELS.includes(level)) {
15-
return level
14+
if (level === 'default') {
15+
return DEFAULT_LEVEL[eventName]
1616
}
1717

18-
validateLevel({ level, eventName })
19-
20-
return DEFAULT_LEVEL[eventName]
21-
}
22-
23-
const validateLevel = function({ level, eventName }) {
24-
if (level === undefined) {
25-
return
18+
if (LEVELS.includes(level)) {
19+
return level
2620
}
2721

2822
emitWarning(
29-
`Invalid option 'level.${eventName}' returning '${level}': function must return undefined or one of ${ALL_LEVELS.join(
23+
`Invalid option 'level.${eventName}' returning '${level}': function must return one of ${LEVELS.join(
3024
', ',
3125
)}`,
3226
)
27+
28+
return DEFAULT_LEVEL[eventName]
3329
}
3430

3531
// Apply `opts.level.default` and default values to `opts.level`
@@ -43,7 +39,7 @@ const applyDefaultLevels = function({
4339
}
4440

4541
const defaultLevels = mapValues(DEFAULT_LEVEL, () => defaultLevel)
46-
return { ...DEFAULT_LEVEL, ...defaultLevels, ...levelA }
42+
return { ...defaultLevels, ...levelA }
4743
}
4844

4945
// Use during options validation

src/validate.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { EVENTS, ALL_LEVELS } = require('./constants')
3+
const { EVENTS, LEVELS } = require('./constants')
44

55
// Validation beyond what `jest-validate` can do
66
const validateOptions = function({ exitOn, level = {} }) {
@@ -18,17 +18,15 @@ const validateLevel = function([eventName, level]) {
1818
}
1919

2020
throw new Error(
21-
`Invalid option 'level.${eventName}' '${level}': must be a function, undefined or one of ${ALL_LEVELS.join(
21+
`Invalid option 'level.${eventName}' '${level}': must be a function or one of ${LEVELS.join(
2222
', ',
2323
)}`,
2424
)
2525
}
2626

2727
const isValidLevel = function({ level }) {
2828
return (
29-
ALL_LEVELS.includes(level) ||
30-
level === undefined ||
31-
typeof level === 'function'
29+
LEVELS.includes(level) || level === undefined || typeof level === 'function'
3230
)
3331
}
3432

test/helpers/init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const getLevel = function({ level, eventName }) {
5959
return level
6060
}
6161

62-
const levelA = level === undefined ? { default: undefined } : level
62+
const levelA = level === undefined ? { default: 'default' } : level
6363

6464
return mapValues(levelA, levelB => onlyEvent.bind(null, levelB, eventName))
6565
}

test/helpers/repeat.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ const getEvent = function([eventName, emitEvent]) {
1717
return { eventName, emitEvent, name: eventName, defaultLevel }
1818
}
1919

20+
const isNormalLevel = function(level) {
21+
return level !== 'silent' && level !== 'default'
22+
}
23+
24+
const NORMAL_LEVELS = LEVELS.filter(isNormalLevel)
25+
2026
const repeatEvents = repeat.bind(null, getEvents())
21-
const repeatLevels = repeat.bind(null, LEVELS)
22-
const repeatEventsLevels = repeat.bind(null, getEvents(), LEVELS)
27+
const repeatLevels = repeat.bind(null, NORMAL_LEVELS)
28+
const repeatEventsLevels = repeat.bind(null, getEvents(), NORMAL_LEVELS)
2329

2430
module.exports = {
2531
repeatEvents,

test/level.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ repeatEvents((prefix, { eventName, emitEvent, defaultLevel }) => {
1717
const OPTIONS = [
1818
{},
1919
{ level: { default: undefined }, exitOn: [] },
20-
{ level: { default: () => undefined } },
20+
{ level: { default: 'default' }, exitOn: [] },
21+
{ level: { default: () => 'default' } },
2122
]
2223
OPTIONS.forEach(options => {
2324
test(`${prefix} ${JSON.stringify(

test/snapshots/level.js.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,33 @@ Generated by [AVA](https://ava.li).
88

99
> Snapshot 1
1010
11-
`[] warning Error: Invalid option 'level.multipleResolves' returning 'invalid': function must return undefined or one of debug, info, warn, error, silent␊
11+
`[] warning Error: Invalid option 'level.multipleResolves' returning 'invalid': function must return one of debug, info, warn, error, silent, default
1212
at STACK TRACE`
1313

1414
## [rejectionHandled] should emit a warning when opts.level() uses an invalid level
1515

1616
> Snapshot 1
1717
18-
`[] warning Error: Invalid option 'level.rejectionHandled' returning 'invalid': function must return undefined or one of debug, info, warn, error, silent␊
18+
`[] warning Error: Invalid option 'level.rejectionHandled' returning 'invalid': function must return one of debug, info, warn, error, silent, default
1919
at STACK TRACE`
2020

2121
## [uncaughtException] should emit a warning when opts.level() uses an invalid level
2222

2323
> Snapshot 1
2424
25-
`[] warning Error: Invalid option 'level.uncaughtException' returning 'invalid': function must return undefined or one of debug, info, warn, error, silent␊
25+
`[] warning Error: Invalid option 'level.uncaughtException' returning 'invalid': function must return one of debug, info, warn, error, silent, default
2626
at STACK TRACE`
2727

2828
## [unhandledRejection] should emit a warning when opts.level() uses an invalid level
2929

3030
> Snapshot 1
3131
32-
`[] warning Error: Invalid option 'level.unhandledRejection' returning 'invalid': function must return undefined or one of debug, info, warn, error, silent␊
32+
`[] warning Error: Invalid option 'level.unhandledRejection' returning 'invalid': function must return one of debug, info, warn, error, silent, default
3333
at STACK TRACE`
3434

3535
## [warning] should emit a warning when opts.level() uses an invalid level
3636

3737
> Snapshot 1
3838
39-
`[] warning Error: Invalid option 'level.warning' returning 'invalid': function must return undefined or one of debug, info, warn, error, silent␊
39+
`[] warning Error: Invalid option 'level.warning' returning 'invalid': function must return one of debug, info, warn, error, silent, default
4040
at STACK TRACE`

test/snapshots/level.js.snap

-5 Bytes
Binary file not shown.

test/snapshots/options.js.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Generated by [AVA](https://ava.li).
144144

145145
> Snapshot 1
146146
147-
'Invalid option \'level.warning\' \'invalid\': must be a function, undefined or one of debug, info, warn, error, silent'
147+
'Invalid option \'level.warning\' \'invalid\': must be a function or one of debug, info, warn, error, silent, default'
148148

149149
## {"name":"level","value":{"warning":true}} should validate options
150150

0 commit comments

Comments
 (0)