trilium/src/services/log.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

"use strict";
const fs = require('fs');
const dataDir = require('./data_dir');
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'
});
function info(message) {
2018-04-08 09:30:01 +08:00
// info messages are logged asynchronously
setTimeout(() => {
console.log(message);
2017-10-30 02:55:48 +08:00
2018-04-08 09:30:01 +08:00
logger.info(message);
}, 0);
}
function error(message) {
2018-04-08 09:30:01 +08:00
message = "ERROR: " + message;
2017-11-09 11:33:08 +08:00
// we're using .info() instead of .error() because simple-node-logger emits weird error for showError()
2018-04-08 09:30:01 +08:00
// errors are logged synchronously to make sure it doesn't get lost in case of crash
logger.info(message);
console.trace(message);
}
2017-11-25 09:51:44 +08:00
const requestBlacklist = [ "/libraries", "/javascripts", "/images", "/stylesheets" ];
function request(req) {
for (const bl of requestBlacklist) {
if (req.url.startsWith(bl)) {
return;
}
}
logger.info(req.method + " " + req.url);
}
info("Using data dir: " + dataDir.TRILIUM_DATA_DIR);
2017-12-07 08:42:23 +08:00
module.exports = {
info,
error,
request
};