@koralabs/kora-labs-common 6.4.14 → 6.4.16

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
@@ -12,6 +12,9 @@ export declare class Logger {
12
12
  static application: string;
13
13
  static network: CardanoNetwork;
14
14
  private static isInitialized;
15
+ private static isInitializing;
16
+ private static getSynchronousApplicationNameFallback;
17
+ private static setFallbackMetadata;
15
18
  static initialize(): Promise<void>;
16
19
  static local(args: {
17
20
  message: string;
@@ -30,4 +33,5 @@ export declare class Logger {
30
33
  dimensions?: string[];
31
34
  } | string): void;
32
35
  private static log_entry;
36
+ private static colorize;
33
37
  }
package/logger/index.js CHANGED
@@ -26,30 +26,72 @@ var LogCategory;
26
26
  LogCategory["FATAL"] = "FATAL";
27
27
  LogCategory["NOTIFY"] = "NOTIFY";
28
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
+ };
29
41
  class Logger {
42
+ static getSynchronousApplicationNameFallback() {
43
+ var _a, _b, _c;
44
+ if (process.env.NODE_ENV === 'test')
45
+ return 'TEST';
46
+ return (_c = (_b = (_a = process.env.APPLICATION_NAME) !== null && _a !== void 0 ? _a : process.env.AWS_LAMBDA_FUNCTION_NAME) !== null && _b !== void 0 ? _b : process.env.ECS_CLUSTER) !== null && _c !== void 0 ? _c : process.cwd();
47
+ }
48
+ static setFallbackMetadata() {
49
+ if (!Logger.application) {
50
+ Logger.application = this.getSynchronousApplicationNameFallback();
51
+ }
52
+ if (!Logger.network) {
53
+ Logger.network = environment_1.Environment.getCardanoNetwork();
54
+ }
55
+ }
30
56
  static async initialize() {
57
+ this.setFallbackMetadata();
31
58
  if (process.env.NODE_ENV !== 'test') {
32
- if (!Logger.application) {
59
+ try {
33
60
  const potentialName = await environment_1.Environment.getPotentialApplicationName();
34
- if (!potentialName) {
35
- throw new Error('Logger.application must be set!');
61
+ if (potentialName) {
62
+ Logger.application = potentialName;
36
63
  }
37
- Logger.application = potentialName;
38
64
  }
39
- }
40
- else {
41
- Logger.application = 'TEST';
65
+ catch (_a) {
66
+ // fall back to sync-derived application name
67
+ }
42
68
  }
43
69
  this.network = environment_1.Environment.getCardanoNetwork();
44
70
  Logger.isInitialized = true;
45
71
  }
46
72
  static local(args) {
47
- if (constants_1.IS_LOCAL)
48
- this.log(args);
73
+ if (!constants_1.IS_LOCAL)
74
+ return;
75
+ const localPrefix = this.colorize('[LOCAL]', ANSI_BLUE);
76
+ if (typeof args === 'string') {
77
+ this.log(`${localPrefix} ${args}`);
78
+ return;
79
+ }
80
+ this.log({
81
+ ...args,
82
+ message: `${localPrefix} ${args.message}`
83
+ });
49
84
  }
50
85
  static log(args) {
51
- if (!Logger.isInitialized)
52
- Logger.initialize();
86
+ this.setFallbackMetadata();
87
+ if (!Logger.isInitialized && !Logger.isInitializing) {
88
+ Logger.isInitializing = true;
89
+ Logger.initialize()
90
+ .catch(() => { })
91
+ .finally(() => {
92
+ Logger.isInitializing = false;
93
+ });
94
+ }
53
95
  if (typeof args === 'string') {
54
96
  this.log_entry(LogCategory.INFO, args);
55
97
  return;
@@ -60,6 +102,9 @@ class Logger {
60
102
  static log_entry(category, message, event, milliseconds, count, dimensions) {
61
103
  const now = new Date().toISOString();
62
104
  message = message.replace(/\\/g, '\\\\').replace(/"/g, '\\"'); // escape double quotes and already escaped escapes
105
+ const logCategory = category !== null && category !== void 0 ? category : LogCategory.INFO;
106
+ const logCategoryColor = constants_1.IS_LOCAL ? LOCAL_CATEGORY_COLORS[logCategory] : undefined;
107
+ const displayCategory = logCategoryColor ? this.colorize(logCategory, logCategoryColor) : logCategory;
63
108
  const log_event = event ? `, "event": "${event}"` : '';
64
109
  const log_milliseconds = milliseconds != undefined && milliseconds != null ? `, "milliseconds": ${milliseconds}` : '';
65
110
  const log_count = count != undefined && count != null ? `, "count": ${count}` : '';
@@ -79,9 +124,13 @@ class Logger {
79
124
  break;
80
125
  }
81
126
  // PLEASE KEEP THIS ALL ON ONE LINE SO LOGS AREN'T BROKEN UP
82
- 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} }`);
127
+ logFunc(`{"network": "${Logger.network}", "application": "${Logger.application}", "category": "${displayCategory}", "message": "${message}"${log_event}, "timestamp": "${now}"${log_milliseconds}${log_count}${log_dimensions} }`);
83
128
  // PLEASE KEEP THIS ALL ON ONE LINE SO LOGS AREN'T BROKEN UP
84
129
  }
130
+ static colorize(value, ansiColor) {
131
+ return `${ansiColor}${value}${ANSI_RESET}`;
132
+ }
85
133
  }
86
134
  exports.Logger = Logger;
87
135
  Logger.isInitialized = false;
136
+ Logger.isInitializing = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koralabs/kora-labs-common",
3
- "version": "6.4.14",
3
+ "version": "6.4.16",
4
4
  "description": "Kora Labs Common Utilities",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",