1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-22 06:49:40 +02:00
portainer/app/docker/helpers/logHelper/formatJSONLogs.ts
andres-portainer 535a26412f
fix(logging): default to pretty logging [EE-4371] (#7847)
* fix(logging): default to pretty logging EE-4371

* feat(app/logs): prettify stack traces in JSON logs

* feat(nomad/logs): prettify JSON logs in log viewer

* feat(kubernetes/logs): prettigy JSON logs in log viewers

* feat(app/logs): format and color zerolog prettified logs

* fix(app/logs): pre-parse logs when they are double serialized

Co-authored-by: andres-portainer <andres-portainer@users.noreply.github.com>
Co-authored-by: LP B <xAt0mZ@users.noreply.github.com>
2022-10-20 16:33:54 +02:00

55 lines
1.4 KiB
TypeScript

import { without } from 'lodash';
import { FormattedLine, Span, JSONLogs, TIMESTAMP_LENGTH } from './types';
import {
formatCaller,
formatKeyValuePair,
formatLevel,
formatMessage,
formatStackTrace,
formatTime,
} from './formatters';
function removeKnownKeys(keys: string[]) {
return without(keys, 'time', 'level', 'caller', 'message', 'stack_trace');
}
export function formatJSONLine(
rawText: string,
withTimestamps?: boolean
): FormattedLine[] {
const spans: Span[] = [];
const lines: FormattedLine[] = [];
let line = '';
const text = withTimestamps ? rawText.substring(TIMESTAMP_LENGTH) : rawText;
const json: JSONLogs = JSON.parse(text);
const { time, level, caller, message, stack_trace: stackTrace } = json;
const keys = removeKnownKeys(Object.keys(json));
if (withTimestamps) {
const timestamp = rawText.substring(0, TIMESTAMP_LENGTH);
spans.push({ text: timestamp });
line += `${timestamp}`;
}
line += formatTime(time, spans, line);
line += formatLevel(level, spans, line);
line += formatCaller(caller, spans, line);
line += formatMessage(message, spans, line, !!keys.length);
keys.forEach((key, idx) => {
line += formatKeyValuePair(
key,
json[key],
spans,
line,
idx === keys.length - 1
);
});
lines.push({ line, spans });
formatStackTrace(stackTrace, lines);
return lines;
}