forked from nightwatchjs/nightwatch-plugin-browserstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogPatcher.js
More file actions
112 lines (94 loc) · 2.96 KB
/
Copy pathlogPatcher.js
File metadata and controls
112 lines (94 loc) · 2.96 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
103
104
105
106
107
108
109
110
111
112
const Transport = require('winston-transport');
const {consoleHolder, PID_MAPPING_REGEX, IPC_SERVER_NAME, EVENTS} = require('./constants');
const CrashReporter = require('./crashReporter');
const TestObservability = require('../testObservability');
const helper = require('./helper');
const eventHelper = require('./eventHelper');
const logSequence = require('./logSequence');
const testObservability = new TestObservability();
let testLogs = [];
let _uuid = '';
const LOG_LEVELS = {
INFO: 'INFO',
ERROR: 'ERROR',
DEBUG: 'DEBUG',
TRACE: 'TRACE',
WARN: 'WARN'
};
class LogPatcher extends Transport {
constructor(opts) {
super(opts);
}
flushAllLogs = () => {
if (testLogs.length === 0) {return}
testLogs.forEach((logObj) => {
if (logObj.eventType === EVENTS.LOG) {testObservability.appendTestItemLog(logObj.loggingData, _uuid)}
});
testLogs = [];
};
logToTestOps = (level = LOG_LEVELS.INFO, message = ['']) => {
try {
let eventType = EVENTS.LOG;
if (typeof message[0] === 'string' && message[0].match(PID_MAPPING_REGEX)) {
eventType = EVENTS.LOG_INIT;
} else {
consoleHolder[level.toLowerCase()](...message);
}
if (!helper.shouldSendLogs()) {return}
const pid = process.pid;
const loggingData = {
timestamp: new Date().toISOString(),
sequence: logSequence.next(),
level: level.toUpperCase(),
message: `"${message.join(', ')}"`,
kind: 'TEST_LOG',
http_response: {}
};
if (_uuid !== '') {
testObservability.appendTestItemLog(loggingData, _uuid);
this.flushAllLogs();
} else {
testLogs.push({eventType, loggingData});
}
// for non parallel execution
eventHelper.emitLogEvent(eventType, loggingData);
// for parallel execution
if (process.send && eventType === EVENTS.LOG_INIT){
process.send({eventType: eventType, loggingData: loggingData, pid: pid});
}
process.on('message', (data) => {
if (data.uuid !== undefined){
_uuid = data.uuid;
process.env.TEST_RUN_UUID = _uuid;
}
});
process.on('disconnect', async () => {
this.flushAllLogs();
await helper.uploadPending();
await helper.shutDownRequestHandler();
});
} catch (error) {
consoleHolder.log('Error in patching logs', error);
}
};
/* Patching this would show user an extended trace on their cli */
trace = (...message) => {
this.logToTestOps(LOG_LEVELS.TRACE, message);
};
debug = (...message) => {
this.logToTestOps(LOG_LEVELS.DEBUG, message);
};
info = (...message) => {
this.logToTestOps(LOG_LEVELS.INFO, message);
};
warn = (...message) => {
this.logToTestOps(LOG_LEVELS.WARN, message);
};
error = (...message) => {
this.logToTestOps(LOG_LEVELS.ERROR, message);
};
log = (...message) => {
this.logToTestOps(LOG_LEVELS.INFO, message);
};
};
module.exports = LogPatcher;