fix(logs): handle unexpected log messages (#2303)

* fix(logs): handle unexpected log messages

* fix(logs): update regexp for json to appease the lgtm gods
pull/2294/head
Danshil Kokil Mungur 3 years ago committed by GitHub
parent febf0677b8
commit f284e4ab97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,7 +3,7 @@ import type { PaginatedResponse } from './common';
export type LogMessage = {
timestamp: string;
level: string;
label: string;
label?: string;
message: string;
data?: Record<string, unknown>;
};

@ -258,25 +258,29 @@ settingsRoutes.get(
try {
fs.readFileSync(logFile)
.toString()
.split('\n')
.split(/(?=\n\d{4}-\d{2})/g)
.forEach((line) => {
if (!line.length) return;
const timestamp = line.match(new RegExp(/^.{24}/)) || [];
const level = line.match(new RegExp(/\s\[\w+\]/)) || [];
const label = line.match(new RegExp(/\]\[.+?\]/)) || [];
const message = line.match(new RegExp(/:\s([^{}]+)({.*})?/)) || [];
const jsonRegexp = new RegExp(
/[{[]{1}([,:{}[\]0-9.\-+Eaeflnr-u \n\r\t]|"[^"\n]*?")+[}\]]{1}/
);
const timestamp = line.match(new RegExp(/.{24}/)) || [];
const level = line.match(new RegExp(/(?<=.{24}\s\[).+?(?=\])/)) || [];
const label =
line.match(new RegExp(/(?<=.{24}\s\[.+\]\[).+(?=\])/)) || [];
const message =
line.match(new RegExp(/(?<=\[.+\]:\s)[\s\S][^\r]+/)) || [];
const data = message[0].match(jsonRegexp) || [];
if (level.length && filter.includes(level[0].slice(2, -1))) {
if (level.length && filter.includes(level[0])) {
logs.push({
timestamp: timestamp[0],
level: level.length ? level[0].slice(2, -1) : '',
label: label.length ? label[0].slice(2, -1) : '',
message: message.length && message[1] ? message[1] : '',
data:
message.length && message[2]
? JSON.parse(message[2])
: undefined,
level: level[0],
label: label[0],
message: message[0].replace(jsonRegexp, ''),
data: data.length ? JSON.parse(data[0]) : undefined,
});
}
});

@ -321,7 +321,9 @@ const SettingsLogs: React.FC = () => {
{row.level.toUpperCase()}
</Badge>
</Table.TD>
<Table.TD className="text-gray-300">{row.label}</Table.TD>
<Table.TD className="text-gray-300">
{row.label ?? ''}
</Table.TD>
<Table.TD className="text-gray-300">{row.message}</Table.TD>
<Table.TD className="flex flex-wrap items-center justify-end -m-1">
{row.data && (

Loading…
Cancel
Save