Skip to content

Build your personal knowledge base with Trilium Notes

more logging

azivner8e47e34d1f9d
diff --git a/routes/api/notes_move.js b/routes/api/notes_move.js
index 6b68552b35..8cc4ed0bb1 100644
--- a/routes/api/notes_move.js
+++ b/routes/api/notes_move.js
@@ -27,7 +27,7 @@ router.put('/:noteId/moveTo/:parentId', auth.checkApiAuth, async (req, res, next
await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?",
[parentId, newNotePos, now, noteId]);
- await sql.addAudit(audit_category.CHANGE_PARENT, req, noteId);
+ await sql.addAudit(audit_category.CHANGE_PARENT, req, noteId, null, parentId);
await sql.commit();
diff --git a/services/backup.js b/services/backup.js
index 892aa4266c..fbbdd91b74 100644
--- a/services/backup.js
+++ b/services/backup.js
@@ -2,9 +2,9 @@
const utils = require('./utils');
const sql = require('./sql');
-const config = require('./config');
const fs = require('fs-extra');
const dataDir = require('./data_dir');
+const log = require('./log');
async function regularBackup() {
const now = utils.nowTimestamp();
@@ -20,18 +20,19 @@ async function regularBackup() {
async function backupNow() {
const now = utils.nowTimestamp();
- const backup_directory = config.Backup.backupDirectory;
-
const date_str = new Date().toISOString().substr(0, 19);
if (!fs.existsSync(dataDir.BACKUP_DIR)) {
fs.mkdirSync(dataDir.BACKUP_DIR, 0o700);
}
- fs.copySync(dataDir.DOCUMENT_PATH, dataDir.BACKUP_DIR + "/" + "backup-" + date_str + ".db");
+ const backupFile = dataDir.BACKUP_DIR + "/" + "backup-" + date_str + ".db";
+
+ fs.copySync(dataDir.DOCUMENT_PATH, backupFile);
+
+ log.info("Created backup at " + backupFile);
await sql.setOption('last_backup_date', now);
- //await sql.commit();
}
async function cleanupOldBackups() {
@@ -46,7 +47,7 @@ async function cleanupOldBackups() {
const date = Date.parse(date_str);
if (now.getTime() - date.getTime() > 30 * 24 * 3600 * 1000) {
- console.log("Removing old backup - " + file);
+ log.info("Removing old backup - " + file);
fs.unlink(dataDir.BACKUP_DIR + "/" + file);
}
diff --git a/services/migration.js b/services/migration.js
index d129c866b7..b0a31d26da 100644
--- a/services/migration.js
+++ b/services/migration.js
@@ -1,6 +1,7 @@
const backup = require('./backup');
const sql = require('./sql');
const fs = require('fs-extra');
+const log = require('./log');
const APP_DB_VERSION = 9;
const MIGRATIONS_DIR = "./migrations";
@@ -8,6 +9,7 @@ const MIGRATIONS_DIR = "./migrations";
async function migrate() {
const migrations = [];
+ // backup before attempting migration
await backup.backupNow();
const currentDbVersion = parseInt(await sql.getOption('db_version'));
@@ -38,7 +40,7 @@ async function migrate() {
const migrationSql = fs.readFileSync(MIGRATIONS_DIR + "/" + mig.file).toString('utf8');
try {
- console.log("Running script: ", migrationSql);
+ log.info("Attempting migration to version " + mig.dbVersion + " with script: " + migrationSql);
await sql.beginTransaction();
@@ -48,17 +50,20 @@ async function migrate() {
await sql.commit();
+ log.info("Migration to version " + mig.dbVersion + " has been successful.");
+
mig['success'] = true;
}
catch (e) {
mig['success'] = false;
mig['error'] = e.stack;
- console.log("error during migration: ", e);
+ log.error("error during migration to version " + mig.dbVersion + ": " + e.stack);
break;
}
}
+
return migrations;
}
diff --git a/services/session_secret.js b/services/session_secret.js
index 34149fe31f..a589c70c9b 100644
--- a/services/session_secret.js
+++ b/services/session_secret.js
@@ -3,6 +3,7 @@
const fs = require('fs');
const crypto = require('crypto');
const dataDir = require('./data_dir');
+const log = require('./log');
const sessionSecretPath = dataDir.TRILIUM_DATA_DIR + "/session_secret.txt";
@@ -17,6 +18,8 @@ function randomValueHex(len) {
if (!fs.existsSync(sessionSecretPath)) {
sessionSecret = randomValueHex(64);
+ log.info("Generated session secret");
+
fs.writeFileSync(sessionSecretPath, sessionSecret, 'ASCII');
}
else {
diff --git a/services/sql.js b/services/sql.js
index edd5b1f3d4..6fc69b4509 100644
--- a/services/sql.js
+++ b/services/sql.js
@@ -2,6 +2,7 @@
const db = require('sqlite');
const utils = require('./utils');
+const log = require('./log');
async function insert(table_name, rec) {
const columns = Object.keys(rec).join(", ");
@@ -55,6 +56,9 @@ async function addAudit(category, req=null, noteId=null, changeFrom=null, change
const browserId = req == null ? null : req.get('x-browser-id');
+ log.info("audit: " + category + ", browserId=" + browserId + ", noteId=" + noteId + ", from=" + changeFrom
+ + ", to=" + changeTo + ", comment=" + comment);
+
await execute("INSERT INTO audit_log (date_modified, category, browser_id, note_id, change_from, change_to, comment)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?)", [now, category, browserId, noteId, changeFrom, changeTo, comment]);
}