-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
70 lines (64 loc) · 2.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* The purpose of this package.
*/
const winston = require('winston');
/**
* Answers whether the specified logger instance's transports list includes
* a Console transport.
*
* @param {Object} transports the deconstructed `transports` property of the
* specified Logger instance.
*
* @return {Boolean}
*/
function hasConsoleTransport({ transports }) {
return (
transports
&& transports.filter(transport => transport instanceof winston.transports.Console)[0]
);
}
/**
* The default test for whether execution is presently happening in a testing
* environment.
*
* Any `NODE_ENV` environment variable values that begin with `test` will
* qualify, including `test`, `tests`, and `testing`.
*
* @return {Boolean}
*/
function isTestingEnv() {
return /^test/.test(process.env.NODE_ENV);
}
/**
* Main module function.
*
* It "testifies" a logger's Console transport when the `isTesting` function
* returns `true` (thus indicating the runtime is a testing environment) by
* directing all console-bound output to stderr.
*
* @param {winston.Logger} logger Winston Logger instance
* @param {Function} [isTesting] function whose return value indicates
* whether execution is testing env.
*/
module.exports = (logger = winston, isTesting = isTestingEnv) => {
const { transports } = logger || winston;
const levels = Object.keys(logger.levels);
if (!isTesting()) {
return;
}
if (hasConsoleTransport(logger)) {
if (typeof transports
.filter(transport => transport instanceof winston.transports.Console)[0]
.stderrLevels !== 'object') {
throw Error('Console transport instance is missing `stderrLevels` property');
}
transports
.filter(transport => transport instanceof winston.transports.Console)[0]
.stderrLevels = {};
levels.forEach((level) => {
transports
.filter(transport => transport instanceof winston.transports.Console)[0]
.stderrLevels[level] = true;
});
}
};