-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathlog.ts
122 lines (88 loc) · 2.82 KB
/
log.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"use strict";
import type { Request, Response } from "express";
import fs from "fs";
import dataDir from "./data_dir.js";
import cls from "./cls.js";
import { isWindows } from "./utils.js";
if (!fs.existsSync(dataDir.LOG_DIR)) {
fs.mkdirSync(dataDir.LOG_DIR, 0o700);
}
let logFile!: fs.WriteStream;
const SECOND = 1000;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
const NEW_LINE = isWindows ? "\r\n" : "\n";
let todaysMidnight!: Date;
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: number) {
if (millisSinceMidnight >= DAY) {
initLogFile();
millisSinceMidnight -= DAY;
}
return millisSinceMidnight;
}
function log(str: string | Error) {
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}`);
console.log(str);
}
function info(message: string | Error) {
log(message);
}
function error(message: string | Error) {
log(`ERROR: ${message}`);
}
const requestBlacklist = ["/libraries", "/app", "/images", "/stylesheets", "/api/recent-notes"];
function request(req: Request, res: Response, timeMs: number, responseLength: number | string = "?") {
for (const bl of requestBlacklist) {
if (req.url.startsWith(bl)) {
return;
}
}
if (req.url.includes(".js.map") || req.url.includes(".css.map")) {
return;
}
info((timeMs >= 10 ? "Slow " : "") + `${res.statusCode} ${req.method} ${req.url} with ${responseLength} bytes took ${timeMs}ms`);
}
function pad(num: number) {
num = Math.floor(num);
return num < 10 ? `0${num}` : num.toString();
}
function padMilli(num: number) {
if (num < 10) {
return `00${num}`;
} else if (num < 100) {
return `0${num}`;
} else {
return num.toString();
}
}
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())}`;
}
export default {
info,
error,
request
};