ftmocks-utils 1.3.1 → 1.3.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ftmocks-utils",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "Util functions for FtMocks",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -13,6 +13,9 @@ const {
13
13
  getTestByName,
14
14
  } = require("./common-utils");
15
15
  const { FtJSON } = require("./json-utils");
16
+ const Logger = require("./log-utils");
17
+
18
+ let logger = null;
16
19
 
17
20
  const getDefaultMockDataFromConfig = (testConfig) => {
18
21
  const defaultPath = path.join(
@@ -320,13 +323,14 @@ async function initiatePlaywrightRoutes(
320
323
  mockPath = "**/*",
321
324
  excludeMockPath = null
322
325
  ) {
326
+ logger = new Logger({disableLogs: ftmocksConifg.DISABLE_LOGS}, ftmocksConifg, testName);
323
327
  const testMockData = testName
324
328
  ? loadMockDataFromConfig(ftmocksConifg, testName)
325
329
  : [];
326
330
  resetAllMockStats({ testMockData, testConfig: ftmocksConifg, testName });
327
331
  const test = await getTestByName(ftmocksConifg, testName);
328
332
  const defaultMockData = getDefaultMockDataFromConfig(ftmocksConifg);
329
- console.debug("\x1b[32mcalling initiatePlaywrightRoutes fetch\x1b[0m");
333
+ logger.debug("\x1b[32mcalling initiatePlaywrightRoutes fetch\x1b[0m");
330
334
  let firstUrl = null;
331
335
  await page.route(mockPath, async (route, request) => {
332
336
  const url = request.url();
@@ -413,7 +417,7 @@ async function initiatePlaywrightRoutes(
413
417
  fallbackDir,
414
418
  ftmocksConifg.FALLBACK_DIR_INDEX_FILE_FOR_STATUS_404 || "index.html"
415
419
  );
416
- console.debug(
420
+ logger.debug(
417
421
  "\x1b[32mserving file for status 404\x1b[0m",
418
422
  filePath,
419
423
  url
@@ -476,19 +480,19 @@ async function initiatePlaywrightRoutes(
476
480
  ".php": "application/x-httpd-php",
477
481
  }[ext] || "application/octet-stream";
478
482
 
479
- console.debug("\x1b[32mserving file\x1b[0m", filePath);
483
+ logger.info("\x1b[32mserving file\x1b[0m", filePath);
480
484
  await route.fulfill({
481
485
  body: fileContent,
482
486
  headers: { "Content-Type": contentType },
483
487
  });
484
488
  } else {
485
- console.debug("\x1b[31mmissing mock data, falling back\x1b[0m", url);
489
+ logger.debug("\x1b[31mmissing mock data, falling back\x1b[0m", url);
486
490
  await route.fallback();
487
491
  }
488
492
  }
489
493
  } catch (e) {
490
- console.error(e);
491
- console.error(
494
+ logger.error(e);
495
+ logger.error(
492
496
  "\x1b[31merror at initiatePlaywrightRoutes\x1b[0m",
493
497
  url,
494
498
  options
@@ -0,0 +1,90 @@
1
+ const path = require("path");
2
+ const fs = require("fs");
3
+ const { getMockDir, nameToFolder } = require("./common-utils");
4
+
5
+ class Logger {
6
+ constructor(options = {}, ftmocksConifg, testName) {
7
+ this.levels = ["error", "warn", "info", "debug"];
8
+ this.level = options.level || "info";
9
+ this.disableLogs = options.disableLogs || false;
10
+ this.logsFile = path.join(
11
+ getMockDir(ftmocksConifg),
12
+ `test_${nameToFolder(testName)}`,
13
+ "_logs.json"
14
+ );
15
+ this.logs = [];
16
+ }
17
+
18
+ setLevel(level) {
19
+ if (this.levels.includes(level)) {
20
+ this.level = level;
21
+ }
22
+ }
23
+
24
+ writeToFile(type, params) {
25
+ try {
26
+ const logMessage = params.join(" ") + "\n"; // Combine params into a string with spaces
27
+ this.logs.push({
28
+ type,
29
+ message: logMessage,
30
+ time: Date.now(),
31
+ source: "ftmocks-utils",
32
+ });
33
+ fs.writeFileSync(this.logsFile, JSON.stringify(this.logs, null, 2), "utf8"); // Append the log message to the file
34
+ } catch (error) {
35
+ // Ignore error
36
+ }
37
+ }
38
+
39
+ log(level, ...args) {
40
+ if (this.disableLogs) return;
41
+ const levelIdx = this.levels.indexOf(level);
42
+ const currentLevelIdx = this.levels.indexOf(this.level);
43
+ if (levelIdx <= currentLevelIdx) {
44
+ const color = this._getColor(level);
45
+ const prefix = `[${level.toUpperCase()}]`;
46
+ if (typeof args[0] === "string") {
47
+ // Color only the prefix
48
+ console.log(`${color}${prefix}\x1b[0m`, ...args);
49
+ } else {
50
+ // Non-string first arg, just print
51
+ console.log(`${color}${prefix}\x1b[0m`, ...args);
52
+ }
53
+ }
54
+ this.writeToFile(level, args);
55
+ }
56
+
57
+ error(...args) {
58
+ this.log("error", ...args);
59
+ }
60
+
61
+ warn(...args) {
62
+ this.log("warn", ...args);
63
+ }
64
+
65
+ info(...args) {
66
+ this.log("info", ...args);
67
+ }
68
+
69
+ debug(...args) {
70
+ this.log("debug", ...args);
71
+ console.debug(...args);
72
+ }
73
+
74
+ _getColor(level) {
75
+ switch (level) {
76
+ case "error":
77
+ return "\x1b[31m"; // Red
78
+ case "warn":
79
+ return "\x1b[33m"; // Yellow
80
+ case "info":
81
+ return "\x1b[36m"; // Cyan
82
+ case "debug":
83
+ return "\x1b[90m"; // Gray
84
+ default:
85
+ return "";
86
+ }
87
+ }
88
+ }
89
+
90
+ module.exports = Logger;