Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/backend log highlighter: add support for "Slow" HTTP actions
Adorian Doran777709289471
diff --git a/apps/client/src/stylesheets/theme-next-dark.css b/apps/client/src/stylesheets/theme-next-dark.cssindex f413c4d49d..f07a98967c 100644--- a/apps/client/src/stylesheets/theme-next-dark.css+++ b/apps/client/src/stylesheets/theme-next-dark.css@@ -271,6 +271,7 @@/* Backend log (the `text/x-trilium-log` code note MIME) */--log-timestamp-color: #818181;+ --log-slow-color: #ff9d7f;--log-http-line-background-color: rgba(255, 255, 255, 0.02);--log-http-verb-color: #d99bf4;--log-http-url-color: #d0b6e4;@@ -350,6 +351,7 @@--task-checkbox-done-hover-color: #41b441;--task-checkbox-unknown-state-color: #e95d5d;--task-checkbox-glyph-color: white;+}/*diff --git a/apps/client/src/stylesheets/theme-next-light.css b/apps/client/src/stylesheets/theme-next-light.cssindex b065f35237..c37466aba9 100644--- a/apps/client/src/stylesheets/theme-next-light.css+++ b/apps/client/src/stylesheets/theme-next-light.css@@ -269,6 +269,7 @@/* Backend log (the `text/x-trilium-log` code note MIME) */--log-timestamp-color: var(--muted-text-color);+ --log-slow-color: #d29922;--log-http-line-background-color: rgba(0, 0, 0, 0.03);--log-http-verb-color: #0969da;--log-http-url-color: #1b6b6b;diff --git a/packages/codemirror/src/extensions/trilium_log_highlighter.spec.ts b/packages/codemirror/src/extensions/trilium_log_highlighter.spec.tsindex 4b4652f102..2e5c778881 100644--- a/packages/codemirror/src/extensions/trilium_log_highlighter.spec.ts+++ b/packages/codemirror/src/extensions/trilium_log_highlighter.spec.ts@@ -82,6 +82,22 @@ describe("triliumLogHighlighter", () => {}});+ it("handles the `Slow` marker the server prepends to slow requests", () => {+ // The `Slow ` prefix shifts the status/verb/URL offsets, so assert each span exactly.+ const doc = "00:10:01.540 Slow 304 GET /api/notes/kwjrB3G4TbPY/blob with 209 bytes took 147ms";+ const spans = decorationsOf(state(doc));++ expect(sliceOf(doc, spans, "cm-log-slow")).toBe("Slow");+ expect(sliceOf(doc, spans, "cm-log-status cm-log-status-3xx")).toBe("304");+ expect(sliceOf(doc, spans, "cm-log-verb")).toBe("GET");+ expect(sliceOf(doc, spans, "cm-log-url")).toBe("/api/notes/kwjrB3G4TbPY/blob");+ expect(sliceOf(doc, spans, "cm-log-timestamp")).toBe("00:10:01.540");+ expect(classesOf(doc)).toContain("cm-log-http"); // still treated as an HTTP line++ // A non-slow line carries no marker.+ expect(classesOf("18:18:38.904 204 PUT /api/options with 0 bytes took 2ms")).not.toContain("cm-log-slow");+ });+it("handles ERROR: blocks, closes them at the next entry, and carries JS Info across lines", () => {const doc = ["17:34:08.519 ERROR: boom", // 1: error header (ERROR: variant)diff --git a/packages/codemirror/src/extensions/trilium_log_highlighter.ts b/packages/codemirror/src/extensions/trilium_log_highlighter.tsindex c6f25a3a0b..fda6d4567e 100644--- a/packages/codemirror/src/extensions/trilium_log_highlighter.ts+++ b/packages/codemirror/src/extensions/trilium_log_highlighter.ts@@ -15,8 +15,9 @@ import { Decoration, type DecorationSet, EditorView } from "@codemirror/view";** Recognised entries:* - the leading timestamp (muted, on every entry)- * - HTTP request lines `<ts> <status> <VERB> …` — the whole line is tinted, the verb bolded and- * the status colour-coded by class (2xx/3xx/4xx/5xx)+ * - HTTP request lines `<ts> [Slow ]<status> <VERB> <url> …` — the whole line is tinted, the verb+ * bolded, the URL coloured and the status colour-coded by class (2xx/3xx/4xx/5xx). The optional+ * `Slow` marker (prepended by the server for requests taking >= 10ms) is flagged as a warning.* - `<ts> JS Error: …` and `<ts> ERROR: …` lines — coloured red, together with their following* continuation lines (e.g. wrapped stack traces), which carry no timestamp of their own* - `<ts> JS Info: …` lines (and their continuation lines) — placeholder class, no colour yet@@ -25,9 +26,13 @@ import { Decoration, type DecorationSet, EditorView } from "@codemirror/view";// Length of the leading `HH:MM:SS.mmm` timestamp.const TIMESTAMP_LENGTH = 12;const TIMESTAMP = /^\d{2}:\d{2}:\d{2}\.\d{3}/;-// After `<ts> `: a 3-digit status, a space, a known HTTP verb, a space, then the request URL (a-// single whitespace-free token). The fixed-width prefix lets us derive the offsets without a rescan.-const HTTP_REQUEST = /^\d{2}:\d{2}:\d{2}\.\d{3} (\d{3}) (GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS) (\S+)/;+// After `<ts> `: an optional `Slow ` marker (the server prepends it when the request took >= 10ms),+// a 3-digit status, a space, a known HTTP verb, a space, then the request URL (a single+// whitespace-free token). Everything up to the URL is fixed-width once the optional marker's length+// is known, so the offsets can be derived without a rescan.+const HTTP_REQUEST = /^\d{2}:\d{2}:\d{2}\.\d{3} (Slow )?(\d{3}) (GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS) (\S+)/;+/** Length of the `Slow` word itself (the capture group includes its trailing space). */+const SLOW_LENGTH = 4;const ERROR_LINE = /^\d{2}:\d{2}:\d{2}\.\d{3} (?:JS Error|ERROR):/;const INFO_LINE = /^\d{2}:\d{2}:\d{2}\.\d{3} JS Info:/;@@ -35,6 +40,7 @@ const INFO_LINE = /^\d{2}:\d{2}:\d{2}\.\d{3} JS Info:/;export const MAX_HIGHLIGHTED_LINES = 10_000;const timestampMark = Decoration.mark({ class: "cm-log-timestamp" });+const slowMark = Decoration.mark({ class: "cm-log-slow" });const verbMark = Decoration.mark({ class: "cm-log-verb" });const urlMark = Decoration.mark({ class: "cm-log-url" });const httpLine = Decoration.line({ class: "cm-log-http" });@@ -54,6 +60,7 @@ const statusMarks: Record<string, Decoration> = {const logHighlightTheme = EditorView.baseTheme({".cm-log-timestamp": { color: "var(--log-timestamp-color, var(--muted-text-color))" },".cm-log-http": { backgroundColor: "var(--log-http-line-background-color, color-mix(in srgb, var(--main-text-color) 3%, transparent))" },+ ".cm-log-slow": { fontWeight: "bold", color: "var(--log-slow-color, #d29922)" },".cm-log-verb": { fontWeight: "bold", color: "var(--log-http-verb-color, #539bf5)" },".cm-log-url": { color: "var(--log-http-url-color, #268a8a)" },".cm-log-status": { fontWeight: "bold" },@@ -140,16 +147,23 @@ function decorateLine(builder: RangeSetBuilder<Decoration>, line: { from: numberbuilder.add(lineStart, lineStart + TIMESTAMP_LENGTH, timestampMark);if (httpMatch) {- // Fixed layout: `<12-char ts><space><3-char status><space><verb>`.- const statusStart = lineStart + TIMESTAMP_LENGTH + 1;- const verbStart = statusStart + 4;- const statusMark = statusMarks[httpMatch[1][0]];+ // Layout: `<12-char ts><space>[Slow ]<3-char status><space><verb><space><url>`. The optional+ // `Slow ` capture includes its trailing space, so its length shifts everything after it.+ const [ , slowPrefix = "", , verb, url ] = httpMatch;+ const slowStart = lineStart + TIMESTAMP_LENGTH + 1;+ const statusStart = slowStart + slowPrefix.length;+ const verbStart = statusStart + 4; // 3-digit status + space+ const urlStart = verbStart + verb.length + 1;++ if (slowPrefix) {+ builder.add(slowStart, slowStart + SLOW_LENGTH, slowMark);+ }+ const statusMark = statusMarks[httpMatch[2][0]];if (statusMark) {builder.add(statusStart, statusStart + 3, statusMark);}- builder.add(verbStart, verbStart + httpMatch[2].length, verbMark);- const urlStart = verbStart + httpMatch[2].length + 1;- builder.add(urlStart, urlStart + httpMatch[3].length, urlMark);+ builder.add(verbStart, verbStart + verb.length, verbMark);+ builder.add(urlStart, urlStart + url.length, urlMark);}return newBlockLine;