@koralabs/kora-labs-common 6.6.3 → 6.6.4
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 +50 -2
- package/package.json +1 -1
package/logger/index.d.ts
CHANGED
package/logger/index.js
CHANGED
|
@@ -102,11 +102,11 @@ class Logger {
|
|
|
102
102
|
}
|
|
103
103
|
static log_entry(category, message, event, milliseconds, count, dimensions, context) {
|
|
104
104
|
const now = new Date().toISOString();
|
|
105
|
-
message =
|
|
105
|
+
message = Logger.escapeJsonStringContent(message);
|
|
106
106
|
const logCategory = category !== null && category !== void 0 ? category : LogCategory.INFO;
|
|
107
107
|
const logCategoryColor = constants_1.IS_LOCAL ? LOCAL_CATEGORY_COLORS[logCategory] : undefined;
|
|
108
108
|
const displayCategory = logCategoryColor ? this.colorize(logCategory, logCategoryColor) : logCategory;
|
|
109
|
-
const log_event = event ? `, "event": "${event}"` : '';
|
|
109
|
+
const log_event = event ? `, "event": "${Logger.escapeJsonStringContent(event)}"` : '';
|
|
110
110
|
const log_milliseconds = milliseconds != undefined && milliseconds != null ? `, "milliseconds": ${milliseconds}` : '';
|
|
111
111
|
const log_count = count != undefined && count != null ? `, "count": ${count}` : '';
|
|
112
112
|
const log_dimensions = dimensions && Object.keys(dimensions).length ? `, "dimensions": ${JSON.stringify(dimensions)}` : '';
|
|
@@ -132,6 +132,54 @@ class Logger {
|
|
|
132
132
|
static colorize(value, ansiColor) {
|
|
133
133
|
return `${ansiColor}${value}${ANSI_RESET}`;
|
|
134
134
|
}
|
|
135
|
+
// Escape characters that JSON forbids inside a string value: backslash,
|
|
136
|
+
// double quote, and control chars 0x00-0x1F. ESC (0x1B) is left alone so
|
|
137
|
+
// ANSI color sequences from `colorize`/`Logger.local` keep rendering on
|
|
138
|
+
// developer terminals; in production no ANSI is ever introduced, so the
|
|
139
|
+
// emitted line is valid JSON. Newlines/tabs in error stacks would otherwise
|
|
140
|
+
// produce invalid JSON that downstream consumers (CloudWatch subscribers,
|
|
141
|
+
// app-alerts ingestion) can't peel — the previous regex only handled \\ and
|
|
142
|
+
// \", letting stack traces through unescaped.
|
|
143
|
+
static escapeJsonStringContent(s) {
|
|
144
|
+
let out = '';
|
|
145
|
+
for (let i = 0; i < s.length; i++) {
|
|
146
|
+
const ch = s.charCodeAt(i);
|
|
147
|
+
if (ch === 0x5c) {
|
|
148
|
+
out += '\\\\';
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (ch === 0x22) {
|
|
152
|
+
out += '\\"';
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (ch === 0x08) {
|
|
156
|
+
out += '\\b';
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (ch === 0x09) {
|
|
160
|
+
out += '\\t';
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (ch === 0x0a) {
|
|
164
|
+
out += '\\n';
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (ch === 0x0c) {
|
|
168
|
+
out += '\\f';
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (ch === 0x0d) {
|
|
172
|
+
out += '\\r';
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (ch < 0x20 && ch !== 0x1b) {
|
|
176
|
+
out += '\\u' + ch.toString(16).padStart(4, '0');
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
out += s[i];
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
135
183
|
}
|
|
136
184
|
exports.Logger = Logger;
|
|
137
185
|
Logger.isInitialized = false;
|