capture-console
capture-console is a small Node.js library built to help when capturing log output via process.stdout
and process.stderr
. The main use case is unit testing (which is why I built it), but there's no reason it can't be used in production code paths.
Installation
capture-console lives on npm, so just install it via the command line and you're good to go.
$ npm install --save-dev capture-console
Usage
There are a whole bunch of ways to use capture-console
, mainly due to scoping, with the two easiest defined below. Depending on your use case you might be pushed more towards one than the other, but in general you can just choose your preference.
Scoped Captures
The easiest way to use capture-console
is with scoping; this is when the output of a provided function is captured.
Note that this form assumes synchronous execution - async stuff will require manual hookups (below).
var capcon = require('capture-console');
var stderr = capcon.captureStderr(function scope() {
});
var stdout = capcon.captureStdout(function scope() {
});
var stdio = capcon.captureStdio(function scope() {
});
Manual Captures
There are also ways to manually stop and start a capture context, by passing a process stream to watch and a callback to fire on each message.
var capcon = require('capture-console');
var output = '';
capcon.startCapture(process.stdout, function (stdout) {
output += stdout;
});
capcon.stopCapture(process.stdout);
Intercepting
You should be aware that all capture
functions will still pass the values through to the main stdio write()
functions, so logging will still go to your standard IO devices.
If this is not desirable, you can use the intercept
functions. These functions are literally s/capture/intercept
when compared to those shown above, and the only difference is that calls aren't forwarded through to the base implementation.