Quellcode durchsuchen

feat: Allow logbook outputs to be configured via json config and other tweaks

Owen Diffey vor 2 Jahren
Ursprung
Commit
1abcd3641d
2 geänderte Dateien mit 63 neuen und 43 gelöschten Zeilen
  1. 25 9
      backend/src/LogBook.ts
  2. 38 34
      backend/src/main.ts

+ 25 - 9
backend/src/LogBook.ts

@@ -1,3 +1,4 @@
+import config from "config";
 import fs from "fs";
 
 export type Log = {
@@ -28,6 +29,8 @@ export type LogOutputs = {
 export default class LogBook {
 	private logs: Log[];
 
+	private default: LogOutputs;
+
 	private outputs: LogOutputs;
 
 	private stream: fs.WriteStream;
@@ -37,7 +40,7 @@ export default class LogBook {
 	 */
 	public constructor(file = "logs/backend.log") {
 		this.logs = [];
-		this.outputs = {
+		this.default = {
 			console: {
 				timestamp: true,
 				title: true,
@@ -46,13 +49,13 @@ export default class LogBook {
 				data: false,
 				color: true,
 				exclude: [
-					// {
-					// 	category: "jobs",
-					// 	type: "success"
-					// },
-					// {
-					// 	type: "debug"
-					// }
+					{
+						category: "jobs",
+						type: "success"
+					},
+					{
+						type: "debug"
+					}
 				]
 			},
 			file: {
@@ -67,6 +70,17 @@ export default class LogBook {
 				enabled: false
 			}
 		};
+		if (config.has("logging"))
+			["console", "file", "memory"].forEach(output => {
+				if (config.has(`logging.${output}`))
+					// @ts-ignore
+					this.default[output] = {
+						// @ts-ignore
+						...this.default[output],
+						...config.get<any>(`logging.${output}`)
+					};
+			});
+		this.outputs = this.default;
 		this.stream = fs.createWriteStream(file, { flags: "a" });
 	}
 
@@ -199,7 +213,7 @@ export default class LogBook {
 							...filters
 						];
 				} else if (action === "reset") {
-					this.outputs[output][key] = [];
+					this.outputs[output][key] = this.default[output][key] || [];
 				} else
 					throw new Error(
 						`Action "${action}" not found for ${key} in ${output}`
@@ -219,6 +233,8 @@ export default class LogBook {
 				if (output !== "memory" && action === "set") {
 					if (!values) throw new Error("No value provided");
 					this.outputs[output][key] = values;
+				} else if (output !== "memory" && action === "reset") {
+					this.outputs[output][key] = this.default[output][key];
 				} else
 					throw new Error(
 						`Action "${action}" not found for ${key} in ${output}`

+ 38 - 34
backend/src/main.ts

@@ -3,6 +3,27 @@ import ModuleManager from "./ModuleManager";
 import LogBook from "./LogBook";
 
 const logBook = new LogBook();
+
+process.removeAllListeners("uncaughtException");
+process.on("uncaughtException", err => {
+	if (err.name === "ECONNREFUSED" || err.name === "UNCERTAIN_STATE") return;
+
+	logBook.log({
+		message: err.message || err,
+		type: "error",
+		category: "uncaught-exceptions",
+		data: {
+			error: err.message
+				? {
+						cause: err.cause,
+						name: err.name,
+						stack: err.stack
+				  }
+				: err
+		}
+	});
+});
+
 const moduleManager = new ModuleManager(logBook);
 moduleManager.startup();
 
@@ -32,18 +53,29 @@ global.rs = () => {
 // 	clearTimeout(interval);
 // }, 3000);
 
+const rl = readline.createInterface({
+	input: process.stdin,
+	output: process.stdout,
+	completer: (command: string) => {
+		const parts = command.split(" ");
+		const commands = ["eval "];
 
+		if (parts.length === 1) {
+			const hits = commands.filter(c => c.startsWith(parts[0]));
 
-// Temp fix
-process.removeAllListeners("uncaughtException");
-
-process.on("uncaughtException", err => {
-	if (err.name === "ECONNREFUSED" || err.name === "UNCERTAIN_STATE") return;
+			return [hits.length ? hits : commands, command];
+		}
 
-	console.log(`UNCAUGHT EXCEPTION: ${err.stack}`);
+		return [];
+	},
+	removeHistoryDuplicates: true
 });
 
 const shutdown = async () => {
+	if (rl) {
+		rl.removeAllListeners();
+		rl.close();
+	}
 	await moduleManager.shutdown().catch(() => process.exit(1));
 	process.exit(0);
 };
@@ -51,17 +83,6 @@ process.on("SIGINT", shutdown);
 process.on("SIGQUIT", shutdown);
 process.on("SIGTERM", shutdown);
 
-// const shutdown = () => {
-// 	moduleManager
-// 		.shutdown()
-// 		.then(() => process.exit(0))
-// 		.catch(() => process.exit(1));
-// };
-// process.on("SIGINT", shutdown);
-// process.on("SIGQUIT", shutdown);
-// process.on("SIGTERM", shutdown);
-// process.on("SIGUSR2", shutdown);
-
 const runCommand = (line: string) => {
 	const [command, ...args] = line.split(" ");
 	switch (command) {
@@ -153,21 +174,4 @@ const runCommand = (line: string) => {
 	}
 };
 
-const rl = readline.createInterface({
-	input: process.stdin,
-	output: process.stdout,
-	completer: (command: string) => {
-		const parts = command.split(" ");
-		const commands = ["eval "];
-
-		if (parts.length === 1) {
-			const hits = commands.filter(c => c.startsWith(parts[0]));
-
-			return [hits.length ? hits : commands, command];
-		}
-
-		return [];
-	}
-});
-
 rl.on("line", runCommand);