add logging to file

This commit is contained in:
Holly Stubbs 2023-04-17 02:04:05 +01:00
parent 61133eb710
commit b1a4933990
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
2 changed files with 52 additions and 27 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ node_modules/
build/ build/
bundle/ bundle/
world/ world/
logs/

View file

@ -1,4 +1,5 @@
import chalk from "chalk"; import chalk from "chalk";
import { createWriteStream, mkdirSync, existsSync, fstat } from "fs";
console.clear(); console.clear();
@ -8,16 +9,26 @@ enum LogType {
ERROR ERROR
} }
const LogTags = { enum LogTag {
INFO: chalk.bgGreen(chalk.black(" INFO ")), INFO,
BANCHO: chalk.bgMagenta(chalk.black(" BANCHO ")), CHAT,
WEBREQ: chalk.bgGreen(chalk.black(" WEBREQ ")), WARN,
CHAT: chalk.bgCyan(chalk.black(" CHAT ")), ERROR
WARN: chalk.bgYellow(chalk.black(" WARN ")), }
ERROR: chalk.bgRed(" ERRR "),
REDIS: chalk.bgRed(chalk.white(" bREDIS ")), const LogTags = [
STREAM: chalk.bgBlue(chalk.black(" STREAM ")) chalk.bgGreen(chalk.black(" INFO ")),
} as const; chalk.bgCyan(chalk.black(" CHAT ")),
chalk.bgYellow(chalk.black(" WARN ")),
chalk.bgRed(" ERRR ")
] as const;
const TagsForFile = [
"[INFO]",
"[CHAT]",
"[WARN]",
"[ERRR]"
] as const;
function correctValue(i:number) : string { function correctValue(i:number) : string {
if (i <= 9) return `0${i}`; if (i <= 9) return `0${i}`;
@ -26,42 +37,55 @@ function correctValue(i:number) : string {
function getTime() : string { function getTime() : string {
const time = new Date(); const time = new Date();
return chalk.green(`[${correctValue(time.getHours())}:${correctValue(time.getMinutes())}:${correctValue(time.getSeconds())}]`); return `[${correctValue(time.getHours())}:${correctValue(time.getMinutes())}:${correctValue(time.getSeconds())}]`;
} }
function log(tag:string, log:string, logType:LogType = LogType.INFO) : void { function log(tag:LogTag, log:string, logType:LogType = LogType.INFO) : void {
const stringTime = getTime(),
fileTag = TagsForFile[tag],
consoleTag = LogTags[tag];
Console.QUEUED_FOR_LOG += `${stringTime} ${fileTag} ${log}\n`;
switch (logType) { switch (logType) {
case LogType.INFO: case LogType.INFO:
return console.log(`${getTime()} ${tag} ${log}`); return console.log(`${chalk.green(stringTime)} ${consoleTag} ${log}`);
case LogType.WARN: case LogType.WARN:
return console.warn(`${getTime()} ${tag} ${log}`); return console.warn(`${chalk.green(stringTime)} ${consoleTag} ${log}`);
case LogType.ERROR: case LogType.ERROR:
return console.error(`${getTime()} ${tag} ${log}`); return console.error(`${chalk.green(stringTime)} ${consoleTag} ${log}`);
} }
} }
if (existsSync("./logs")) {
} else {
mkdirSync("./logs/");
}
export class Console { export class Console {
public static printWebReq(s:string) : void { public static QUEUED_FOR_LOG:string = "";
log(LogTags.WEBREQ, s); private static logFileWriteStream = createWriteStream("./logs/latest.log");
} private static flushTimer:NodeJS.Timer = setInterval(() => {
if (Console.QUEUED_FOR_LOG.length !== 0) {
public static printStream(s:string) : void { const strRef = Console.QUEUED_FOR_LOG;
log(LogTags.STREAM, s); Console.QUEUED_FOR_LOG = "";
Console.logFileWriteStream.write(strRef);
} }
}, 1000 * 10);
public static printInfo(s:string) : void { public static printInfo(s:string) : void {
log(LogTags.INFO, s); log(LogTag.INFO, s);
} }
public static printChat(s:string) : void { public static printChat(s:string) : void {
log(LogTags.CHAT, s); log(LogTag.CHAT, s);
} }
public static printWarn(s:string) : void { public static printWarn(s:string) : void {
log(LogTags.WARN, s); log(LogTag.WARN, s, LogType.WARN);
} }
public static printError(s:string) : void { public static printError(s:string) : void {
log(LogTags.ERROR, s); log(LogTag.ERROR, s, LogType.ERROR);
} }
} }