fix(app/logs): change pattern to detect double serialized JSON logs [EE-4525] (#7962)

* fix(app/logs): change pattern to detect double serialized JSON logs

* fix(app/logs): fallback to raw display when parsing fails + include timestamp for Zerolog logs
pull/7996/head
LP B 2022-11-04 13:58:18 +01:00 committed by GitHub
parent 9f3d5185b0
commit 6b02d9a1e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 13 deletions

View File

@ -31,7 +31,7 @@ export function formatJSONLine(
if (withTimestamps) { if (withTimestamps) {
const timestamp = rawText.substring(0, TIMESTAMP_LENGTH); const timestamp = rawText.substring(0, TIMESTAMP_LENGTH);
spans.push({ text: timestamp }); spans.push({ text: timestamp });
line += `${timestamp}`; line += `${timestamp} `;
} }
line += formatTime(time, spans, line); line += formatTime(time, spans, line);
line += formatLevel(level, spans, line); line += formatLevel(level, spans, line);

View File

@ -33,8 +33,14 @@ export function formatLogs(
if (stripHeaders) { if (stripHeaders) {
logs = stripHeadersFunc(logs); logs = stripHeadersFunc(logs);
} }
if (logs.includes('\\n')) { // if JSON logs come serialized 2 times, parse them once to unwrap them
logs = JSON.parse(logs); // for example when retrieving Edge Agent logs on Nomad
if (logs.startsWith('"')) {
try {
logs = JSON.parse(logs);
} catch (error) {
// noop, throw error away if logs cannot be parsed
}
} }
const tokens: Token[][] = tokenize(logs); const tokens: Token[][] = tokenize(logs);
@ -83,16 +89,26 @@ export function formatLogs(
} }
const text = stripEscapeCodes(tokenLine); const text = stripEscapeCodes(tokenLine);
if ( try {
(!withTimestamps && text.startsWith('{')) || if (
(withTimestamps && text.substring(TIMESTAMP_LENGTH).startsWith('{')) (!withTimestamps && text.startsWith('{')) ||
) { (withTimestamps && text.substring(TIMESTAMP_LENGTH).startsWith('{'))
const lines = formatJSONLine(text, withTimestamps); ) {
formattedLogs.push(...lines); const lines = formatJSONLine(text, withTimestamps);
} else if (ZerologRegex.test(text)) { formattedLogs.push(...lines);
const lines = formatZerologLogs(text, withTimestamps); } else if (
formattedLogs.push(...lines); (!withTimestamps && ZerologRegex.test(text)) ||
} else { (withTimestamps &&
ZerologRegex.test(text.substring(TIMESTAMP_LENGTH)))
) {
const lines = formatZerologLogs(text, withTimestamps);
formattedLogs.push(...lines);
} else {
spans.push({ fgColor, bgColor, text, fontWeight });
line += text;
}
} catch (error) {
// in case parsing fails for whatever reason, push the raw logs and continue
spans.push({ fgColor, bgColor, text, fontWeight }); spans.push({ fgColor, bgColor, text, fontWeight });
line += text; line += text;
} }

View File

@ -55,6 +55,12 @@ export function formatZerologLogs(rawText: string, withTimestamps?: boolean) {
const text = withTimestamps ? rawText.substring(TIMESTAMP_LENGTH) : rawText; const text = withTimestamps ? rawText.substring(TIMESTAMP_LENGTH) : rawText;
if (withTimestamps) {
const timestamp = rawText.substring(0, TIMESTAMP_LENGTH);
spans.push({ text: timestamp });
line += `${timestamp} `;
}
const [, date, level, caller, messageAndPairs] = const [, date, level, caller, messageAndPairs] =
text.match(ZerologRegex) || []; text.match(ZerologRegex) || [];