2017-10-24 22:04:52 -04:00
|
|
|
"use strict";
|
|
|
|
|
2024-02-16 20:54:54 +02:00
|
|
|
import { Request, Response } from "express";
|
|
|
|
import * as fs from "fs";
|
2023-11-22 19:34:48 +01:00
|
|
|
const dataDir = require('./data_dir.js');
|
|
|
|
const cls = require('./cls.js');
|
2017-10-24 22:04:52 -04:00
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
if (!fs.existsSync(dataDir.LOG_DIR)) {
|
|
|
|
fs.mkdirSync(dataDir.LOG_DIR, 0o700);
|
2017-10-24 22:04:52 -04:00
|
|
|
}
|
|
|
|
|
2024-02-16 20:54:54 +02:00
|
|
|
let logFile!: fs.WriteStream;
|
2017-10-24 22:04:52 -04:00
|
|
|
|
2020-06-23 13:38:27 +02:00
|
|
|
const SECOND = 1000;
|
|
|
|
const MINUTE = 60 * SECOND;
|
|
|
|
const HOUR = 60 * MINUTE;
|
|
|
|
const DAY = 24 * HOUR;
|
|
|
|
|
|
|
|
const NEW_LINE = process.platform === "win32" ? '\r\n' : '\n';
|
|
|
|
|
2024-02-16 20:54:54 +02:00
|
|
|
let todaysMidnight!: Date;
|
2020-06-23 13:38:27 +02:00
|
|
|
|
|
|
|
initLogFile();
|
|
|
|
|
|
|
|
function getTodaysMidnight() {
|
|
|
|
const now = new Date();
|
2017-10-29 14:55:48 -04:00
|
|
|
|
2020-06-23 13:38:27 +02:00
|
|
|
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
2017-10-24 22:04:52 -04:00
|
|
|
}
|
|
|
|
|
2020-06-23 13:38:27 +02:00
|
|
|
function initLogFile() {
|
|
|
|
todaysMidnight = getTodaysMidnight();
|
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
const path = `${dataDir.LOG_DIR}/trilium-${formatDate()}.log`;
|
2020-06-23 13:38:27 +02:00
|
|
|
|
|
|
|
if (logFile) {
|
|
|
|
logFile.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
logFile = fs.createWriteStream(path, {flags: 'a'});
|
|
|
|
}
|
|
|
|
|
2024-02-16 20:54:54 +02:00
|
|
|
function checkDate(millisSinceMidnight: number) {
|
2020-06-23 13:38:27 +02:00
|
|
|
if (millisSinceMidnight >= DAY) {
|
|
|
|
initLogFile();
|
2021-05-25 21:03:00 +02:00
|
|
|
|
2021-08-12 20:57:43 +02:00
|
|
|
millisSinceMidnight -= DAY;
|
2020-06-23 13:38:27 +02:00
|
|
|
}
|
2021-05-25 21:03:00 +02:00
|
|
|
|
|
|
|
return millisSinceMidnight;
|
2020-06-23 13:38:27 +02:00
|
|
|
}
|
|
|
|
|
2024-02-16 20:54:54 +02:00
|
|
|
function log(str: string) {
|
2022-02-01 22:25:38 +01:00
|
|
|
const bundleNoteId = cls.get("bundleNoteId");
|
|
|
|
|
|
|
|
if (bundleNoteId) {
|
|
|
|
str = `[Script ${bundleNoteId}] ${str}`;
|
|
|
|
}
|
|
|
|
|
2021-05-25 21:03:00 +02:00
|
|
|
let millisSinceMidnight = Date.now() - todaysMidnight.getTime();
|
2020-06-23 13:38:27 +02:00
|
|
|
|
2021-05-25 21:03:00 +02:00
|
|
|
millisSinceMidnight = checkDate(millisSinceMidnight);
|
2020-06-23 13:38:27 +02:00
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
logFile.write(`${formatTime(millisSinceMidnight)} ${str}${NEW_LINE}`);
|
2018-04-07 21:30:01 -04:00
|
|
|
|
2020-06-23 13:38:27 +02:00
|
|
|
console.log(str);
|
|
|
|
}
|
2018-04-07 21:30:01 -04:00
|
|
|
|
2024-02-16 20:54:54 +02:00
|
|
|
function info(message: string) {
|
2020-06-23 13:38:27 +02:00
|
|
|
log(message);
|
|
|
|
}
|
|
|
|
|
2024-02-16 20:54:54 +02:00
|
|
|
function error(message: string) {
|
2022-12-21 15:19:05 +01:00
|
|
|
log(`ERROR: ${message}`);
|
2017-10-24 22:04:52 -04:00
|
|
|
}
|
|
|
|
|
2021-10-08 15:14:23 +02:00
|
|
|
const requestBlacklist = [ "/libraries", "/app", "/images", "/stylesheets", "/api/recent-notes" ];
|
2017-10-24 22:04:52 -04:00
|
|
|
|
2024-02-16 20:54:54 +02:00
|
|
|
function request(req: Request, res: Response, timeMs: number, responseLength = "?") {
|
2017-10-24 22:04:52 -04:00
|
|
|
for (const bl of requestBlacklist) {
|
|
|
|
if (req.url.startsWith(bl)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 20:26:40 +01:00
|
|
|
if (req.url.includes(".js.map") || req.url.includes(".css.map")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-02 23:15:37 +02:00
|
|
|
info((timeMs >= 10 ? "Slow " : "") +
|
2021-10-06 20:52:23 +02:00
|
|
|
`${res.statusCode} ${req.method} ${req.url} with ${responseLength} bytes took ${timeMs}ms`);
|
2020-06-23 13:38:27 +02:00
|
|
|
}
|
|
|
|
|
2024-02-16 20:54:54 +02:00
|
|
|
function pad(num: number) {
|
2020-06-23 13:38:27 +02:00
|
|
|
num = Math.floor(num);
|
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
return num < 10 ? (`0${num}`) : num.toString();
|
2020-06-23 13:38:27 +02:00
|
|
|
}
|
|
|
|
|
2024-02-16 20:54:54 +02:00
|
|
|
function padMilli(num: number) {
|
2020-06-23 13:38:27 +02:00
|
|
|
if (num < 10) {
|
2022-12-21 15:19:05 +01:00
|
|
|
return `00${num}`;
|
2020-06-23 13:38:27 +02:00
|
|
|
}
|
|
|
|
else if (num < 100) {
|
2022-12-21 15:19:05 +01:00
|
|
|
return `0${num}`;
|
2020-06-23 13:38:27 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return num.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-16 20:54:54 +02:00
|
|
|
function formatTime(millisSinceMidnight: number) {
|
2022-12-21 15:19:05 +01:00
|
|
|
return `${pad(millisSinceMidnight / HOUR)}:${pad((millisSinceMidnight % HOUR) / MINUTE)}:${pad((millisSinceMidnight % MINUTE) / SECOND)}.${padMilli(millisSinceMidnight % SECOND)}`;
|
2020-06-23 13:38:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatDate() {
|
2022-12-21 15:19:05 +01:00
|
|
|
return `${pad(todaysMidnight.getFullYear())}-${pad(todaysMidnight.getMonth() + 1)}-${pad(todaysMidnight.getDate())}`;
|
2017-10-24 22:04:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
info,
|
|
|
|
error,
|
|
|
|
request
|
2020-06-23 13:38:27 +02:00
|
|
|
};
|