Notes/src/services/log.ts

125 lines
2.8 KiB
TypeScript
Raw Normal View History

"use strict";
2024-02-16 20:54:54 +02:00
import { Request, Response } from "express";
import * as fs from "fs";
const dataDir = require('./data_dir.js');
const cls = require('./cls.js');
if (!fs.existsSync(dataDir.LOG_DIR)) {
fs.mkdirSync(dataDir.LOG_DIR, 0o700);
}
2024-02-16 20:54:54 +02:00
let logFile!: fs.WriteStream;
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;
initLogFile();
function getTodaysMidnight() {
const now = new Date();
2017-10-29 14:55:48 -04:00
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'});
}
2024-02-16 20:54:54 +02:00
function checkDate(millisSinceMidnight: number) {
if (millisSinceMidnight >= DAY) {
initLogFile();
2021-08-12 20:57:43 +02:00
millisSinceMidnight -= DAY;
}
return millisSinceMidnight;
}
2024-02-16 20:54:54 +02:00
function log(str: string) {
const bundleNoteId = cls.get("bundleNoteId");
if (bundleNoteId) {
str = `[Script ${bundleNoteId}] ${str}`;
}
let millisSinceMidnight = Date.now() - todaysMidnight.getTime();
millisSinceMidnight = checkDate(millisSinceMidnight);
logFile.write(`${formatTime(millisSinceMidnight)} ${str}${NEW_LINE}`);
2018-04-07 21:30:01 -04:00
console.log(str);
}
2018-04-07 21:30:01 -04:00
2024-02-16 20:54:54 +02:00
function info(message: string) {
log(message);
}
2024-02-16 20:54:54 +02:00
function error(message: string) {
log(`ERROR: ${message}`);
}
const requestBlacklist = [ "/libraries", "/app", "/images", "/stylesheets", "/api/recent-notes" ];
2024-02-16 20:54:54 +02:00
function request(req: Request, res: Response, timeMs: number, responseLength = "?") {
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 " : "") +
`${res.statusCode} ${req.method} ${req.url} with ${responseLength} bytes took ${timeMs}ms`);
}
2024-02-16 20:54:54 +02:00
function pad(num: number) {
num = Math.floor(num);
return num < 10 ? (`0${num}`) : num.toString();
}
2024-02-16 20:54:54 +02:00
function padMilli(num: number) {
if (num < 10) {
return `00${num}`;
}
else if (num < 100) {
return `0${num}`;
}
else {
return num.toString();
}
}
2024-02-16 20:54:54 +02:00
function formatTime(millisSinceMidnight: number) {
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 = {
info,
error,
request
};