Update logger, add helpers, host env

This commit is contained in:
Hri7566 2023-09-07 21:16:13 -04:00
parent b6c2adeff4
commit 5c6c25feb1
4 changed files with 45 additions and 11 deletions

View File

@ -1,39 +1,50 @@
import { padNum, unimportant } from "./helpers";
export class Logger { export class Logger {
private static log(method: string, ...args: any[]) { private static log(method: string, ...args: any[]) {
(console as unknown as Record<string, (..._args: any[]) => any>)[ (console as unknown as Record<string, (..._args: any[]) => any>)[
method method
](this.getHHMMSS(), ...args); ](
unimportant(this.getDate()),
unimportant(this.getHHMMSSMS()),
...args
);
} }
public static getHHMMSS() { public static getHHMMSSMS() {
const ms = Date.now(); const ms = Date.now();
const s = ms / 1000; const s = ms / 1000;
const m = s / 60; const m = s / 60;
const h = m / 60; const h = m / 60;
const ss = Math.floor(s) % 60; const ss = padNum(Math.floor(s) % 60, 2, "0");
const mm = Math.floor(m) % 60; const mm = padNum(Math.floor(m) % 60, 2, "0");
const hh = Math.floor(h) % 24; 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) {} constructor(public id: string) {}
public info(...args: any[]) { 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[]) { 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[]) { 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[]) { 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);
} }
} }

View File

@ -2,11 +2,14 @@ import dotenv from "dotenv";
import { createEnv } from "@t3-oss/env-core"; import { createEnv } from "@t3-oss/env-core";
import { z } from "zod"; import { z } from "zod";
dotenv.config(); dotenv.config({
path: "./.env"
});
export const env = createEnv({ export const env = createEnv({
server: { server: {
PORT: z.coerce.number(), PORT: z.coerce.number(),
HOST: z.union([z.string().url(), z.string().ip()]).optional(),
SALT: z.string().min(10), SALT: z.string().min(10),
ADMIN_PASS: z.string() ADMIN_PASS: z.string()
}, },

14
src/util/helpers.ts Normal file
View File

@ -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);
}

6
src/util/readline.ts Normal file
View File

@ -0,0 +1,6 @@
import readline from "readline";
export const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});