-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjest.logger.js
More file actions
102 lines (90 loc) · 2.74 KB
/
Copy pathjest.logger.js
File metadata and controls
102 lines (90 loc) · 2.74 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const moment = require('moment');
const {inspect} = require('util');
class JestLogger {
/**
* @param {{dateFormat:string=,logLevel:string=,format:raw=}=} options
*/
constructor(options) {
this.options = Object.assign({}, {
dateFormat: 'DD/MMM/YYYY:HH:mm:ss Z',
logLevel: 'info',
format: 'raw'
}, options);
if (options == null) {
// validate NODE_ENV environment variable
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
this.options.logLevel = 'debug';
}
}
this.level = JestLogger.Levels.info;
if (Object.prototype.hasOwnProperty.call(JestLogger.Levels), this.options.logLevel) {
this.level = JestLogger.Levels[this.options.logLevel];
}
}
static get Levels() {
return {
error: 0,
warn: 1,
info: 2,
verbose: 3,
debug: 4
};
}
log() {
if (this.level < JestLogger.Levels.info) {
return;
}
this.write.apply(this, ['log'].concat(Array.from(arguments)));
}
info() {
if (this.level < JestLogger.Levels.info) {
return;
}
this.write.apply(this, ['info'].concat(Array.from(arguments)));
}
warn() {
if (this.level < JestLogger.Levels.warn) {
return;
}
this.write.apply(this, ['warn'].concat(Array.from(arguments)));
}
error() {
if (this.level < JestLogger.Levels.error) {
return;
}
this.write.apply(this, ['error'].concat(Array.from(arguments)));
}
verbose() {
if (this.level < JestLogger.Levels.verbose) {
return;
}
this.write.apply(this, ['verbose'].concat(Array.from(arguments)));
}
debug() {
if (this.level < JestLogger.Levels.debug) {
return;
}
this.write.apply(this, ['debug'].concat(Array.from(arguments)));
}
/**
* @param {string} level
* @param {...*} arg
*/
// eslint-disable-next-line no-unused-vars
write(level, arg) {
const args = Array.from(arguments);
const log = (level === 'error') ? process.stderr : process.stdout
if (args.length > 1) {
if (args[args.length - 1] == null) {
args.pop();
}
}
// add timestamp
args.unshift(moment().format(this.options.dateFormat || 'DD/MMM/YYYY:HH:mm:ss Z'));
log.write(args.map((arg) => inspect(arg)).map(
(arg) => arg.replace(/^'/, '').replace(/'$/, '')
).join(',') + '\n');
}
}
// use JestLogger as default logger
module.exports = JestLogger;