@koralabs/kora-labs-common 6.4.13 → 6.4.15

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/logger/index.d.ts CHANGED
@@ -13,6 +13,14 @@ export declare class Logger {
13
13
  static network: CardanoNetwork;
14
14
  private static isInitialized;
15
15
  static initialize(): Promise<void>;
16
+ static local(args: {
17
+ message: string;
18
+ category?: LogCategory;
19
+ event?: string;
20
+ milliseconds?: number;
21
+ count?: number;
22
+ dimensions?: string[];
23
+ } | string): void;
16
24
  static log(args: {
17
25
  message: string;
18
26
  category?: LogCategory;
@@ -22,4 +30,5 @@ export declare class Logger {
22
30
  dimensions?: string[];
23
31
  } | string): void;
24
32
  private static log_entry;
33
+ private static colorize;
25
34
  }
package/logger/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Logger = exports.LogCategory = void 0;
4
+ const constants_1 = require("../constants");
4
5
  const environment_1 = require("../environment");
5
6
  // Fix from https://stackoverflow.com/questions/18391212/is-it-not-possible-to-stringify-an-error-using-json-stringify
6
7
  if (!('toJSON' in Error.prototype))
@@ -25,6 +26,18 @@ var LogCategory;
25
26
  LogCategory["FATAL"] = "FATAL";
26
27
  LogCategory["NOTIFY"] = "NOTIFY";
27
28
  })(LogCategory = exports.LogCategory || (exports.LogCategory = {}));
29
+ const ANSI_RESET = '\x1b[0m';
30
+ const ANSI_BLUE = '\x1b[34m';
31
+ const ANSI_GREEN = '\x1b[32m';
32
+ const ANSI_YELLOW = '\x1b[33m';
33
+ const ANSI_ORANGE = '\x1b[38;5;208m';
34
+ const ANSI_RED = '\x1b[31m';
35
+ const LOCAL_CATEGORY_COLORS = {
36
+ [LogCategory.INFO]: ANSI_GREEN,
37
+ [LogCategory.WARN]: ANSI_YELLOW,
38
+ [LogCategory.ERROR]: ANSI_ORANGE,
39
+ [LogCategory.NOTIFY]: ANSI_RED
40
+ };
28
41
  class Logger {
29
42
  static async initialize() {
30
43
  if (process.env.NODE_ENV !== 'test') {
@@ -42,6 +55,19 @@ class Logger {
42
55
  this.network = environment_1.Environment.getCardanoNetwork();
43
56
  Logger.isInitialized = true;
44
57
  }
58
+ static local(args) {
59
+ if (!constants_1.IS_LOCAL)
60
+ return;
61
+ const localPrefix = this.colorize('[LOCAL]', ANSI_BLUE);
62
+ if (typeof args === 'string') {
63
+ this.log(`${localPrefix} ${args}`);
64
+ return;
65
+ }
66
+ this.log({
67
+ ...args,
68
+ message: `${localPrefix} ${args.message}`
69
+ });
70
+ }
45
71
  static log(args) {
46
72
  if (!Logger.isInitialized)
47
73
  Logger.initialize();
@@ -55,6 +81,9 @@ class Logger {
55
81
  static log_entry(category, message, event, milliseconds, count, dimensions) {
56
82
  const now = new Date().toISOString();
57
83
  message = message.replace(/\\/g, '\\\\').replace(/"/g, '\\"'); // escape double quotes and already escaped escapes
84
+ const logCategory = category !== null && category !== void 0 ? category : LogCategory.INFO;
85
+ const logCategoryColor = constants_1.IS_LOCAL ? LOCAL_CATEGORY_COLORS[logCategory] : undefined;
86
+ const displayCategory = logCategoryColor ? this.colorize(logCategory, logCategoryColor) : logCategory;
58
87
  const log_event = event ? `, "event": "${event}"` : '';
59
88
  const log_milliseconds = milliseconds != undefined && milliseconds != null ? `, "milliseconds": ${milliseconds}` : '';
60
89
  const log_count = count != undefined && count != null ? `, "count": ${count}` : '';
@@ -74,9 +103,12 @@ class Logger {
74
103
  break;
75
104
  }
76
105
  // PLEASE KEEP THIS ALL ON ONE LINE SO LOGS AREN'T BROKEN UP
77
- logFunc(`{"network": "${Logger.network}", "application": "${Logger.application}", "category": "${category !== null && category !== void 0 ? category : LogCategory.INFO}", "message": "${message}"${log_event}, "timestamp": "${now}"${log_milliseconds}${log_count}${log_dimensions} }`);
106
+ logFunc(`{"network": "${Logger.network}", "application": "${Logger.application}", "category": "${displayCategory}", "message": "${message}"${log_event}, "timestamp": "${now}"${log_milliseconds}${log_count}${log_dimensions} }`);
78
107
  // PLEASE KEEP THIS ALL ON ONE LINE SO LOGS AREN'T BROKEN UP
79
108
  }
109
+ static colorize(value, ansiColor) {
110
+ return `${ansiColor}${value}${ANSI_RESET}`;
111
+ }
80
112
  }
81
113
  exports.Logger = Logger;
82
114
  Logger.isInitialized = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koralabs/kora-labs-common",
3
- "version": "6.4.13",
3
+ "version": "6.4.15",
4
4
  "description": "Kora Labs Common Utilities",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",