Skip to content

Commit f8071a8

Browse files
committed
feat: add output methods
1 parent 6f7f5ff commit f8071a8

File tree

4 files changed

+72
-54
lines changed

4 files changed

+72
-54
lines changed

README.md

+26-28
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,56 @@
11
# proc-log
22

3-
Emits 'log' events on the process object which a log output listener can
4-
consume and print to the terminal.
3+
Emits events on the process object which a listener can consume and print to the terminal or log file.
54

6-
This is used by various modules within the npm CLI stack in order to send
7-
log events that can be consumed by a listener on the process object.
5+
This is used by various modules within the npm CLI stack in order to send log events that can be consumed by a listener on the process object.
6+
7+
Currently emits `log` and `output` events.
88

99
## API
1010

1111
```js
12-
const { log } = require('proc-log')
12+
const { log, output } = require('proc-log')
1313
```
1414

15+
#### output
16+
* `output.standard(...args)` calls `process.emit('output', 'standard', ...args)`
17+
This is for general standard output. Consumers will typically show this on stdout (after optionally formatting or filtering it).
18+
* `output.error(...args)` calls `process.emit('output', 'error', ...args)`
19+
This is for general error output. Consumers will typically show this on stderr (after optionally formatting or filtering it).
20+
* `output.buffer(...args)` calls `process.emit('output', 'buffer', ...args)`
21+
This is for buffered output. Consumers will typically buffer this until they are ready to display.
22+
* `output.LEVELS` an array of strings of all output method names
23+
24+
#### log
1525
* `log.error(...args)` calls `process.emit('log', 'error', ...args)`
16-
The highest log level. For printing extremely serious errors that
17-
indicate something went wrong.
26+
The highest log level. For printing extremely serious errors that indicate something went wrong.
1827
* `log.warn(...args)` calls `process.emit('log', 'warn', ...args)`
19-
A fairly high log level. Things that the user needs to be aware of, but
20-
which won't necessarily cause improper functioning of the system.
28+
A fairly high log level. Things that the user needs to be aware of, but which won't necessarily cause improper functioning of the system.
2129
* `log.notice(...args)` calls `process.emit('log', 'notice', ...args)`
22-
Notices which are important, but not necessarily dangerous or a cause for
23-
excess concern.
30+
Notices which are important, but not necessarily dangerous or a cause for excess concern.
2431
* `log.info(...args)` calls `process.emit('log', 'info', ...args)`
25-
Informative messages that may benefit the user, but aren't particularly
26-
important.
32+
Informative messages that may benefit the user, but aren't particularly important.
2733
* `log.verbose(...args)` calls `process.emit('log', 'verbose', ...args)`
2834
Noisy output that is more detail that most users will care about.
2935
* `log.silly(...args)` calls `process.emit('log', 'silly', ...args)`
30-
Extremely noisy excessive logging messages that are typically only useful
31-
for debugging.
36+
Extremely noisy excessive logging messages that are typically only useful for debugging.
3237
* `log.http(...args)` calls `process.emit('log', 'http', ...args)`
3338
Information about HTTP requests made and/or completed.
3439
* `log.timing(...args)` calls `process.emit('log', 'timing', ...args)`
3540
Timing information.
36-
* `log.pause()` calls `process.emit('log', 'pause')` Used to tell
37-
the consumer to stop printing messages.
41+
* `log.pause()` calls `process.emit('log', 'pause')`
42+
Used to tell the consumer to stop printing messages.
3843
* `log.resume()` calls `process.emit('log', 'resume')`
3944
Used to tell the consumer that it is ok to print messages again.
4045
* `log.LEVELS` an array of strings of all log method names
4146

4247
## Examples
4348

44-
Every `log` method calls `process.emit('log', level, ...otherArgs)` internally.
45-
So in order to consume those events you need to do `process.on('log', fn)`.
49+
Every `log` method calls `process.emit('log', level, ...otherArgs)` internally. So in order to consume those events you need to do `process.on('log', fn)`.
4650

4751
### Colorize based on level
4852

49-
Here's an example of how to consume `proc-log` events and colorize them
50-
based on level:
53+
Here's an example of how to consume `proc-log` log events and colorize them based on level:
5154

