@koralabs/kora-labs-common 6.4.14 → 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 +1 -0
- package/logger/index.js +30 -3
- package/package.json +1 -1
package/logger/index.d.ts
CHANGED
package/logger/index.js
CHANGED
|
@@ -26,6 +26,18 @@ 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 {
|
|
30
42
|
static async initialize() {
|
|
31
43
|
if (process.env.NODE_ENV !== 'test') {
|
|
@@ -44,8 +56,17 @@ class Logger {
|
|
|
44
56
|
Logger.isInitialized = true;
|
|
45
57
|
}
|
|
46
58
|
static local(args) {
|
|
47
|
-
if (constants_1.IS_LOCAL)
|
|
48
|
-
|
|
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
|
+
});
|
|
49
70
|
}
|
|
50
71
|
static log(args) {
|
|
51
72
|
if (!Logger.isInitialized)
|
|
@@ -60,6 +81,9 @@ class Logger {
|
|
|
60
81
|
static log_entry(category, message, event, milliseconds, count, dimensions) {
|
|
61
82
|
const now = new Date().toISOString();
|
|
62
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;
|
|
63
87
|
const log_event = event ? `, "event": "${event}"` : '';
|
|
64
88
|
const log_milliseconds = milliseconds != undefined && milliseconds != null ? `, "milliseconds": ${milliseconds}` : '';
|
|
65
89
|
const log_count = count != undefined && count != null ? `, "count": ${count}` : '';
|
|
@@ -79,9 +103,12 @@ class Logger {
|
|
|
79
103
|
break;
|
|
80
104
|
}
|
|
81
105
|
// PLEASE KEEP THIS ALL ON ONE LINE SO LOGS AREN'T BROKEN UP
|
|
82
|
-
logFunc(`{"network": "${Logger.network}", "application": "${Logger.application}", "category": "${
|
|
106
|
+
logFunc(`{"network": "${Logger.network}", "application": "${Logger.application}", "category": "${displayCategory}", "message": "${message}"${log_event}, "timestamp": "${now}"${log_milliseconds}${log_count}${log_dimensions} }`);
|
|
83
107
|
// PLEASE KEEP THIS ALL ON ONE LINE SO LOGS AREN'T BROKEN UP
|
|
84
108
|
}
|
|
109
|
+
static colorize(value, ansiColor) {
|
|
110
|
+
return `${ansiColor}${value}${ANSI_RESET}`;
|
|
111
|
+
}
|
|
85
112
|
}
|
|
86
113
|
exports.Logger = Logger;
|
|
87
114
|
Logger.isInitialized = false;
|