-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
65 lines (53 loc) · 2.27 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
const { checkLogLevel, generateConfig, generatePrefix, selectOutputStream, FakeStream } = require( './lib/utils.js' );
let consoleStamp = ( con, options = {} ) => {
if ( con.__patched ) {
con.reset();
}
const helperConsoleStream = new FakeStream();
const helperConsole = new console.Console( helperConsoleStream, helperConsoleStream );
const config = generateConfig( options );
const include = config.include.filter( m => typeof con[m] === 'function' );
const org = {};
Object.keys( con ).forEach( m => org[m] = con[m] );
con.org = org;
include.forEach( method => {
const stream = selectOutputStream( method, config );
const trg = con[method];
con[method] = new Proxy( trg, {
apply: ( target, context, args ) => {
if ( checkLogLevel( config, method ) ) {
helperConsole.log.apply( context, args );
// TODO: custom msg vs table will not work
let outputMessage = `${generatePrefix( method, config, helperConsoleStream.last_msg )} `;
if(method === 'table'){
outputMessage += '\n';
helperConsole.table.apply( context, args);
outputMessage += helperConsoleStream.last_msg;
}else if(!( config.preventDefaultMessage || /:msg\b/.test( config.format ) )){
outputMessage += `${helperConsoleStream.last_msg}`;
}
outputMessage += '\n';
stream.write( outputMessage );
}
}
} );
con.__patched = true
} );
if(!include.includes('table')) {
// Normaly table calls log to write to stream, we need to prevent prefix when table is not included
const tableConsole = new console.Console( config.stdout, config.stderr );
con.table = tableConsole.table;
}
con.reset = () => {
Object.keys( con.org ).forEach( m => {
con[m] = con.org[m];
delete con.org[m];
} );
delete con.org;
delete con.__patched;
delete con.reset;
helperConsoleStream.end();
};
};
module.exports = consoleStamp;
module.exports.default = consoleStamp;