mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
got rid of simple-node-logger in favor of simple custom logger
This commit is contained in:
parent
22302e8200
commit
b245b249d3
@ -68,7 +68,6 @@
|
|||||||
"semver": "7.3.2",
|
"semver": "7.3.2",
|
||||||
"serve-favicon": "2.5.0",
|
"serve-favicon": "2.5.0",
|
||||||
"session-file-store": "1.4.0",
|
"session-file-store": "1.4.0",
|
||||||
"simple-node-logger": "18.12.24",
|
|
||||||
"string-similarity": "4.0.1",
|
"string-similarity": "4.0.1",
|
||||||
"tar-stream": "2.1.2",
|
"tar-stream": "2.1.2",
|
||||||
"turndown": "6.0.0",
|
"turndown": "6.0.0",
|
||||||
|
@ -123,7 +123,12 @@ class Note extends Entity {
|
|||||||
throw new Error(`Cannot set null content to note ${this.noteId}`);
|
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
|
// force updating note itself so that dateModified is represented correctly even for the content
|
||||||
this.forcedChange = true;
|
this.forcedChange = true;
|
||||||
|
@ -7,30 +7,59 @@ if (!fs.existsSync(dataDir.LOG_DIR)) {
|
|||||||
fs.mkdirSync(dataDir.LOG_DIR, 0o700);
|
fs.mkdirSync(dataDir.LOG_DIR, 0o700);
|
||||||
}
|
}
|
||||||
|
|
||||||
const logger = require('simple-node-logger').createRollingFileLogger({
|
let logFile = null;
|
||||||
errorEventName: 'error',
|
|
||||||
logDirectory: dataDir.LOG_DIR,
|
const SECOND = 1000;
|
||||||
fileNamePattern: 'trilium-<DATE>.log',
|
const MINUTE = 60 * SECOND;
|
||||||
dateFormat:'YYYY-MM-DD'
|
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) {
|
function info(message) {
|
||||||
// info messages are logged asynchronously
|
log(message);
|
||||||
setTimeout(() => {
|
|
||||||
console.log(message);
|
|
||||||
|
|
||||||
logger.info(message);
|
|
||||||
}, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function error(message) {
|
function error(message) {
|
||||||
message = "ERROR: " + message;
|
log("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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const requestBlacklist = [ "/libraries", "/app", "/images", "/stylesheets" ];
|
const requestBlacklist = [ "/libraries", "/app", "/images", "/stylesheets" ];
|
||||||
@ -46,11 +75,42 @@ function request(req) {
|
|||||||
return;
|
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 = {
|
module.exports = {
|
||||||
info,
|
info,
|
||||||
error,
|
error,
|
||||||
request
|
request
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
const backupService = require('./backup');
|
const backupService = require('./backup');
|
||||||
const sql = require('./sql');
|
const sql = require('./sql');
|
||||||
const sqlInit = require('./sql_init');
|
|
||||||
const optionService = require('./options');
|
const optionService = require('./options');
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const log = require('./log');
|
const log = require('./log');
|
||||||
@ -75,6 +74,8 @@ function migrate() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sqlInit = require('./sql_init');
|
||||||
|
|
||||||
if (sqlInit.isDbUpToDate()) {
|
if (sqlInit.isDbUpToDate()) {
|
||||||
sqlInit.initDbConnection();
|
sqlInit.initDbConnection();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user