From b245b249d34951c2fd14a285e645791260c140aa Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 23 Jun 2020 13:38:27 +0200 Subject: [PATCH] got rid of simple-node-logger in favor of simple custom logger --- package.json | 1 - src/entities/note.js | 7 ++- src/services/log.js | 102 ++++++++++++++++++++++++++++++-------- src/services/migration.js | 3 +- 4 files changed, 89 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 2e5c7ea3d..c551fce27 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 b2d93add2..caac1885b 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 9c2be4187..007d40cef 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-.log', - dateFormat:'YYYY-MM-DD' -}); +let logFile = null; + +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(); + + return new Date(now.getFullYear(), now.getMonth(), now.getDate()); +} + +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); + + console.log(str); +} function info(message) { - // info messages are logged asynchronously - setTimeout(() => { - console.log(message); - - logger.info(message); - }, 0); + log(message); } function error(message) { - message = "ERROR: " + message; - - // 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.trace(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 383b1464a..be84464d2 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(); }