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
feat(server): color the startup banner in the terminal
Elian Dorane3bcfd4d68f5
diff --git a/apps/server/src/assets/help/help_content.json b/apps/server/src/assets/help/help_content.jsonindex deb3570991..32d50ca9a1 100644--- a/apps/server/src/assets/help/help_content.json+++ b/apps/server/src/assets/help/help_content.json@@ -188,7 +188,7 @@"_help_wy8So3yZZlH9": "<p>Go to <a href=\"https://github.com/TriliumNext/Trilium/issues\">Issues · TriliumNext/Trilium</a> and press “New issue”.</p><p>If you are reporting a bug, select “Bug Report” and fill in the details.</p>","_help_x59R8J8KV5Bp": "<p><img src=\"{{helpAssets}}/User%20Guide/Troubleshooting/Anonymized%20Database_image.jpg\"></p><p>In certain scenarios, understanding the structure of a database is crucial for troubleshooting issues. However, sharing your actual <a href=\"#root/_help_wX4HbRucYSDD\">database</a> file with personal notes is not advisable. To address this, Trilium offers a feature to anonymize the database. This feature can be accessed via Menu -> Options -> Advanced tab.</p><p>This feature creates a copy of your database with all sensitive data removed. Specifically, it strips out note titles, contents, revisions, history, and some non-system attributes while retaining the overall structure and metadata, such as modification dates. After anonymization, the database undergoes a <a href=\"https://sqlite.org/lang_vacuum.html\">vacuuming process</a> to ensure no sensitive data remnants remain in the file. The anonymized database is saved in the <code>anonymized</code> directory within the <a href=\"#root/_help_tAassRL4RSQL\">data directory</a>, making it safe to share with bug reports.</p><p>This will create a copy of your document and remove all sensitive data (currently note titles, contents, revisions, history and some of the options, and non-system attributes) while leaving all structure and metadata (e.g. date of last change). After this is done, the database is <a href=\"https://sqlite.org/lang_vacuum.html\">VACUUMed</a> to make sure there's no stale sensitive data in the document file. The resulting file is stored in <code>anonymized</code> directory (placed in the <a href=\"#root/_help_tAassRL4RSQL\">data directory</a>). You can safely attach it to a bug report.</p><h2>Command Line Anonymization</h2><p>If your <a href=\"#root/_help_wX4HbRucYSDD\">database</a> is corrupted to the point where Trilium cannot start, the anonymization process can still be executed via the command line:</p><pre><code class=\"language-text-x-trilium-auto\">node src/anonymize.js</code></pre><p>Run this command from the directory containing Trilium's source files, typically found in the <code>resources/app</code> directory for desktop builds.</p>","_help_qzNzp9LYQyPT": "<p>It's important to provide all available error logs together with bug reports.</p><p>There are two types of error logs, both of which are useful when reporting bugs.</p><ul><li><a class=\"reference-link\" href=\"#root/_help_bnyigUA2UK7s\">Backend (server) logs</a></li><li><a class=\"reference-link\" href=\"#root/_help_9yEHzMyFirZR\">Frontend logs</a></li></ul><h2>Providing sensitive data</h2><p>If you don't feel comfortable attaching the logs or anything sensitive to the public GitHub issues, feel free to contact the devs in our Matrix <a href=\"https://github.com/TriliumNext/Trilium#-discuss-with-us\">support channel</a>.</p><p>Use this email to also provide anything which could assist in analysing the bug - e.g. files/images/ZIPs being imported or <a href=\"#root/_help_x59R8J8KV5Bp\">anonymized database</a>.</p><h3>Exporting note subtree for reproduction</h3><p>Often times, bugs manifest themselves in specific notes and having them would greatly ease reproduction and fixing.</p><p>In such case, please export the relevant note subtree by right-clicking it on the left tree, choosing Export - HTML as ZIP:</p><p><img src=\"{{helpAssets}}/User%20Guide/Troubleshooting/Error%20logs_error-logs-export-subtree.png\"></p>",- "_help_bnyigUA2UK7s": "<h2>Accessing via the backend log</h2><p>In the <a class=\"reference-link\" href=\"#root/_help_x3i7MxGccDuM\">Global menu</a>, go to <em>Advanced</em> → <em>Show backend log</em>. This will display the current backend log (i.e. today's), with the historical information available only on disk (see below).</p><p>Interaction since v0.104.0:</p><ul><li>The file can be downloaded as a text file using the <span class=\"tn-icon bx bx-download\"></span> button in the <a class=\"reference-link\" href=\"#root/_help_8YBEPzcpUgxw\">Note menu</a> area.</li><li>The file can also be searched just like a normal <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> note.</li></ul><h2>Location on the disk</h2><p>Backend logs are stored on the file system. To find them, open the <a class=\"reference-link\" href=\"#root/_help_tAassRL4RSQL\">Data directory</a>, go to the <code>log</code> subdirectory and find the latest log file, e.g. <code>trilium-2022-12-14.log</code>. </p><h2>In the terminal</h2><p>The server also prints every log entry to its standard output, which is what <code>docker logs</code> or <code>journalctl</code> show. When that output is a terminal, each entry starts with its time and is colored the same way as the backend log in the app: request status codes by class, slow SQL queries, and errors in red. Output to a file, a pipe or a container log stays plain text without the time, since those tools add their own.</p><p>To turn the colors off in a terminal, set the <code>NO_COLOR</code> environment variable; to force them on elsewhere, set <code>FORCE_COLOR=1</code>.</p><h2>Reporting backend bugs</h2><p>You can attach the whole file to the bug report (preferable) or open it and copy-paste only the last lines / lines you believe are relevant.</p><h2>Customizing the retention of logs</h2><p>The backend logs are fully managed by the Trilium server. By default the last 90 days worth of logs are kept; the logs older than that are deleted in order to reduce the space consumption.</p><p>It's possible to change the retention period by modifying the <a class=\"reference-link\" href=\"#root/_help_Gzjqa934BdH4\">Configuration (config.ini or environment variables)</a> via the <code>.ini</code> file:</p><pre><code class=\"language-text-x-trilium-auto\">[Logging]\nretentionDays=7</code></pre><p>Or via the environment variable <code>TRILIUM_LOGGING_RETENTION_DAYS</code>.</p><p>Special cases:</p><ul><li>Positive values indicate the number of days worth of logs to keep</li><li>A value of 0 results with the default value (90 days) to be used</li><li>Negative values (e.g. <code>-1</code>) result with all logs to be kept, irrespective how ancient and numerous (and</li></ul><aside class=\"admonition note\"><p>If you set the retention days to a low number, you might notice that not all the log files are being deleted. This is because a minimum number of logs (7 at the time of writing) is maintained at all times.</p></aside>",+ "_help_bnyigUA2UK7s": "<h2>Accessing via the backend log</h2><p>In the <a class=\"reference-link\" href=\"#root/_help_x3i7MxGccDuM\">Global menu</a>, go to <em>Advanced</em> → <em>Show backend log</em>. This will display the current backend log (i.e. today's), with the historical information available only on disk (see below).</p><p>Interaction since v0.104.0:</p><ul><li>The file can be downloaded as a text file using the <span class=\"tn-icon bx bx-download\"></span> button in the <a class=\"reference-link\" href=\"#root/_help_8YBEPzcpUgxw\">Note menu</a> area.</li><li>The file can also be searched just like a normal <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> note.</li></ul><h2>Location on the disk</h2><p>Backend logs are stored on the file system. To find them, open the <a class=\"reference-link\" href=\"#root/_help_tAassRL4RSQL\">Data directory</a>, go to the <code>log</code> subdirectory and find the latest log file, e.g. <code>trilium-2022-12-14.log</code>. </p><h2>In the terminal</h2><p>The server also prints every log entry to its standard output, which is what <code>docker logs</code> or <code>journalctl</code> show. When that output is a terminal, each entry starts with its time and is colored the same way as the backend log in the app: request status codes by class, slow SQL queries, and errors in red. 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.</p><p>To turn the colors off in a terminal, set the <code>NO_COLOR</code> environment variable; to force them on elsewhere, set <code>FORCE_COLOR=1</code>.</p><h2>Reporting backend bugs</h2><p>You can attach the whole file to the bug report (preferable) or open it and copy-paste only the last lines / lines you believe are relevant.</p><h2>Customizing the retention of logs</h2><p>The backend logs are fully managed by the Trilium server. By default the last 90 days worth of logs are kept; the logs older than that are deleted in order to reduce the space consumption.</p><p>It's possible to change the retention period by modifying the <a class=\"reference-link\" href=\"#root/_help_Gzjqa934BdH4\">Configuration (config.ini or environment variables)</a> via the <code>.ini</code> file:</p><pre><code class=\"language-text-x-trilium-auto\">[Logging]\nretentionDays=7</code></pre><p>Or via the environment variable <code>TRILIUM_LOGGING_RETENTION_DAYS</code>.</p><p>Special cases:</p><ul><li>Positive values indicate the number of days worth of logs to keep</li><li>A value of 0 results with the default value (90 days) to be used</li><li>Negative values (e.g. <code>-1</code>) result with all logs to be kept, irrespective how ancient and numerous (and</li></ul><aside class=\"admonition note\"><p>If you set the retention days to a low number, you might notice that not all the log files are being deleted. This is because a minimum number of logs (7 at the time of writing) is maintained at all times.</p></aside>","_help_9yEHzMyFirZR": "<p>To provide frontend logs, we need to open the Developer Console. Often the easiest way is to press <kbd>Ctrl</kbd>-<kbd>Shift</kbd>-<kbd>I</kbd> which should work in most browsers (and desktop app). Make sure that the error producing action happened right before you copy&paste the errors, the console is cleared on app restart.</p><p>If that doesn't work, then:</p><ul><li>in Trilium desktop app, go to top-left menu button -> Advanced -> Open dev tools</li><li>In Firefox/Chrome right-click anywhere in the page and click Inspect:</li></ul><p><img src=\"{{helpAssets}}/User%20Guide/Troubleshooting/Error%20logs/Frontend%20logs_error-logs-firefox-context-men.png\"></p><p>Once you have Dev Tools open, click on \"Console\" tab:</p><p><img src=\"{{helpAssets}}/User%20Guide/Troubleshooting/Error%20logs/Frontend%20logs_image.jpg\"></p><p>Copy-paste (or screenshot) the logs. It's better to provide not just errors, but the whole log, which might provide context while analyzing the bug.</p>","_help_vdlYGAcpXAgc": "<p>Synchronization can sometimes take a long amount of time in order to compute the items that require update. When running behind a reverse proxy, the request can time out.</p><p>The solution is to increase the timeout at proxy level.</p><h2>Nginx</h2><p>Add the following to the configuration file:</p><pre><code class=\"language-text-x-nginx-conf\">proxy_connect_timeout 300;\nproxy_send_timeout 300;\nproxy_read_timeout 300;\nsend_timeout 300;</code></pre><p>And restart the server.</p><p>See <a href=\"#root/_help_ud6MShXL4WpO\">Nginx Proxy Setup</a> for more information about the Nginx setup.</p><p>If it still doesn't work, try increasing the timeout.</p>","_help_s8alTXmpFR61": "<p>Some changes to the application will not take effect immediately and as such it might require to manually reload or refresh the application.</p><p>To do so, simply press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>R</kbd>.</p>",diff --git a/apps/server/src/log_provider.spec.ts b/apps/server/src/log_provider.spec.tsindex 627fd27b2f..ddd3c5f548 100644--- a/apps/server/src/log_provider.spec.ts+++ b/apps/server/src/log_provider.spec.ts@@ -2,7 +2,7 @@ import type { Request, Response } from "express";import { styleText } from "node:util";import { describe, expect, it, vi } from "vitest";-import ServerLogService, { colorizeLogEntry } from "./log_provider.js";+import ServerLogService, { colorizeLogEntry, TRILIUM_LOGO } from "./log_provider.js";describe("colorizeLogEntry", () => {const ts = paint("gray", "12:34:56.789");@@ -37,6 +37,19 @@ describe("colorizeLogEntry", () => {}});+ it("colours the startup intro without timestamping the logo or blank lines", () => {+ expect(colorizeLogEntry(`\n${TRILIUM_LOGO} 0.99.1\n`, "12:34:56.789")).toBe(+ `\n${paint([ "bold", "green" ], TRILIUM_LOGO)} ${paint("gray", "0.99.1")}\n`+ );+ expect(colorizeLogEntry("", "12:34:56.789")).toBe("");+ expect(colorizeLogEntry("📦 Versions: app=0.99.1 db=233", "12:34:56.789")).toBe(+ `${ts} ${paint("gray", "📦 Versions:")} app=0.99.1 db=233`+ );++ const warning = "WARNING: Backend script execution is ENABLED.";+ expect(colorizeLogEntry(warning, "12:34:56.789")).toBe(`${ts} ${paint("yellow", warning)}`);+ });+it("leaves other entries uncoloured", () => {for (const message of [ "JS Info: hello", "Becca (note cache) load took 120ms", "[Script abc] 200 GET /x" ]) {expect(colorizeLogEntry(message, "12:34:56.789")).toBe(`${ts} ${message}`);diff --git a/apps/server/src/log_provider.ts b/apps/server/src/log_provider.tsindex ac709f8722..f89e4d2bb7 100644--- a/apps/server/src/log_provider.ts+++ b/apps/server/src/log_provider.ts@@ -10,6 +10,14 @@ import dataDir from "./services/data_dir.js";const LOG_FILE_PATTERN = /^trilium-\d{4}-\d{2}-\d{2}\.log$/;+/** The ASCII-art banner `www.ts` prints at startup, followed by the version. */+export const TRILIUM_LOGO = `\+ _____ _ _ _+|_ _| __(_) (_)_ _ _ __ ___ | \\ | | ___ | |_ ___ ___+ | || '__| | | | | | | '_ \` _ \\ | \\| |/ _ \\| __/ _ \\/ __|+ | || | | | | | |_| | | | | | | | |\\ | (_) | || __/\\__ \\+ |_||_| |_|_|_|\\__,_|_| |_| |_| |_| \\_|\\___/ \\__\\___||___/`;+const requestBlacklist = ["/app","/images",@@ -159,6 +167,10 @@ const URL_STYLE: Style = "cyan";const QUERY_VERB_STYLE: Style = [ "bold", "magenta" ];const SQL_STYLE: Style = "magentaBright";const ERROR_STYLE: Style = "red";+const WARNING_STYLE: Style = "yellow";+// The green of the leaves in `icon-color.svg`.+const LOGO_STYLE: Style = [ "bold", "green" ];+const MUTED_STYLE: Style = "gray";const STATUS_STYLES: Record<string, Style> = {"2": [ "bold", "green" ],"3": [ "bold", "gray" ],@@ -169,9 +181,17 @@ const STATUS_STYLES: Record<string, Style> = {const HTTP_REQUEST = /^(Slow )?(\d{3}) (GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS) (\S+)/;const SLOW_QUERY = /^(Slow (?:recursive )?query)( took \d+ms)(?:(: )(.+))?/;const ERROR_ENTRY = /^(?:JS Error|ERROR):/;+const WARNING_ENTRY = /^WARNING:/;+// The `📦 Versions: …` rows of the startup intro.+const INTRO_ROW = /^\p{Extended_Pictographic}️? [^:]+:/u;+const LOGO_PREFIX = `\n${TRILIUM_LOGO} `;/** Prefixes `message` with its `timestamp` and colours both the way the Backend Log note does. */export function colorizeLogEntry(message: string, timestamp: string): string {+ // The logo and the blank line after the intro start their own lines.+ if (!message || message.startsWith("\n")) {+ return colorizeMessage(message);+ }return `${paint(TIMESTAMP_STYLE, timestamp)} ${colorizeMessage(message)}`;}@@ -199,7 +219,20 @@ function colorizeMessage(message: string): string {].join("");}- return ERROR_ENTRY.test(message) ? paint(ERROR_STYLE, message) : message;+ if (message.startsWith(LOGO_PREFIX)) {+ const version = message.slice(LOGO_PREFIX.length).trimEnd();+ return `\n${paint(LOGO_STYLE, TRILIUM_LOGO)} ${paint(MUTED_STYLE, version)}\n`;+ }++ const introRow = INTRO_ROW.exec(message);+ if (introRow) {+ return paint(MUTED_STYLE, introRow[0]) + message.slice(introRow[0].length);+ }++ if (ERROR_ENTRY.test(message)) {+ return paint(ERROR_STYLE, message);+ }+ return WARNING_ENTRY.test(message) ? paint(WARNING_STYLE, message) : message;}function paint(style: Style, text: string): string {diff --git a/apps/server/src/www.ts b/apps/server/src/www.tsindex 37d6b99612..77a4ece31f 100644--- a/apps/server/src/www.ts+++ b/apps/server/src/www.ts@@ -7,6 +7,7 @@ import { networkInterfaces } from "os";import tmp from "tmp";import buildApp from "./app.js";+import { TRILIUM_LOGO } from "./log_provider.js";import config from "./services/config.js";import dataDir from "./services/data_dir.js";import { startCpuProfiler, writeCpuProfile } from "./services/cpu_profiler.js";@@ -24,14 +25,6 @@ import { isHttpAttachableMessagingProvider } from "./services/ws_messaging_proviconst MINIMUM_NODE_VERSION = "20.0.0";-const LOGO = `\- _____ _ _ _-|_ _| __(_) (_)_ _ _ __ ___ | \\ | | ___ | |_ ___ ___- | || '__| | | | | | | '_ \` _ \\ | \\| |/ _ \\| __/ _ \\/ __|- | || | | | | | |_| | | | | | | | |\\ | (_) | || __/\\__ \\- |_||_| |_|_|_|\\__,_|_| |_| |_| |_| \\_|\\___/ \\__\\___||___/ [version]-`;-export default async function startTriliumServer(): Promise<Express> {await displayStartupMessage();@@ -105,7 +98,7 @@ export default async function startTriliumServer(): Promise<Express> {}async function displayStartupMessage() {- getLog().info(`\n${LOGO.replace("[version]", appInfo.appVersion)}`);+ getLog().info(`\n${TRILIUM_LOGO} ${appInfo.appVersion}\n`);getLog().info(`📦 Versions: app=${appInfo.appVersion} db=${appInfo.dbVersion} sync=${appInfo.syncVersion} clipper=${appInfo.clipperProtocolVersion}`);getLog().info(`🔧 Build: ${utils.formatUtcTime(appInfo.buildDate)} (${appInfo.buildRevision.substring(0, 10)})`);getLog().info(`📂 Data dir: ${appInfo.dataDirectory}`);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.mdindex d9f09911d9..ef8cd11f71 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@@ -14,7 +14,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 the same way as the backend log in the app: request status codes by class, slow SQL queries, and errors in red. 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 the same way as the backend log in the app: request status codes by class, slow SQL queries, and errors in red. 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`.