From 079927a9fafb0ab29add7f823d69b3b26d744193 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Tue, 2 Apr 2024 15:50:29 -0600 Subject: [PATCH] Support other log types as well --- web/src/pages/Logs.tsx | 96 +++++++++++++++++++++++++++++++++++------- 1 file changed, 81 insertions(+), 15 deletions(-) diff --git a/web/src/pages/Logs.tsx b/web/src/pages/Logs.tsx index ec7cf45a1..31ac86b9d 100644 --- a/web/src/pages/Logs.tsx +++ b/web/src/pages/Logs.tsx @@ -11,9 +11,14 @@ import useSWR from "swr"; const logTypes = ["frigate", "go2rtc", "nginx"] as const; type LogType = (typeof logTypes)[number]; -const datestamp = /\[[\d\s-:]*]/; -const severity = /(DEBUG)|(INFO)|(WARNING)|(ERROR)/; -const section = /[\w.]*/; +const frigateDateStamp = /\[[\d\s-:]*]/; +const frigateSeverity = /(DEBUG)|(INFO)|(WARNING)|(ERROR)/; +const frigateSection = /[\w.]*/; + +const goSeverity = /(DEB )|(INF )|(WARN )|(ERR )/; +const goSection = /\[[\w]*]/; + +const ngSeverity = /(GET)|(POST)|(PATCH)|(DELETE)/; function Logs() { const [logService, setLogService] = useState("frigate"); @@ -21,8 +26,12 @@ function Logs() { const { data: frigateLogs } = useSWR("logs/frigate", { refreshInterval: 1000, }); - const { data: go2rtcLogs } = useSWR("logs/go2rtc", { refreshInterval: 1000 }); - const { data: nginxLogs } = useSWR("logs/nginx", { refreshInterval: 1000 }); + const { data: go2rtcLogs } = useSWR("logs/go2rtc", { + refreshInterval: 1000, + }); + const { data: nginxLogs } = useSWR("logs/nginx", { + refreshInterval: 1000, + }); // convert to log data @@ -47,13 +56,13 @@ function Logs() { return frigateLogs .split("\n") .map((line) => { - const match = datestamp.exec(line); + const match = frigateDateStamp.exec(line); if (!match) { return null; } - const sectionMatch = section.exec( + const sectionMatch = frigateSection.exec( line.substring(match.index + match[0].length).trim(), ); @@ -63,7 +72,7 @@ function Logs() { return { dateStamp: match.toString().slice(1, -1), - severity: severity + severity: frigateSeverity .exec(line) ?.at(0) ?.toString() @@ -76,18 +85,75 @@ function Logs() { }) .filter((value) => value != null) as LogLine[]; } 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") { - 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 { return []; } }, [logService, frigateLogs, go2rtcLogs, nginxLogs]); - //console.log(`the logs are ${JSON.stringify(logLines)}`); - const handleCopyLogs = useCallback(() => { - copy(logs); + if (logs) { + copy(logs); + } }, [logs]); // scroll to bottom button @@ -166,7 +232,7 @@ function Logs() { >
- Severity + Type
Timestamp @@ -179,7 +245,7 @@ function Logs() {
{logLines.map((log, idx) => ( - + ))}