5255
```js
5356
const chalk = require('chalk')
@@ -63,18 +66,13 @@ process.on('log', (level, ...args) => {
6366

6467
### Pause and resume
6568

66-
`pause` and `resume` are included so you have the ability to tell your consumer
67-
that you want to pause or resume your display of logs. In the npm CLI we use
68-
this to buffer all logs on init until we know the correct loglevel to display.
69-
But we also setup a second handler that writes everything to a file even if
70-
paused.
69+
`log.pause` and `log.resume` are included so you have the ability to tell your consumer that you want to pause or resume your display of logs. In the npm CLI we use this to buffer all logs on init until we know the correct loglevel to display. But we also setup a second handler that writes everything to a file even if paused.
7170

7271
```js
7372
let paused = true
7473
const buffer = []
7574

76-
// this handler will buffer and replay logs only
77-
// after `procLog.resume()` is called
75+
// this handler will buffer and replay logs only after `procLog.resume()` is called
7876
process.on('log', (level, ...args) => {
7977
if (level === 'resume') {
8078
buffer.forEach((item) => console.log(...item))

lib/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
module.exports = {
2+
output: {
3+
LEVELS: [
4+
'standard',
5+
'error',
6+
'buffer',
7+
],
8+
standard: (...args) => process.emit('output', 'standard', ...args),
9+
error: (...args) => process.emit('output', 'error', ...args),
10+
buffer: (...args) => process.emit('output', 'buffer', ...args),
11+
},
212
log: {
313
LEVELS: [
414
'notice',

tap-snapshots/test/index.js.test.cjs

+8
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ Array [
1919
"resume",
2020
]
2121
`
22+
23+
exports[`test/index.js TAP output > output levels 1`] = `
24+
Array [
25+
"standard",
26+
"error",
27+
"buffer",
28+
]
29+
`

test/index.js

+28-26
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
const t = require('tap')
2-
const { log } = require('../')
3-
const { LEVELS } = log
4-
5-
t.test('log', t => {
6-
t.matchSnapshot(LEVELS, 'log levels')
7-
t.test('all log.LEVELS have a function in log', t => {
8-
for (const level of LEVELS) {
9-
t.test(level, t => {
10-
t.match(log[level], Function)
11-
process.once('log', (actual, ...args) => {
12-
t.equal(actual, level, 'emitted log with expected level')
13-
t.same(args, [1, 'two', [3], { 4: 4 }], 'got expected args')
14-
t.end()
2+
const procLog = require('../')
3+
for (const method in procLog) {
4+
t.test(method, t => {
5+
const log = procLog[method]
6+
const { LEVELS } = log
7+
t.matchSnapshot(LEVELS, `${method} levels`)
8+
t.test(`all ${method}.LEVELS have a function in ${method}`, t => {
9+
for (const level of LEVELS) {
10+
t.test(level, t => {
11+
t.match(log[level], Function)
12+
process.once(method, (actual, ...args) => {
13+
t.equal(actual, level, `emitted ${method} with expected level`)
14+
t.same(args, [1, 'two', [3], { 4: 4 }], 'got expected args')
15+
t.end()
16+
})
17+
log[level](1, 'two', [3], { 4: 4 })
1518
})
16-
log[level](1, 'two', [3], { 4: 4 })
17-
})
18-
}
19-
t.end()
20-
})
21-
t.test('all log functions are in log.LEVELS', t => {
22-
t.plan(LEVELS.length)
23-
for (const fn in log) {
24-
if (fn !== 'LEVELS') {
25-
t.ok(LEVELS.includes(fn), `log.${fn} is in LEVELS`)
2619
}
27-
}
20+
t.end()
21+
})
22+
t.test(`all ${method} functions are in ${method}.LEVELS`, t => {
23+
t.plan(LEVELS.length)
24+
for (const fn in log) {
25+
if (fn !== 'LEVELS') {
26+
t.ok(LEVELS.includes(fn), `${method}.${fn} is in ${method}.LEVELS`)
27+
}
28+
}
29+
})
30+
t.end()
2831
})
29-
t.end()
30-
})
32+
}

0 commit comments

Comments
 (0)