@testomatio/reporter 2.0.1-beta.4 → 2.0.1-beta.5-timestamp

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