From 06ebcc210e47a5e97386d7af9d1253889f31406d Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Sat, 11 Jan 2025 00:54:10 +0100 Subject: [PATCH 1/7] refactor(backend_log): use async readFile using synchronous functions on the backend is not recommended, as it is "blocking the event loop", i.e. no other tasks get executed/processed, while the file is being read --- src/routes/api/backend_log.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/routes/api/backend_log.ts b/src/routes/api/backend_log.ts index f57886242..ae2fec492 100644 --- a/src/routes/api/backend_log.ts +++ b/src/routes/api/backend_log.ts @@ -1,15 +1,15 @@ "use strict"; -import fs from "fs"; +import { readFile } from "fs/promises"; import dateUtils from "../../services/date_utils.js"; import dataDir from "../../services/data_dir.js"; const { LOG_DIR } = dataDir; -function getBackendLog() { +async function getBackendLog() { const file = `${LOG_DIR}/trilium-${dateUtils.localNowDate()}.log`; try { - return fs.readFileSync(file, "utf8"); + return await readFile(file, "utf8"); } catch (e) { // most probably the log file does not exist yet - https://github.com/zadam/trilium/issues/1977 return ""; From eb4b5a44df780ec6be96a6b888266a9978aa55a0 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Sat, 11 Jan 2025 01:06:13 +0100 Subject: [PATCH 2/7] refactor(backend_log): use path.join for log file path --- src/routes/api/backend_log.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/api/backend_log.ts b/src/routes/api/backend_log.ts index ae2fec492..a3ad441b4 100644 --- a/src/routes/api/backend_log.ts +++ b/src/routes/api/backend_log.ts @@ -1,14 +1,14 @@ "use strict"; import { readFile } from "fs/promises"; +import { join } from "path"; import dateUtils from "../../services/date_utils.js"; import dataDir from "../../services/data_dir.js"; const { LOG_DIR } = dataDir; async function getBackendLog() { - const file = `${LOG_DIR}/trilium-${dateUtils.localNowDate()}.log`; - try { + const file = join(LOG_DIR, `trilium-${dateUtils.localNowDate()}.log`); return await readFile(file, "utf8"); } catch (e) { // most probably the log file does not exist yet - https://github.com/zadam/trilium/issues/1977 From c4ad84ab061d4277528e2b00244f4f619f4a7a09 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Sat, 11 Jan 2025 01:14:16 +0100 Subject: [PATCH 3/7] refactor(backend_log): print error to the log --- src/routes/api/backend_log.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/routes/api/backend_log.ts b/src/routes/api/backend_log.ts index a3ad441b4..318f894c6 100644 --- a/src/routes/api/backend_log.ts +++ b/src/routes/api/backend_log.ts @@ -4,6 +4,8 @@ import { readFile } from "fs/promises"; import { join } from "path"; import dateUtils from "../../services/date_utils.js"; import dataDir from "../../services/data_dir.js"; +import log from "../../services/log.js"; + const { LOG_DIR } = dataDir; async function getBackendLog() { @@ -11,6 +13,8 @@ async function getBackendLog() { const file = join(LOG_DIR, `trilium-${dateUtils.localNowDate()}.log`); return await readFile(file, "utf8"); } catch (e) { + log.error((e instanceof Error) ? e : "Reading the Backend Log failed with an unknown error."); + // most probably the log file does not exist yet - https://github.com/zadam/trilium/issues/1977 return ""; } From 67d858441ae0ad1101cae557b8792ede69c62443 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Sat, 11 Jan 2025 01:45:53 +0100 Subject: [PATCH 4/7] refactor(backend_log): include filename in log --- src/routes/api/backend_log.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/routes/api/backend_log.ts b/src/routes/api/backend_log.ts index 318f894c6..acb6d3271 100644 --- a/src/routes/api/backend_log.ts +++ b/src/routes/api/backend_log.ts @@ -9,11 +9,12 @@ import log from "../../services/log.js"; const { LOG_DIR } = dataDir; async function getBackendLog() { + const fileName = `trilium-${dateUtils.localNowDate()}.log` try { - const file = join(LOG_DIR, `trilium-${dateUtils.localNowDate()}.log`); + const file = join(LOG_DIR, fileName); return await readFile(file, "utf8"); } catch (e) { - log.error((e instanceof Error) ? e : "Reading the Backend Log failed with an unknown error."); + log.error((e instanceof Error) ? e : `Reading the backend log '${fileName}' failed with an unknown error.`); // most probably the log file does not exist yet - https://github.com/zadam/trilium/issues/1977 return ""; From dcfdb67539bcf67d817fa2a75d1099e148fee26e Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Sat, 11 Jan 2025 13:30:39 +0100 Subject: [PATCH 5/7] refactor(backend_log): improve handle 'file not found' handle errors more "user friendly" and actually let the user know, that either the file is not existing (yet), or that reading the log failed. --- src/routes/api/backend_log.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/routes/api/backend_log.ts b/src/routes/api/backend_log.ts index acb6d3271..a20e14ebd 100644 --- a/src/routes/api/backend_log.ts +++ b/src/routes/api/backend_log.ts @@ -14,10 +14,16 @@ async function getBackendLog() { const file = join(LOG_DIR, fileName); return await readFile(file, "utf8"); } catch (e) { - log.error((e instanceof Error) ? e : `Reading the backend log '${fileName}' failed with an unknown error.`); + const isErrorInstance = e instanceof Error; // most probably the log file does not exist yet - https://github.com/zadam/trilium/issues/1977 - return ""; + if (isErrorInstance && "code" in e && e.code === "ENOENT") { + log.error(e); + return `The backend log file '${fileName}' does not exist (yet).`; + } + + log.error(isErrorInstance ? e : `Reading the backend log '${fileName}' failed with an unknown error: '${e}'.`); + return `Reading the backend log '${fileName}' failed.`; } } From 903988fec5e0d5cb1457688f1b8a03bfbef5efd2 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Sat, 11 Jan 2025 13:41:32 +0100 Subject: [PATCH 6/7] i18n(backend_log): translate messages --- src/routes/api/backend_log.ts | 5 +++-- translations/de/server.json | 4 ++++ translations/en/server.json | 4 ++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/routes/api/backend_log.ts b/src/routes/api/backend_log.ts index a20e14ebd..4c45877fe 100644 --- a/src/routes/api/backend_log.ts +++ b/src/routes/api/backend_log.ts @@ -5,6 +5,7 @@ import { join } from "path"; import dateUtils from "../../services/date_utils.js"; import dataDir from "../../services/data_dir.js"; import log from "../../services/log.js"; +import { t } from "i18next"; const { LOG_DIR } = dataDir; @@ -19,11 +20,11 @@ async function getBackendLog() { // most probably the log file does not exist yet - https://github.com/zadam/trilium/issues/1977 if (isErrorInstance && "code" in e && e.code === "ENOENT") { log.error(e); - return `The backend log file '${fileName}' does not exist (yet).`; + return t("backend_log.log-does-not-exist", { fileName }); } log.error(isErrorInstance ? e : `Reading the backend log '${fileName}' failed with an unknown error: '${e}'.`); - return `Reading the backend log '${fileName}' failed.`; + return t("backend_log.reading-log-failed", { fileName }); } } diff --git a/translations/de/server.json b/translations/de/server.json index a782e3c70..41bf37a70 100644 --- a/translations/de/server.json +++ b/translations/de/server.json @@ -193,5 +193,9 @@ "test_sync": { "not-configured": "Der Synchronisations-Server-Host ist nicht konfiguriert. Bitte konfiguriere zuerst die Synchronisation.", "successful": "Die Server-Verbindung wurde erfolgreich hergestellt, die Synchronisation wurde gestartet." + }, + "backend_log": { + "log-does-not-exist": "Die Backend-Log-Datei '{{ fileName }}' existiert (noch) nicht.", + "reading-log-failed": "Das Lesen der Backend-Log-Datei '{{ fileName }}' ist fehlgeschlagen." } } diff --git a/translations/en/server.json b/translations/en/server.json index fd4aa19c8..2b1d4fb6b 100644 --- a/translations/en/server.json +++ b/translations/en/server.json @@ -246,5 +246,9 @@ "new-note": "New note", "duplicate-note-suffix": "(dup)", "duplicate-note-title": "{{ noteTitle }} {{ duplicateNoteSuffix }}" + }, + "backend_log": { + "log-does-not-exist": "The backend log file '{{ fileName }}' does not exist (yet).", + "reading-log-failed": "Reading the backend log file '{{ fileName }}' failed." } } From bcbf4f40900814f929da140507c510b425d42015 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Sat, 11 Jan 2025 14:04:44 +0100 Subject: [PATCH 7/7] chore: fix formatting --- src/routes/api/backend_log.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/api/backend_log.ts b/src/routes/api/backend_log.ts index 4c45877fe..2f056158d 100644 --- a/src/routes/api/backend_log.ts +++ b/src/routes/api/backend_log.ts @@ -1,6 +1,6 @@ "use strict"; -import { readFile } from "fs/promises"; +import { readFile } from "fs/promises"; import { join } from "path"; import dateUtils from "../../services/date_utils.js"; import dataDir from "../../services/data_dir.js"; @@ -10,7 +10,7 @@ import { t } from "i18next"; const { LOG_DIR } = dataDir; async function getBackendLog() { - const fileName = `trilium-${dateUtils.localNowDate()}.log` + const fileName = `trilium-${dateUtils.localNowDate()}.log`; try { const file = join(LOG_DIR, fileName); return await readFile(file, "utf8");