Support other log types as well

This commit is contained in:
Nicolas Mowen 2024-04-02 15:50:29 -06:00
parent 8b9a5dcdf7
commit 079927a9fa

View File

@ -11,9 +11,14 @@ import useSWR from "swr";
const logTypes = ["frigate", "go2rtc", "nginx"] as const; const logTypes = ["frigate", "go2rtc", "nginx"] as const;
type LogType = (typeof logTypes)[number]; type LogType = (typeof logTypes)[number];
const datestamp = /\[[\d\s-:]*]/; const frigateDateStamp = /\[[\d\s-:]*]/;
const severity = /(DEBUG)|(INFO)|(WARNING)|(ERROR)/; const frigateSeverity = /(DEBUG)|(INFO)|(WARNING)|(ERROR)/;
const section = /[\w.]*/; const frigateSection = /[\w.]*/;
const goSeverity = /(DEB )|(INF )|(WARN )|(ERR )/;
const goSection = /\[[\w]*]/;
const ngSeverity = /(GET)|(POST)|(PATCH)|(DELETE)/;
function Logs() { function Logs() {
const [logService, setLogService] = useState<LogType>("frigate"); const [logService, setLogService] = useState<LogType>("frigate");
@ -21,8 +26,12 @@ function Logs() {
const { data: frigateLogs } = useSWR<string>("logs/frigate", { const { data: frigateLogs } = useSWR<string>("logs/frigate", {
refreshInterval: 1000, refreshInterval: 1000,
}); });
const { data: go2rtcLogs } = useSWR("logs/go2rtc", { refreshInterval: 1000 }); const { data: go2rtcLogs } = useSWR<string>("logs/go2rtc", {
const { data: nginxLogs } = useSWR("logs/nginx", { refreshInterval: 1000 }); refreshInterval: 1000,
});
const { data: nginxLogs } = useSWR<string>("logs/nginx", {
refreshInterval: 1000,
});
// convert to log data // convert to log data
@ -47,13 +56,13 @@ function Logs() {
return frigateLogs return frigateLogs
.split("\n") .split("\n")
.map((line) => { .map((line) => {
const match = datestamp.exec(line); const match = frigateDateStamp.exec(line);
if (!match) { if (!match) {
return null; return null;
} }
const sectionMatch = section.exec( const sectionMatch = frigateSection.exec(
line.substring(match.index + match[0].length).trim(), line.substring(match.index + match[0].length).trim(),
); );
@ -63,7 +72,7 @@ function Logs() {
return { return {
dateStamp: match.toString().slice(1, -1), dateStamp: match.toString().slice(1, -1),
severity: severity severity: frigateSeverity
.exec(line) .exec(line)
?.at(0) ?.at(0)
?.toString() ?.toString()
@ -76,18 +85,75 @@ function Logs() {
}) })
.filter((value) => value != null) as LogLine[]; .filter((value) => value != null) as LogLine[];
} else if (logService == "go2rtc") { } else if (logService == "go2rtc") {
return []; if (!go2rtcLogs) {
return [];
}
return go2rtcLogs
.split("\n")
.map((line) => {
if (line.length == 0) {
return null;
}
const severity = goSeverity.exec(line);
let section =
goSection.exec(line)?.toString()?.slice(1, -1) ?? "startup";
if (frigateSeverity.exec(section)) {
section = "startup";
}
let contentStart;
if (section == "startup") {
if (severity) {
contentStart = severity.index + severity[0].length;
} else {
contentStart = line.lastIndexOf("]") + 1;
}
} else {
contentStart = line.indexOf(section) + section.length + 2;
}
return {
dateStamp: line.substring(0, 19),
severity: "INFO",
section: section,
content: line.substring(contentStart).trim(),
};
})
.filter((value) => value != null) as LogLine[];
} else if (logService == "nginx") { } else if (logService == "nginx") {
return []; if (!nginxLogs) {
return [];
}
return nginxLogs
.split("\n")
.map((line) => {
if (line.length == 0) {
return null;
}
return {
dateStamp: line.substring(0, 19),
severity: "INFO",
section: ngSeverity.exec(line)?.at(0)?.toString() ?? "META",
content: line.substring(line.indexOf(" ", 20)).trim(),
};
})
.filter((value) => value != null) as LogLine[];
} else { } else {
return []; return [];
} }
}, [logService, frigateLogs, go2rtcLogs, nginxLogs]); }, [logService, frigateLogs, go2rtcLogs, nginxLogs]);
//console.log(`the logs are ${JSON.stringify(logLines)}`);
const handleCopyLogs = useCallback(() => { const handleCopyLogs = useCallback(() => {
copy(logs); if (logs) {
copy(logs);
}
}, [logs]); }, [logs]);
// scroll to bottom button // scroll to bottom button
@ -166,7 +232,7 @@ function Logs() {
> >
<div className="py-2 sticky top-0 -translate-y-1/4 grid grid-cols-5 sm:grid-cols-8 md:grid-cols-12 bg-background *:p-2"> <div className="py-2 sticky top-0 -translate-y-1/4 grid grid-cols-5 sm:grid-cols-8 md:grid-cols-12 bg-background *:p-2">
<div className="p-1 flex items-center capitalize border-y border-l"> <div className="p-1 flex items-center capitalize border-y border-l">
Severity Type
</div> </div>
<div className="col-span-2 sm:col-span-1 flex items-center border-y border-l"> <div className="col-span-2 sm:col-span-1 flex items-center border-y border-l">
Timestamp Timestamp
@ -179,7 +245,7 @@ function Logs() {
</div> </div>
</div> </div>
{logLines.map((log, idx) => ( {logLines.map((log, idx) => (
<LogLineData key={idx} offset={idx} line={log} /> <LogLineData key={`${idx}-${log.content}`} offset={idx} line={log} />
))} ))}
<div ref={endLogRef} /> <div ref={endLogRef} />
</div> </div>