From 5c6c25feb19d0e25bff4e5d43ac8c3545ead3440 Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Thu, 7 Sep 2023 21:16:13 -0400 Subject: [PATCH] Update logger, add helpers, host env --- src/util/Logger.ts | 31 +++++++++++++++++++++---------- src/util/env.ts | 5 ++++- src/util/helpers.ts | 14 ++++++++++++++ src/util/readline.ts | 6 ++++++ 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 src/util/helpers.ts create mode 100644 src/util/readline.ts diff --git a/src/util/Logger.ts b/src/util/Logger.ts index 15f3284..e252588 100755 --- a/src/util/Logger.ts +++ b/src/util/Logger.ts @@ -1,39 +1,50 @@ +import { padNum, unimportant } from "./helpers"; + export class Logger { private static log(method: string, ...args: any[]) { (console as unknown as Record any>)[ method - ](this.getHHMMSS(), ...args); + ]( + unimportant(this.getDate()), + unimportant(this.getHHMMSSMS()), + ...args + ); } - public static getHHMMSS() { + public static getHHMMSSMS() { const ms = Date.now(); const s = ms / 1000; const m = s / 60; const h = m / 60; - const ss = Math.floor(s) % 60; - const mm = Math.floor(m) % 60; - const hh = Math.floor(h) % 24; + const ss = padNum(Math.floor(s) % 60, 2, "0"); + const mm = padNum(Math.floor(m) % 60, 2, "0"); + const hh = padNum(Math.floor(h) % 24, 2, "0"); + const ll = padNum(ms % 1000, 3, "0"); - return `${hh}:${mm}:${ss}`; + return `${hh}:${mm}:${ss}.${ll}`; + } + + public static getDate() { + return new Date().toISOString().split("T")[0]; } constructor(public id: string) {} public info(...args: any[]) { - Logger.log("log", `[${this.id}]`, `[\x1b[34mINFO\x1b[0m]`, ...args); + Logger.log("log", `[${this.id}]`, `\x1b[34m[info]\x1b[0m`, ...args); } public error(...args: any[]) { - Logger.log("error", `[${this.id}]`, `[\x1b[31mERROR\x1b[0m]`, ...args); + Logger.log("error", `[${this.id}]`, `\x1b[31m[error]\x1b[0m`, ...args); } public warn(...args: any[]) { - Logger.log("warn", `[${this.id}]`, `[\x1b[33mWARN\x1b[0m]`, ...args); + Logger.log("warn", `[${this.id}]`, `\x1b[33m[warn]\x1b[0m`, ...args); } public debug(...args: any[]) { - Logger.log("debug", `[${this.id}]`, `[\x1b[32mDEBUG\x1b[0m]`, ...args); + Logger.log("debug", `[${this.id}]`, `\x1b[32m[debug]\x1b[0m`, ...args); } } diff --git a/src/util/env.ts b/src/util/env.ts index fa583ae..ff2b12e 100644 --- a/src/util/env.ts +++ b/src/util/env.ts @@ -2,11 +2,14 @@ import dotenv from "dotenv"; import { createEnv } from "@t3-oss/env-core"; import { z } from "zod"; -dotenv.config(); +dotenv.config({ + path: "./.env" +}); export const env = createEnv({ server: { PORT: z.coerce.number(), + HOST: z.union([z.string().url(), z.string().ip()]).optional(), SALT: z.string().min(10), ADMIN_PASS: z.string() }, diff --git a/src/util/helpers.ts b/src/util/helpers.ts new file mode 100644 index 0000000..be635af --- /dev/null +++ b/src/util/helpers.ts @@ -0,0 +1,14 @@ +export function unimportant(str: string) { + return `\x1b[90m${str}\x1b[0m`; +} + +export function padNum( + num: number, + padAmount: number, + padChar: string, + left: boolean = true +) { + return left + ? num.toString().padStart(padAmount, padChar) + : num.toString().padEnd(padAmount, padChar); +} diff --git a/src/util/readline.ts b/src/util/readline.ts new file mode 100644 index 0000000..c5b633a --- /dev/null +++ b/src/util/readline.ts @@ -0,0 +1,6 @@ +import readline from "readline"; + +export const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +});