mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 13:39:01 +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",
 | 
			
		||||
    "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",
 | 
			
		||||
 | 
			
		||||
@ -123,7 +123,12 @@ class Note extends Entity {
 | 
			
		||||
            throw new Error(`Cannot set null content to note ${this.noteId}`);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        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;
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
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,7 +75,38 @@ 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 = {
 | 
			
		||||
 | 
			
		||||
@ -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();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user