Skip to content

Build your personal knowledge base with Trilium Notes

got rid of simple-node-logger in favor of simple custom logger

zadamb245b249d349
diff --git a/package.json b/package.json
index 2e5c7ea3d8..c551fce270 100644
--- a/package.json
+++ b/package.json
@@ -68,7 +68,6 @@
"semver": "7.3.2",
"serve-favicon": "2.5.0",
"session-file-store": "1.4.0",
- "simple-node-logger": "18.12.24",
"string-similarity": "4.0.1",
"tar-stream": "2.1.2",
"turndown": "6.0.0",
diff --git a/src/entities/note.js b/src/entities/note.js
index b2d93add25..caac1885b2 100644
--- a/src/entities/note.js
+++ b/src/entities/note.js
@@ -123,7 +123,12 @@ class Note extends Entity {
throw new Error(`Cannot set null content to note ${this.noteId}`);
}
- content = Buffer.isBuffer(content) ? content : Buffer.from(content);
+ if (this.isStringNote()) {
+ content = content.toString();
+ }
+ else {
+ content = Buffer.isBuffer(content) ? content : Buffer.from(content);
+ }
// force updating note itself so that dateModified is represented correctly even for the content
this.forcedChange = true;
diff --git a/src/services/log.js b/src/services/log.js
index 9c2be41872..007d40cefd 100644
--- a/src/services/log.js
+++ b/src/services/log.js
@@ -7,30 +7,59 @@ if (!fs.existsSync(dataDir.LOG_DIR)) {
fs.mkdirSync(dataDir.LOG_DIR, 0o700);
}
-const logger = require('simple-node-logger').createRollingFileLogger({
- errorEventName: 'error',
- logDirectory: dataDir.LOG_DIR,
- fileNamePattern: 'trilium-<DATE>.log',
- dateFormat:'YYYY-MM-DD'
-});
+let logFile = null;
-function info(message) {
- // info messages are logged asynchronously
- setTimeout(() => {
- console.log(message);
+const SECOND = 1000;
+const MINUTE = 60 * SECOND;
+const HOUR = 60 * MINUTE;
+const DAY = 24 * HOUR;
+
+const NEW_LINE = process.platform === "win32" ? '\r\n' : '\n';
+
+let todaysMidnight = null;
+
+initLogFile();
+
+function getTodaysMidnight() {
+ const now = new Date();
- logger.info(message);
- }, 0);
+ return new Date(now.getFullYear(), now.getMonth(), now.getDate());
}
-function error(message) {
- message = "ERROR: " + message;
+function initLogFile() {
+ todaysMidnight = getTodaysMidnight();
+
+ const path = dataDir.LOG_DIR + '/trilium-' + formatDate() + '.log';
+
+ if (logFile) {
+ logFile.end();
+ }
+
+ logFile = fs.createWriteStream(path, {flags: 'a'});
+}
+
+function checkDate(millisSinceMidnight) {
+ if (millisSinceMidnight >= DAY) {
+ initLogFile();
+ }
+}
+
+function log(str) {
+ const millisSinceMidnight = Date.now() - todaysMidnight.getTime();
+
+ checkDate(millisSinceMidnight);
+
+ logFile.write(formatTime(millisSinceMidnight) + ' ' + str + NEW_LINE);
- // we're using .info() instead of .error() because simple-node-logger emits weird error for showError()
- // errors are logged synchronously to make sure it doesn't get lost in case of crash
- logger.info(message);
+ console.log(str);
+}
- console.trace(message);
+function info(message) {
+ log(message);
+}
+
+function error(message) {
+ log("ERROR: " + message);
}
const requestBlacklist = [ "/libraries", "/app", "/images", "/stylesheets" ];
@@ -46,11 +75,42 @@ function request(req) {
return;
}
- logger.info(req.method + " " + req.url);
+ info(req.method + " " + req.url);
+}
+
+function pad(num) {
+ num = Math.floor(num);
+
+ return num < 10 ? ("0" + num) : num.toString();
+}
+
+function padMilli(num) {
+ if (num < 10) {
+ return "00" + num;
+ }
+ else if (num < 100) {
+ return "0" + num;
+ }
+ else {
+ return num.toString();
+ }
+}
+
+function formatTime(millisSinceMidnight) {
+ return pad(millisSinceMidnight / HOUR)
+ + ":" + pad((millisSinceMidnight % HOUR) / MINUTE)
+ + ":" + pad((millisSinceMidnight % MINUTE) / SECOND)
+ + "." + padMilli(millisSinceMidnight % SECOND);
+}
+
+function formatDate() {
+ return pad(todaysMidnight.getFullYear())
+ + "-" + pad(todaysMidnight.getMonth() + 1)
+ + "-" + pad(todaysMidnight.getDate());
}
module.exports = {
info,
error,
request
-};
\ No newline at end of file
+};
diff --git a/src/services/migration.js b/src/services/migration.js
index 383b1464a4..be84464d24 100644
--- a/src/services/migration.js
+++ b/src/services/migration.js
@@ -1,6 +1,5 @@
const backupService = require('./backup');
const sql = require('./sql');
-const sqlInit = require('./sql_init');
const optionService = require('./options');
const fs = require('fs-extra');
const log = require('./log');
@@ -75,6 +74,8 @@ function migrate() {
}
}
+ const sqlInit = require('./sql_init');
+
if (sqlInit.isDbUpToDate()) {
sqlInit.initDbConnection();
}