Skip to content

Build your personal knowledge base with Trilium Notes

feat(log): add a warning level, colored amber in the backend log

Elian Doran93f901a2d44a
diff --git a/apps/client/src/stylesheets/theme-next-dark.css b/apps/client/src/stylesheets/theme-next-dark.css
index 8422efc65d..9dff38dd85 100644
--- a/apps/client/src/stylesheets/theme-next-dark.css
+++ b/apps/client/src/stylesheets/theme-next-dark.css
@@ -291,6 +291,8 @@
--log-search-count-color: #818181;
--log-error-color: #FF867F;
--log-error-line-background-color: #d44b4b1f;
+ --log-warning-color: #e3b341;
+ --log-warning-line-background-color: #e3b3411a;
--log-info-color: inherit; /* Placeholder: give it a colour to make info lines stand out. */
/* Search query syntax (the search string of a saved search) */
diff --git a/apps/client/src/stylesheets/theme-next-light.css b/apps/client/src/stylesheets/theme-next-light.css
index feacec82da..cea6678381 100644
--- a/apps/client/src/stylesheets/theme-next-light.css
+++ b/apps/client/src/stylesheets/theme-next-light.css
@@ -289,6 +289,8 @@
--log-search-count-color: #00000077;
--log-error-color: #b32017;
--log-error-line-background-color: #ff000017;
+ --log-warning-color: #9a6700;
+ --log-warning-line-background-color: #ffb30017;
--log-info-color: inherit; /* Placeholder: give it a colour to make info lines stand out. */
/* Search query syntax (the search string of a saved search) */
diff --git a/apps/server/src/routes/login.ts b/apps/server/src/routes/login.ts
index 62386ae215..37aab08ac1 100644
--- a/apps/server/src/routes/login.ts
+++ b/apps/server/src/routes/login.ts
@@ -117,9 +117,9 @@ async function login(req: Request, res: Response) {
function sendLoginError(req: Request, res: Response, errorType: 'password' | 'totp' = 'password') {
// note that logged IP address is usually meaningless since the traffic should come from a reverse proxy
if (totp.isTotpEnabled()) {
- getLog().info(`WARNING: Wrong ${errorType} from ${req.ip}, rejecting.`);
+ getLog().warn(`Wrong ${errorType} from ${req.ip}, rejecting.`);
} else {
- getLog().info(`WARNING: Wrong password from ${req.ip}, rejecting.`);
+ getLog().warn(`Wrong password from ${req.ip}, rejecting.`);
}
// The client submits via fetch; report the failed factor as JSON. The 401 keeps the
diff --git a/apps/server/src/services/auth.ts b/apps/server/src/services/auth.ts
index 834d10622d..9612aca377 100644
--- a/apps/server/src/services/auth.ts
+++ b/apps/server/src/services/auth.ts
@@ -271,7 +271,7 @@ async function checkCredentials(req: Request, res: Response, next: NextFunction)
const totpToken = req.headers["trilium-totp"];
if (totp.isTotpEnabled() && (typeof totpToken !== "string" || !totpToken)) {
res.setHeader("Content-Type", "text/plain").status(401).send("TOTP token is required");
- getLog().info(`WARNING: Missing TOTP token from ${req.ip}, rejecting.`);
+ getLog().warn(`Missing TOTP token from ${req.ip}, rejecting.`);
return;
}
@@ -280,10 +280,10 @@ async function checkCredentials(req: Request, res: Response, next: NextFunction)
const failedFactor = await cls.init(() => verifyLoginCredentials(password, totpToken));
if (failedFactor === "password") {
res.setHeader("Content-Type", "text/plain").status(401).send("Incorrect password");
- getLog().info(`WARNING: Wrong password from ${req.ip}, rejecting.`);
+ getLog().warn(`Wrong password from ${req.ip}, rejecting.`);
} else if (failedFactor === "totp") {
res.setHeader("Content-Type", "text/plain").status(401).send("Incorrect TOTP token");
- getLog().info(`WARNING: Wrong TOTP token from ${req.ip}, rejecting.`);
+ getLog().warn(`Wrong TOTP token from ${req.ip}, rejecting.`);
} else {
next();
}
diff --git a/apps/server/src/www.ts b/apps/server/src/www.ts
index 6cf778d87a..7a90d34bba 100644
--- a/apps/server/src/www.ts
+++ b/apps/server/src/www.ts
@@ -111,7 +111,7 @@ async function displayStartupMessage() {
getLog().info(`💾 DB size: ${utils.formatSize(getDbSize() * 1024)}`);
if (isScriptingEnabled()) {
- getLog().info("WARNING: Backend script execution is ENABLED. Backend scripts have full server access including " +
+ getLog().warn("Backend script execution is ENABLED. Backend scripts have full server access including " +
"filesystem, network, and OS commands. Only enable in trusted environments.");
} else {
getLog().info("Backend script execution is DISABLED. Set [Security] backendScriptingEnabled=true in config.ini to enable.");
diff --git a/docs/User Guide/User Guide/Troubleshooting/Error logs/Backend (server) logs.md b/docs/User Guide/User Guide/Troubleshooting/Error logs/Backend (server) logs.md
index 2710471d90..2186642042 100644
--- a/docs/User Guide/User Guide/Troubleshooting/Error logs/Backend (server) logs.md
+++ b/docs/User Guide/User Guide/Troubleshooting/Error logs/Backend (server) logs.md
@@ -3,7 +3,7 @@
In the <a class="reference-link" href="../../Basic%20Concepts%20and%20Features/UI%20Elements/Global%20menu.md">Global menu</a>, go to _Advanced_ → _Backend log_. This will display the current backend log (i.e. today's), with the historical information available only on disk (see below).
-The log is colored to make it easier to read: request status codes by class, slow SQL queries, and errors in red. The statement of a slow SQL query is highlighted the same way as in the <a class="reference-link" href="../../Advanced%20Usage/Database/Manually%20altering%20the%20database/SQL%20Console.md">SQL Console</a>, using the color scheme of code notes (see _Color schemes_ in <a class="reference-link" href="../../Note%20Types/Code.md">Code</a>).
+The log is colored to make it easier to read: request status codes by class, slow SQL queries, warnings in amber and errors in red. The statement of a slow SQL query is highlighted the same way as in the <a class="reference-link" href="../../Advanced%20Usage/Database/Manually%20altering%20the%20database/SQL%20Console.md">SQL Console</a>, using the color scheme of code notes (see _Color schemes_ in <a class="reference-link" href="../../Note%20Types/Code.md">Code</a>).
A search that the app runs in the background, for example to filter a collection, or that a script runs is followed by a `Search:` line with its query and the number of notes it returned. The query is highlighted the same way as in a <a class="reference-link" href="../../Note%20Types/Saved%20Search.md">Saved Search</a>. Quick search and saved searches don't add this line.
@@ -18,7 +18,7 @@ Backend logs are stored on the file system. To find them, open the <a class="re
## In the terminal
-The server also prints every log entry to its standard output, which is what `docker logs` or `journalctl` show. When that output is a terminal, each entry starts with its time and is colored like the backend log in the app: request status codes by class, slow SQL queries, and errors in red. The statement of a slow SQL query and the query of a search are shown without syntax highlighting there. The startup banner with the Trilium logo and version information is colored too. Output to a file, a pipe or a container log stays plain text without the time, since those tools add their own.
+The server also prints every log entry to its standard output, which is what `docker logs` or `journalctl` show. When that output is a terminal, each entry starts with its time and is colored like the backend log in the app: request status codes by class, slow SQL queries, warnings in yellow and errors in red. The statement of a slow SQL query and the query of a search are shown without syntax highlighting there. The startup banner with the Trilium logo and version information is colored too. Output to a file, a pipe or a container log stays plain text without the time, since those tools add their own.
To turn the colors off in a terminal, set the `NO_COLOR` environment variable; to force them on elsewhere, set `FORCE_COLOR=1`.
diff --git a/packages/codemirror/src/extensions/trilium_log_highlighter.spec.ts b/packages/codemirror/src/extensions/trilium_log_highlighter.spec.ts
index 75315ebde4..815a4bd372 100644
--- a/packages/codemirror/src/extensions/trilium_log_highlighter.spec.ts
+++ b/packages/codemirror/src/extensions/trilium_log_highlighter.spec.ts
@@ -207,6 +207,20 @@ describe("triliumLogHighlighter", () => {
expect(lineClassesAt(s, 5)).toContain("cm-log-info");
});
+ it("colours a WARNING: block and its continuation lines, closing it at the next entry", () => {
+ const doc = [
+ "17:34:08.519 WARNING: shaky",
+ " at foo (bar.ts:1:2)",
+ "17:34:08.600 Becca (note cache) load took 120ms"
+ ].join("\n");
+ const s = state(doc);
+
+ expect(lineClassesAt(s, 1)).toContain("cm-log-warning");
+ expect(lineClassesAt(s, 2)).toContain("cm-log-warning");
+ expect(lineClassesAt(s, 3)).not.toContain("cm-log-warning");
+ expect(lineClassesAt(s, 1)).not.toContain("cm-log-error");
+ });
+
it("rebuilds decorations when the document changes (editable notes)", () => {
let s = state("17:35:56.745 Backend script execution is DISABLED.");
expect(classesOfState(s)).toEqual([ "cm-log-timestamp" ]);
diff --git a/packages/codemirror/src/extensions/trilium_log_highlighter.ts b/packages/codemirror/src/extensions/trilium_log_highlighter.ts
index 9feb4ce02d..78b2f71e13 100644
--- a/packages/codemirror/src/extensions/trilium_log_highlighter.ts
+++ b/packages/codemirror/src/extensions/trilium_log_highlighter.ts
@@ -31,6 +31,7 @@ import { decorateSearchQuery, searchHighlightTheme } from "./trilium_search_high
* request: the verb bolded, the query highlighted as search syntax and the count muted
* - `<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> WARNING: …` lines — coloured amber, with their continuation lines like errors
* - `<ts> JS Info: …` lines (and their continuation lines) — placeholder class, no colour yet
*/
@@ -45,6 +46,7 @@ const HTTP_REQUEST = /^\d{2}:\d{2}:\d{2}\.\d{3} (Slow )?(\d{3}) (GET|POST|PUT|PA
/** 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 WARNING_LINE = /^\d{2}:\d{2}:\d{2}\.\d{3} WARNING:/;
const INFO_LINE = /^\d{2}:\d{2}:\d{2}\.\d{3} JS Info:/;
// Slow SQL queries (logged when a query takes >= 20ms). Two shapes:
// `Slow query took 78ms: SELECT …` — the statement follows, whitespace-normalised to one line
@@ -77,6 +79,7 @@ const searchCountMark = Decoration.mark({ class: "cm-log-search-count" });
const httpLine = Decoration.line({ class: "cm-log-http" });
const queryLine = Decoration.line({ class: "cm-log-query" });
const errorLine = Decoration.line({ class: "cm-log-error" });
+const warningLine = Decoration.line({ class: "cm-log-warning" });
const infoLine = Decoration.line({ class: "cm-log-info" });
const statusMarks: Record<string, Decoration> = {
"2": Decoration.mark({ class: "cm-log-status cm-log-status-2xx" }),
@@ -118,6 +121,10 @@ const logHighlightTheme = EditorView.baseTheme({
// the way `.cm-log-http` is.
backgroundColor: "var(--log-error-line-background-color, transparent)"
},
+ ".cm-log-warning": {
+ color: "var(--log-warning-color, #d29922)",
+ backgroundColor: "var(--log-warning-line-background-color, transparent)"
+ },
// Placeholder: no colour yet — set `--log-info-color` to activate.
".cm-log-info": { color: "var(--log-info-color, inherit)" }
});
@@ -227,11 +234,12 @@ function sqlTokenMark(classes: string) {
/**
* Returns the line decoration a multi-line block entry applies to itself and its continuation lines
- * (e.g. `errorLine` for `JS Error:`/`ERROR:`, `infoLine` for `JS Info:`), or null for entries that
+ * (e.g. `errorLine` for `JS Error:`/`ERROR:`, `warningLine` for `WARNING:`, `infoLine` for `JS Info:`), or null for entries that
* don't span lines. `null` for continuation lines, which carry no timestamp.
*/
function blockLineFor(text: string): Decoration | null {
if (ERROR_LINE.test(text)) return errorLine;
+ if (WARNING_LINE.test(text)) return warningLine;
if (INFO_LINE.test(text)) return infoLine;
return null;
}
diff --git a/packages/trilium-core/src/services/file_based_log.spec.ts b/packages/trilium-core/src/services/file_based_log.spec.ts
index 43b774bbb7..b8ee1e73e1 100644
--- a/packages/trilium-core/src/services/file_based_log.spec.ts
+++ b/packages/trilium-core/src/services/file_based_log.spec.ts
@@ -123,6 +123,17 @@ describe("FileBasedLogService (shared logic)", () => {
expect(contents).toContain("plain failure");
});
+ it("warn() writes a WARNING: entry, with the stack of an Error", async () => {
+ await log.initialize();
+ log.warn("careful");
+ log.warn(new Error("shaky"));
+
+ const contents = log.getLogContents() ?? "";
+ expect(contents).toMatch(/\d{2}:\d{2}:\d{2}\.\d{3} WARNING: careful/);
+ expect(contents).toContain("WARNING: Error: shaky\n at ");
+ expect(consoleLogSpy).toHaveBeenCalledWith("WARNING: careful");
+ });
+
it("getScriptContext returns undefined when the context lookup throws", () => {
vi.spyOn(cls, "getContext").mockImplementation(() => {
throw new Error("no context");
diff --git a/packages/trilium-core/src/services/file_based_log.ts b/packages/trilium-core/src/services/file_based_log.ts
index 71f860b372..ee292eee8a 100644
--- a/packages/trilium-core/src/services/file_based_log.ts
+++ b/packages/trilium-core/src/services/file_based_log.ts
@@ -185,11 +185,12 @@ export default abstract class FileBasedLogService extends LogService {
this.log(message);
}
+ override warn(message: string | Error | unknown): void {
+ this.log(`WARNING: ${stringifyMessage(message)}`);
+ }
+
override error(message: string | Error | unknown): void {
- const str = message instanceof Error
- ? message.stack || message.message
- : String(message);
- this.log(`ERROR: ${str}`);
+ this.log(`ERROR: ${stringifyMessage(message)}`);
}
override getLogContents(): string | null {
@@ -220,3 +221,9 @@ export default abstract class FileBasedLogService extends LogService {
return `${this.pad(this.todaysMidnight.getFullYear())}-${this.pad(this.todaysMidnight.getMonth() + 1)}-${this.pad(this.todaysMidnight.getDate())}`;
}
}
+
+function stringifyMessage(message: string | Error | unknown): string {
+ return message instanceof Error
+ ? message.stack || message.message
+ : String(message);
+}
diff --git a/packages/trilium-core/src/services/icon_packs.spec.ts b/packages/trilium-core/src/services/icon_packs.spec.ts
index 81d0891163..3f23cc5ac1 100644
--- a/packages/trilium-core/src/services/icon_packs.spec.ts
+++ b/packages/trilium-core/src/services/icon_packs.spec.ts
@@ -229,18 +229,18 @@ describe("Generating CSS for untrusted manifests", () => {
});
it("reports the skipped icon keys of a pack in a single log line", () => {
- const errorSpy = vi.spyOn(getLog(), "error").mockImplementation(() => {});
+ const warnSpy = vi.spyOn(getLog(), "warn").mockImplementation(() => {});
generateCssFor({
- "un-ok": { glyph: "", terms: [ "ok" ] },
- "un-a,b": { glyph: "", terms: [ "a" ] },
- "un-c,d": { glyph: "", terms: [ "c" ] },
- "un e": { glyph: "", terms: [ "e" ] }
+ "un-ok": { glyph: "\ue9c2", terms: [ "ok" ] },
+ "un-a,b": { glyph: "\uec92", terms: [ "a" ] },
+ "un-c,d": { glyph: "\uec92", terms: [ "c" ] },
+ "un e": { glyph: "\uec92", terms: [ "e" ] }
});
- expect(errorSpy).toHaveBeenCalledOnce();
- expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("Skipping 3 icons of icon pack 'un'"));
- expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("'un-a,b', 'un-c,d', 'un e'"));
- errorSpy.mockRestore();
+ expect(warnSpy).toHaveBeenCalledOnce();
+ expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("Skipping 3 icons of icon pack 'un'"));
+ expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("'un-a,b', 'un-c,d', 'un e'"));
+ warnSpy.mockRestore();
});
it("keeps non-ASCII icon keys, which are valid CSS class names", () => {
diff --git a/packages/trilium-core/src/services/icon_packs.ts b/packages/trilium-core/src/services/icon_packs.ts
index 278bf2051e..4370409130 100644
--- a/packages/trilium-core/src/services/icon_packs.ts
+++ b/packages/trilium-core/src/services/icon_packs.ts
@@ -120,7 +120,7 @@ export function getIconPacks() {
if (!iconPack) return false;
if (usedPrefixes.has(iconPack.prefix)) {
- getLog().info(`Skipping icon pack with duplicate prefix '${iconPack.prefix}': ${iconPack.title} (${iconPack.manifestNoteId})`);
+ getLog().warn(`Skipping icon pack with duplicate prefix '${iconPack.prefix}': ${iconPack.title} (${iconPack.manifestNoteId})`);
return false;
}
usedPrefixes.add(iconPack.prefix);
@@ -251,7 +251,7 @@ export function generateCss({ manifest, fontMime, builtin, fontAttachmentId, pre
if (skippedKeys.length) {
const examples = skippedKeys.slice(0, MAX_LOGGED_SKIPPED_KEYS).map((key) => `'${key}'`).join(", ");
const rest = skippedKeys.length > MAX_LOGGED_SKIPPED_KEYS ? ", …" : "";
- getLog().error(`Skipping ${skippedKeys.length} icons of icon pack '${prefix}' (${examples}${rest}): keys allow only letters, digits, dashes, underscores and non-ASCII characters.`);
+ getLog().warn(`Skipping ${skippedKeys.length} icons of icon pack '${prefix}' (${examples}${rest}): keys allow only letters, digits, dashes, underscores and non-ASCII characters.`);
}
const fontFamily = builtin ? fontAttachmentId : `trilium-icon-pack-${prefix}`;
diff --git a/packages/trilium-core/src/services/log.spec.ts b/packages/trilium-core/src/services/log.spec.ts
index 509b9b8f4b..b7e11f0d53 100644
--- a/packages/trilium-core/src/services/log.spec.ts
+++ b/packages/trilium-core/src/services/log.spec.ts
@@ -8,18 +8,21 @@ describe("LogService (base)", () => {
// directly and assert it forwards to the console.
afterEach(() => vi.restoreAllMocks());
- it("forwards log/info/error to the console", () => {
+ it("forwards log/info/warn/error to the console", () => {
const service = new LogService();
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
const infoSpy = vi.spyOn(console, "info").mockImplementation(() => {});
+ const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
service.log("hello");
service.info("info-msg");
+ service.warn("careful");
service.error("boom");
expect(logSpy).toHaveBeenCalledWith("hello");
expect(infoSpy).toHaveBeenCalledWith("info-msg");
+ expect(warnSpy).toHaveBeenCalledWith("WARNING: ", "careful");
expect(errorSpy).toHaveBeenCalledWith("ERROR: ", "boom");
});
diff --git a/packages/trilium-core/src/services/log.ts b/packages/trilium-core/src/services/log.ts
index 78095a224e..ee0cf35df9 100644
--- a/packages/trilium-core/src/services/log.ts
+++ b/packages/trilium-core/src/services/log.ts
@@ -8,6 +8,10 @@ export default class LogService {
console.info(message);
}
+ warn(message: string | Error | unknown) {
+ console.warn("WARNING: ", message);
+ }
+
error(message: string | Error | unknown) {
console.error("ERROR: ", message);
}