@testomatio/reporter 1.6.13 → 2.0.1-beta-esm
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/adapter/codecept.d.ts +2 -0
- package/lib/adapter/codecept.js +295 -335
- package/lib/adapter/cucumber/current.d.ts +14 -0
- package/lib/adapter/cucumber/current.js +195 -203
- package/lib/adapter/cucumber/legacy.d.ts +0 -0
- package/lib/adapter/cucumber/legacy.js +130 -155
- package/lib/adapter/cucumber.d.ts +2 -0
- package/lib/adapter/cucumber.js +5 -16
- package/lib/adapter/cypress-plugin/index.d.ts +2 -0
- package/lib/adapter/cypress-plugin/index.js +93 -105
- package/lib/adapter/jasmine.d.ts +11 -0
- package/lib/adapter/jasmine.js +54 -53
- package/lib/adapter/jest.d.ts +13 -0
- package/lib/adapter/jest.js +97 -99
- package/lib/adapter/mocha.d.ts +2 -0
- package/lib/adapter/mocha.js +112 -140
- package/lib/adapter/playwright.d.ts +14 -0
- package/lib/adapter/playwright.js +195 -231
- package/lib/adapter/vitest.d.ts +35 -0
- package/lib/adapter/vitest.js +150 -149
- package/lib/adapter/webdriver.d.ts +24 -0
- package/lib/adapter/webdriver.js +134 -119
- package/lib/bin/cli.d.ts +2 -0
- package/lib/bin/cli.js +164 -211
- package/lib/bin/reportXml.d.ts +2 -0
- package/lib/bin/reportXml.js +49 -52
- package/lib/bin/startTest.d.ts +2 -0
- package/lib/bin/startTest.js +82 -95
- package/lib/bin/uploadArtifacts.d.ts +2 -0
- package/lib/bin/uploadArtifacts.js +55 -61
- package/lib/client.d.ts +76 -0
- package/lib/client.js +411 -465
- package/lib/config.d.ts +1 -0
- package/lib/config.js +16 -21
- package/lib/constants.d.ts +25 -0
- package/lib/constants.js +50 -44
- package/lib/data-storage.d.ts +34 -0
- package/lib/data-storage.js +206 -188
- package/lib/junit-adapter/adapter.d.ts +9 -0
- package/lib/junit-adapter/adapter.js +17 -20
- package/lib/junit-adapter/csharp.d.ts +4 -0
- package/lib/junit-adapter/csharp.js +18 -14
- package/lib/junit-adapter/index.d.ts +3 -0
- package/lib/junit-adapter/index.js +27 -25
- package/lib/junit-adapter/java.d.ts +5 -0
- package/lib/junit-adapter/java.js +41 -53
- package/lib/junit-adapter/javascript.d.ts +4 -0
- package/lib/junit-adapter/javascript.js +30 -27
- package/lib/junit-adapter/python.d.ts +5 -0
- package/lib/junit-adapter/python.js +38 -37
- package/lib/junit-adapter/ruby.d.ts +4 -0
- package/lib/junit-adapter/ruby.js +11 -8
- package/lib/output.d.ts +11 -0
- package/lib/output.js +44 -52
- package/lib/package.json +3 -0
- package/lib/pipe/bitbucket.d.ts +23 -0
- package/lib/pipe/bitbucket.js +210 -229
- package/lib/pipe/csv.d.ts +47 -0
- package/lib/pipe/csv.js +113 -126
- package/lib/pipe/debug.d.ts +29 -0
- package/lib/pipe/debug.js +104 -99
- package/lib/pipe/github.d.ts +30 -0
- package/lib/pipe/github.js +186 -213
- package/lib/pipe/gitlab.d.ts +23 -0
- package/lib/pipe/gitlab.js +166 -207
- package/lib/pipe/html.d.ts +34 -0
- package/lib/pipe/html.js +260 -319
- package/lib/pipe/index.d.ts +1 -0
- package/lib/pipe/index.js +84 -66
- package/lib/pipe/testomatio.d.ts +70 -0
- package/lib/pipe/testomatio.js +413 -462
- package/lib/reporter-functions.d.ts +34 -0
- package/lib/reporter-functions.js +28 -26
- package/lib/reporter.d.ts +232 -0
- package/lib/reporter.js +34 -29
- package/lib/services/artifacts.d.ts +33 -0
- package/lib/services/artifacts.js +55 -51
- package/lib/services/index.d.ts +9 -0
- package/lib/services/index.js +14 -12
- package/lib/services/key-values.d.ts +27 -0
- package/lib/services/key-values.js +56 -53
- package/lib/services/logger.d.ts +64 -0
- package/lib/services/logger.js +227 -245
- package/lib/template/testomatio.hbs +651 -1366
- package/lib/uploader.d.ts +60 -0
- package/lib/uploader.js +291 -360
- package/lib/utils/pipe_utils.d.ts +41 -0
- package/lib/utils/pipe_utils.js +89 -85
- package/lib/utils/utils.d.ts +45 -0
- package/lib/utils/utils.js +347 -307
- package/lib/xmlReader.d.ts +92 -0
- package/lib/xmlReader.js +490 -529
- package/package.json +57 -15
- package/src/adapter/codecept.js +375 -0
- package/src/adapter/cucumber/current.js +228 -0
- package/src/adapter/cucumber/legacy.js +158 -0
- package/src/adapter/cucumber.js +4 -0
- package/src/adapter/cypress-plugin/index.js +112 -0
- package/src/adapter/jasmine.js +60 -0
- package/src/adapter/jest.js +107 -0
- package/src/adapter/mocha.cjs +2 -0
- package/src/adapter/mocha.js +157 -0
- package/src/adapter/playwright.js +250 -0
- package/src/adapter/vitest.js +183 -0
- package/src/adapter/webdriver.js +142 -0
- package/src/bin/cli.js +280 -0
- package/src/bin/reportXml.js +74 -0
- package/src/bin/startTest.js +123 -0
- package/src/bin/uploadArtifacts.js +90 -0
- package/src/client.js +504 -0
- package/src/config.js +30 -0
- package/src/constants.js +53 -0
- package/src/data-storage.js +204 -0
- package/src/junit-adapter/adapter.js +23 -0
- package/src/junit-adapter/csharp.js +16 -0
- package/src/junit-adapter/index.js +28 -0
- package/src/junit-adapter/java.js +58 -0
- package/src/junit-adapter/javascript.js +31 -0
- package/src/junit-adapter/python.js +42 -0
- package/src/junit-adapter/ruby.js +10 -0
- package/src/output.js +57 -0
- package/src/pipe/bitbucket.js +254 -0
- package/src/pipe/csv.js +140 -0
- package/src/pipe/debug.js +104 -0
- package/src/pipe/github.js +233 -0
- package/src/pipe/gitlab.js +229 -0
- package/src/pipe/html.js +374 -0
- package/src/pipe/index.js +71 -0
- package/src/pipe/testomatio.js +503 -0
- package/src/reporter-functions.js +55 -0
- package/src/reporter.cjs_decprecated +21 -0
- package/src/reporter.js +33 -0
- package/src/services/artifacts.js +59 -0
- package/src/services/index.js +13 -0
- package/src/services/key-values.js +59 -0
- package/src/services/logger.js +316 -0
- package/src/template/emptyData.svg +23 -0
- package/src/template/testomatio.hbs +706 -0
- package/src/uploader.js +371 -0
- package/src/utils/pipe_utils.js +119 -0
- package/src/utils/utils.js +383 -0
- package/src/xmlReader.js +562 -0
package/lib/services/logger.js
CHANGED
|
@@ -1,272 +1,255 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.logger = void 0;
|
|
7
|
+
const picocolors_1 = __importDefault(require("picocolors"));
|
|
8
|
+
const debug_1 = __importDefault(require("debug"));
|
|
9
|
+
const data_storage_js_1 = require("../data-storage.js");
|
|
10
|
+
const debug = (0, debug_1.default)('@testomatio/reporter:services-logger');
|
|
5
11
|
const LOG_METHODS = ['assert', 'debug', 'error', 'info', 'log', 'trace', 'warn'];
|
|
6
12
|
const LEVELS = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
ALL: { severity: 1, color: '' },
|
|
14
|
+
VERBOSE: { severity: 3, color: 'grey' },
|
|
15
|
+
TRACE: { severity: 5, color: 'grey' },
|
|
16
|
+
DEBUG: { severity: 7, color: 'cyan' },
|
|
17
|
+
INFO: { severity: 9, color: 'black' },
|
|
18
|
+
LOG: { severity: 11, color: 'black' },
|
|
19
|
+
WARN: { severity: 13, color: 'yellow' },
|
|
20
|
+
ERROR: { severity: 15, color: 'red' },
|
|
15
21
|
};
|
|
16
|
-
|
|
17
22
|
// ! DON'T use console.log, console.warn, etc in this file, because it will lead to infinite loop
|
|
18
23
|
// use debug() instead
|
|
19
|
-
|
|
20
24
|
/**
|
|
21
25
|
* Logger allows to intercept logs from any logger (console.log, tracer, pino, etc)
|
|
22
26
|
* and save in the testomatio reporter.
|
|
23
27
|
* Supports different syntaxes to satisfy any user preferences.
|
|
24
28
|
*/
|
|
25
29
|
class Logger {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
this.#instance = new Logger();
|
|
30
|
+
// set default logger to be used in log, warn, error, etc methods
|
|
31
|
+
#originalUserLogger = { ...console };
|
|
32
|
+
#userLoggerWithOverridenMethods;
|
|
33
|
+
static #instance;
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @returns {Logger}
|
|
37
|
+
*/
|
|
38
|
+
static getInstance() {
|
|
39
|
+
if (!this.#instance) {
|
|
40
|
+
this.#instance = new Logger();
|
|
41
|
+
}
|
|
42
|
+
return this.#instance;
|
|
40
43
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
constructor() {
|
|
47
|
-
if (!dataStorage.isFileStorage || process.env.TESTOMATIO_INTERCEPT_CONSOLE_LOGS) this.intercept(console);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Allows you to define a step inside a test. Step name is attached to the report and
|
|
52
|
-
* helps to understand the test flow.
|
|
53
|
-
* @param {*} strings
|
|
54
|
-
* @param {...any} values
|
|
55
|
-
*/
|
|
56
|
-
step(strings, ...values) {
|
|
57
|
-
let logs = '';
|
|
58
|
-
for (let i = 0; i < strings.length; i++) {
|
|
59
|
-
logs += strings[i];
|
|
60
|
-
if (i < values.length) {
|
|
61
|
-
logs += values[i];
|
|
62
|
-
}
|
|
44
|
+
logLevel = process?.env?.LOG_LEVEL?.toUpperCase() || 'ALL';
|
|
45
|
+
constructor() {
|
|
46
|
+
if (!data_storage_js_1.dataStorage.isFileStorage ||
|
|
47
|
+
(process.env.TESTOMATIO_INTERCEPT_CONSOLE_LOGS && process.env.TESTOMATIO_INTERCEPT_CONSOLE_LOGS !== 'false'))
|
|
48
|
+
this.intercept(console);
|
|
63
49
|
}
|
|
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
|
-
logs
|
|
89
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Allows you to define a step inside a test. Step name is attached to the report and
|
|
52
|
+
* helps to understand the test flow.
|
|
53
|
+
* @param {*} strings
|
|
54
|
+
* @param {...any} values
|
|
55
|
+
*/
|
|
56
|
+
step(strings, ...values) {
|
|
57
|
+
let logs = '';
|
|
58
|
+
for (let i = 0; i < strings.length; i++) {
|
|
59
|
+
logs += strings[i];
|
|
60
|
+
if (i < values.length) {
|
|
61
|
+
logs += values[i];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
logs = picocolors_1.default.blue(`> ${logs}`);
|
|
65
|
+
data_storage_js_1.dataStorage.putData('log', logs);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
*
|
|
69
|
+
* @param {string} context testId or test context from test runner
|
|
70
|
+
* @returns {string[]}
|
|
71
|
+
*/
|
|
72
|
+
getLogs(context) {
|
|
73
|
+
const logs = data_storage_js_1.dataStorage.getData('log', context);
|
|
74
|
+
if (!logs)
|
|
75
|
+
return [];
|
|
76
|
+
return logs;
|
|
77
|
+
}
|
|
78
|
+
#stringifyLogs(...args) {
|
|
79
|
+
const logs = [];
|
|
80
|
+
// stringify everything except strings
|
|
81
|
+
for (const arg of args) {
|
|
82
|
+
// ignore empty strings
|
|
83
|
+
if (arg === '')
|
|
84
|
+
continue;
|
|
85
|
+
if (typeof arg === 'string') {
|
|
86
|
+
logs.push(arg);
|
|
87
|
+
}
|
|
88
|
+
else if (Array.isArray(arg)) {
|
|
89
|
+
logs.push(arg.join(' '));
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
try {
|
|
93
|
+
// eslint-disable-next-line no-unused-expressions
|
|
94
|
+
this.prettyObjects ? logs.push(JSON.stringify(arg, null, 2)) : logs.push(JSON.stringify(arg));
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
debug('Error while stringify object', e);
|
|
98
|
+
logs.push(arg);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return logs.join(' ');
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Tagget template literal. Allows to use different syntaxes:
|
|
106
|
+
* 1. Tagget template: log`text ${someVar}`
|
|
107
|
+
* 2. Standard: log(`text ${someVar}`)
|
|
108
|
+
* 3. Standard with multiple arguments: log('text', someVar)
|
|
109
|
+
*/
|
|
110
|
+
_templateLiteralLog(strings, ...args) {
|
|
111
|
+
if (Array.isArray(strings))
|
|
112
|
+
strings = strings.filter(item => item !== '').map(item => item.trim());
|
|
113
|
+
if (Array.isArray(args))
|
|
114
|
+
args = args.filter(item => item !== '');
|
|
115
|
+
let logs;
|
|
116
|
+
// this block means tagged template is used (syntax like $`text ${someVar}`)
|
|
117
|
+
if (Array.isArray(strings) && strings.length === args.length + 1) {
|
|
118
|
+
logs = strings.reduce((result, current, index) => result +
|
|
119
|
+
current +
|
|
120
|
+
// strings are splitted by args when use tagged template, thus we add arg after each string
|
|
121
|
+
// it looks like: `string1 arg1 string2 arg2 string3`
|
|
122
|
+
(args[index] !== undefined // eslint-disable-line no-nested-ternary
|
|
123
|
+
? typeof args[index] === 'string'
|
|
124
|
+
? args[index] // add arg as it is
|
|
125
|
+
: this.#stringifyLogs(args[index]) // stringify arg
|
|
126
|
+
: ''),
|
|
127
|
+
// initial accumulator value
|
|
128
|
+
'');
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
// this block means arguments syntax is used (syntax like $('text', someVar))
|
|
132
|
+
// in this case strings represents just a first argument
|
|
133
|
+
logs = this.#stringifyLogs(strings, ...args);
|
|
134
|
+
}
|
|
135
|
+
this.#originalUserLogger.log(logs);
|
|
136
|
+
data_storage_js_1.dataStorage.putData('log', logs);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* This function is a wrapper for each logging methods (log, warn, error etc) (not to repeat the same code)
|
|
140
|
+
* @param {*} argsArray
|
|
141
|
+
* @param {*} level
|
|
142
|
+
* @returns
|
|
143
|
+
*/
|
|
144
|
+
#logWrapper(argsArray, level) {
|
|
145
|
+
if (!argsArray.length)
|
|
146
|
+
return;
|
|
147
|
+
const severity = LEVELS[level].severity;
|
|
148
|
+
if (severity < LEVELS[this.logLevel]?.severity)
|
|
149
|
+
return;
|
|
150
|
+
const logs = this.#stringifyLogs(...argsArray);
|
|
151
|
+
const colorizedLogs = picocolors_1.default[LEVELS[level].color](logs);
|
|
152
|
+
// do not attach logs from testomatio reporter itself
|
|
153
|
+
if (!logs.includes('[TESTOMATIO]')) {
|
|
154
|
+
data_storage_js_1.dataStorage.putData('log', colorizedLogs);
|
|
155
|
+
}
|
|
90
156
|
try {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
157
|
+
// level.toLowerCase() represents method name (log, warn, error, etc)
|
|
158
|
+
this.#originalUserLogger[level.toLowerCase()](colorizedLogs);
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
// method could be unexisting, ignore error
|
|
96
162
|
}
|
|
97
|
-
}
|
|
98
163
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Tagget template literal. Allows to use different syntaxes:
|
|
104
|
-
* 1. Tagget template: log`text ${someVar}`
|
|
105
|
-
* 2. Standard: log(`text ${someVar}`)
|
|
106
|
-
* 3. Standard with multiple arguments: log('text', someVar)
|
|
107
|
-
*/
|
|
108
|
-
_templateLiteralLog(strings, ...args) {
|
|
109
|
-
if (Array.isArray(strings)) strings = strings.filter(item => item !== '').map(item => item.trim());
|
|
110
|
-
if (Array.isArray(args)) args = args.filter(item => item !== '');
|
|
111
|
-
|
|
112
|
-
let logs;
|
|
113
|
-
// this block means tagged template is used (syntax like $`text ${someVar}`)
|
|
114
|
-
if (Array.isArray(strings) && strings.length === args.length + 1) {
|
|
115
|
-
logs = strings.reduce(
|
|
116
|
-
(result, current, index) =>
|
|
117
|
-
result +
|
|
118
|
-
current +
|
|
119
|
-
// strings are splitted by args when use tagged template, thus we add arg after each string
|
|
120
|
-
// it looks like: `string1 arg1 string2 arg2 string3`
|
|
121
|
-
(args[index] !== undefined // eslint-disable-line no-nested-ternary
|
|
122
|
-
? typeof args[index] === 'string'
|
|
123
|
-
? args[index] // add arg as it is
|
|
124
|
-
: this.#stringifyLogs(args[index]) // stringify arg
|
|
125
|
-
: ''),
|
|
126
|
-
// initial accumulator value
|
|
127
|
-
'',
|
|
128
|
-
);
|
|
129
|
-
} else {
|
|
130
|
-
// this block means arguments syntax is used (syntax like $('text', someVar))
|
|
131
|
-
// in this case strings represents just a first argument
|
|
132
|
-
logs = this.#stringifyLogs(strings, ...args);
|
|
164
|
+
assert(...args) {
|
|
165
|
+
this.#logWrapper(args, 'ERROR');
|
|
133
166
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* This function is a wrapper for each logging methods (log, warn, error etc) (not to repeat the same code)
|
|
140
|
-
* @param {*} argsArray
|
|
141
|
-
* @param {*} level
|
|
142
|
-
* @returns
|
|
143
|
-
*/
|
|
144
|
-
#logWrapper(argsArray, level) {
|
|
145
|
-
if (!argsArray.length) return;
|
|
146
|
-
|
|
147
|
-
const severity = LEVELS[level].severity;
|
|
148
|
-
if (severity < LEVELS[this.logLevel]?.severity) return;
|
|
149
|
-
|
|
150
|
-
const logs = this.#stringifyLogs(...argsArray);
|
|
151
|
-
|
|
152
|
-
const colorizedLogs = chalk[LEVELS[level].color](logs);
|
|
153
|
-
// do not attach logs from testomatio reporter itself
|
|
154
|
-
if (!logs.includes('[TESTOMATIO]')) {
|
|
155
|
-
dataStorage.putData('log', colorizedLogs);
|
|
167
|
+
debug(...args) {
|
|
168
|
+
this.#logWrapper(args, 'DEBUG');
|
|
156
169
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
// level.toLowerCase() represents method name (log, warn, error, etc)
|
|
160
|
-
this.#originalUserLogger[level.toLowerCase()](colorizedLogs);
|
|
161
|
-
} catch (e) {
|
|
162
|
-
// method could be unexisting, ignore error
|
|
170
|
+
error(...args) {
|
|
171
|
+
this.#logWrapper(args, 'ERROR');
|
|
163
172
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
assert(...args) {
|
|
167
|
-
this.#logWrapper(args, 'ERROR');
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
debug(...args) {
|
|
171
|
-
this.#logWrapper(args, 'DEBUG');
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
error(...args) {
|
|
175
|
-
this.#logWrapper(args, 'ERROR');
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
info(...args) {
|
|
179
|
-
this.#logWrapper(args, 'INFO');
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
log(...args) {
|
|
183
|
-
this.#logWrapper(args, 'LOG');
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
trace(...args) {
|
|
187
|
-
this.#logWrapper(args, 'TRACE');
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
warn(...args) {
|
|
191
|
-
this.#logWrapper(args, 'WARN');
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Intercepts user logger messages.
|
|
196
|
-
* When call this method, Logger start to control the user logger
|
|
197
|
-
* @param {*} userLogger
|
|
198
|
-
*/
|
|
199
|
-
intercept(userLogger) {
|
|
200
|
-
// STEP 1: reset previously intercepted logger methods to original
|
|
201
|
-
if (this.#userLoggerWithOverridenMethods) {
|
|
202
|
-
for (const method of LOG_METHODS) {
|
|
203
|
-
this.#userLoggerWithOverridenMethods[method] = this.#originalUserLogger[method];
|
|
204
|
-
}
|
|
173
|
+
info(...args) {
|
|
174
|
+
this.#logWrapper(args, 'INFO');
|
|
205
175
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
this.#originalUserLogger = { ...userLogger };
|
|
209
|
-
|
|
210
|
-
const isUserLoggerConsole = userLogger.toString?.().toLowerCase() === '[object console]';
|
|
211
|
-
debug(`Intercepting ${isUserLoggerConsole ? 'console' : 'some user'} logger}`);
|
|
212
|
-
|
|
213
|
-
// override user logger (any, e.g. console) methods to intercept log messages
|
|
214
|
-
for (const method of LOG_METHODS) {
|
|
215
|
-
/*
|
|
216
|
-
its better to create method even if it does not exist in user logger;
|
|
217
|
-
on method invocation, we will store the data anyway and catch block will prevent potential errors
|
|
218
|
-
while trying to output the message to terminal
|
|
219
|
-
*/
|
|
220
|
-
// if (!this._loggerToIntercept[method]) continue;
|
|
221
|
-
userLogger[method] = (...args) => this[method](...args);
|
|
176
|
+
log(...args) {
|
|
177
|
+
this.#logWrapper(args, 'LOG');
|
|
222
178
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
179
|
+
trace(...args) {
|
|
180
|
+
this.#logWrapper(args, 'TRACE');
|
|
181
|
+
}
|
|
182
|
+
warn(...args) {
|
|
183
|
+
this.#logWrapper(args, 'WARN');
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Intercepts user logger messages.
|
|
187
|
+
* When call this method, Logger start to control the user logger
|
|
188
|
+
* @param {*} userLogger
|
|
189
|
+
*/
|
|
190
|
+
intercept(userLogger) {
|
|
191
|
+
// STEP 1: reset previously intercepted logger methods to original
|
|
192
|
+
if (this.#userLoggerWithOverridenMethods) {
|
|
193
|
+
for (const method of LOG_METHODS) {
|
|
194
|
+
this.#userLoggerWithOverridenMethods[method] = this.#originalUserLogger[method];
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// STEP 2: intercept new logger
|
|
198
|
+
this.#originalUserLogger = { ...userLogger };
|
|
199
|
+
const isUserLoggerConsole = userLogger.toString?.().toLowerCase() === '[object console]';
|
|
200
|
+
debug(`Intercepting ${isUserLoggerConsole ? 'console' : 'some user'} logger}`);
|
|
201
|
+
// override user logger (any, e.g. console) methods to intercept log messages
|
|
202
|
+
for (const method of LOG_METHODS) {
|
|
203
|
+
/*
|
|
204
|
+
its better to create method even if it does not exist in user logger;
|
|
205
|
+
on method invocation, we will store the data anyway and catch block will prevent potential errors
|
|
206
|
+
while trying to output the message to terminal
|
|
207
|
+
*/
|
|
208
|
+
// if (!this._loggerToIntercept[method]) continue;
|
|
209
|
+
userLogger[method] = (...args) => this[method](...args);
|
|
210
|
+
}
|
|
211
|
+
this.#userLoggerWithOverridenMethods = userLogger;
|
|
212
|
+
/*
|
|
213
|
+
Initial idea was to intercept any logger (tracer, pino, etc),
|
|
214
|
+
intercept message and provide output by the same logger.
|
|
215
|
+
But reality brings some problems: the same messages are intercepted multiple times
|
|
216
|
+
(because of multiple loggers are created at the same terminal process).
|
|
217
|
+
Also its difficult to understand (actually did not find the way to do it) if logger was already intercepted or not.
|
|
218
|
+
Thus, decided to intercept only console by default and provide output by default console.
|
|
219
|
+
It means, if user uses his own logger, its messages will be intercepted,
|
|
220
|
+
but the output will be always provided by console.
|
|
221
|
+
TODO: try to implement the providing output to terminal by user logger
|
|
222
|
+
*/
|
|
223
|
+
}
|
|
224
|
+
stopInterception() {
|
|
225
|
+
debug('Stop ntercepting logs');
|
|
226
|
+
// restore original user logger
|
|
227
|
+
if (this.#userLoggerWithOverridenMethods) {
|
|
228
|
+
for (const method of LOG_METHODS) {
|
|
229
|
+
this.#userLoggerWithOverridenMethods[method] = this.#originalUserLogger[method];
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Allows to configure logger. Make sure you do it before the logger usage in your code.
|
|
235
|
+
*
|
|
236
|
+
* @param {Object} [config={}] - The configuration object.
|
|
237
|
+
* @param {string} [config.logLevel] - The desired log level. Valid values are 'DEBUG', 'INFO', 'WARN', and 'ERROR'.
|
|
238
|
+
* @param {boolean} [config.prettyObjects] - Specifies whether to enable pretty printing of objects.
|
|
239
|
+
* @returns {void}
|
|
240
|
+
*/
|
|
241
|
+
configure(config = {}) {
|
|
242
|
+
if (!config)
|
|
243
|
+
return;
|
|
244
|
+
if (config.prettyObjects === false || config.prettyObjects === true)
|
|
245
|
+
this.prettyObjects = config.prettyObjects;
|
|
246
|
+
if (config.logLevel)
|
|
247
|
+
this.logLevel = config.logLevel.toUpperCase();
|
|
247
248
|
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Allows to configure logger. Make sure you do it before the logger usage in your code.
|
|
252
|
-
*
|
|
253
|
-
* @param {Object} [config={}] - The configuration object.
|
|
254
|
-
* @param {string} [config.logLevel] - The desired log level. Valid values are 'DEBUG', 'INFO', 'WARN', and 'ERROR'.
|
|
255
|
-
* @param {boolean} [config.prettyObjects] - Specifies whether to enable pretty printing of objects.
|
|
256
|
-
* @returns {void}
|
|
257
|
-
*/
|
|
258
|
-
configure(config = {}) {
|
|
259
|
-
if (!config) return;
|
|
260
|
-
if (config.prettyObjects === false || config.prettyObjects === true) this.prettyObjects = config.prettyObjects;
|
|
261
|
-
if (config.logLevel) this.logLevel = config.logLevel.toUpperCase();
|
|
262
|
-
}
|
|
263
249
|
}
|
|
264
|
-
|
|
265
|
-
module.exports.logger = Logger.getInstance();
|
|
266
|
-
|
|
250
|
+
exports.logger = Logger.getInstance();
|
|
267
251
|
// TODO: parse passed arguments as {level: 'str', message: 'str'} because some loggers use such syntax;
|
|
268
252
|
// upd: did not face such loggers, but still could be useful
|
|
269
|
-
|
|
270
253
|
/* Cypress
|
|
271
254
|
There is no listener like "after:test" in cypress, only "after:spec" is available.
|
|
272
255
|
Thus, cannot separate logs even when I gather them (because I don't know when the test is done, just know about suite).
|
|
@@ -306,6 +289,5 @@ Finally, in the test it will look like:
|
|
|
306
289
|
|
|
307
290
|
Parallelization in Cypress is only available if using Cypress Dashboard??
|
|
308
291
|
*/
|
|
309
|
-
|
|
310
292
|
// TODO: add time to logs
|
|
311
293
|
// TODO: add logger name to logs?
|