diff --git a/src/index.ts b/src/index.ts index aecd711..22787fa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,8 +2,6 @@ import fs from "node:fs"; import path from "node:path"; import consoleColours from "console-log-colors"; import { colorize } from "json-colorizer"; -import fs from "node:fs"; -import path from "node:path"; import { type ClassOptions, type Config, @@ -18,6 +16,12 @@ function getTime(): string { return date.toISOString().replace(/.*T(.*)Z/, "$1"); } +function getDate(): string { + const now = new Date(); + const date = new Date(now.getTime() - now.getTimezoneOffset() * 60000); + return date.toISOString().split("T")[0]; +} + export class Logger implements Config { name: string; fileWriter?: fs.WriteStream; @@ -27,11 +31,17 @@ export class Logger implements Config { logLevel: 1, suppressLoggerWarning: false, }; - files: FilesOptions = { enabled: false }; + files: FilesOptions = { + enabled: false, + format: "{time} {level} {message}", + naming: "{time}", + }; constructor( name: string, options?: Partial, - files?: Partial | { enabled: false }, + files?: + | { enabled: false } + | (Partial & { enabled: true; path: string }), ) { this.name = name; @@ -69,22 +79,23 @@ export class Logger implements Config { if (typeof this.files.format === "undefined") this.files.format === "{time} {level} {message}"; if (typeof this.files.naming === "undefined") - this.files.naming === "json"; + this.files.naming === "{date}-{time}"; if (typeof this.files.noConsole === "undefined") this.files.noConsole === false; - if (typeof this.files.path === "undefined") this.files.path === "json"; if (typeof this.files.type === "undefined") this.files.type === "json"; - if (!path.isAbsolute(this.files.path)) { + if (!path.isAbsolute(this.files.path + `.` + this.files.type)) { this.internal( "Presuming that path provided is from project root. Using following path:", path.join(process.cwd(), this.files.path), ); - this.files.path = path.join(process.cwd(), this.files.path); + this.files.path = path.join(process.cwd(), this.files.path + `.` + this.files.type); - this.fileWriter = fs.createWriteStream(this.files.path, { - flags: "a", - }); + if (fs.existsSync(this.files.path)) + + this.fileWriter = fs.createWriteStream(this.files.path + `.` + this.files.type, { + flags: "a", + }); this.fileWriter.write("["); } @@ -93,7 +104,9 @@ export class Logger implements Config { this.fileWriter.write("]"); } - this.fileWriter.close(); + this.fileWriter.close( + err => { if (err) { throw err } } + ); }); } } @@ -118,7 +131,11 @@ export class Logger implements Config { })},`, ); } else { - this.fileWriter.write(); + let data: string; + + data = this.files.format.replace("{date}", getDate()).replace("{time}", getTime()).replace("{level}", Level[level].toLocaleUpperCase()).replace("{message}", Data) + + this.fileWriter.write(data + `\n`); } } @@ -145,6 +162,11 @@ export class Logger implements Config { * @param data Any information to be logged */ public trace(...data: any[]) { + if (this.options.logLevel > Level.Trace) return + if (this.files.enabled === true) { + this.writeFile(Level.Trace, ...data) + if (this.files.noConsole === true) return + } return this.log( `${consoleColours.grey(getTime())} ${consoleColours.underline(this.name)} ${consoleColours.blue("[Trace]")}`, ...data, @@ -155,6 +177,11 @@ export class Logger implements Config { * @param data Any information to be logged */ public debug(...data: any[]) { + if (this.options.logLevel > Level.Debug) return + if (this.files.enabled === true) { + this.writeFile(Level.Debug, ...data) + if (this.files.noConsole === true) return + } return this.log( `${consoleColours.grey(getTime())} ${consoleColours.underline(this.name)} ${consoleColours.cyan("[Debug]")}`, ...data, @@ -165,6 +192,11 @@ export class Logger implements Config { * @param data Any information to be logged */ public info(...data: any[]) { + if (this.options.logLevel > Level.Info) return + if (this.files.enabled === true) { + this.writeFile(Level.Info, ...data) + if (this.files.noConsole === true) return + } return this.log( `${consoleColours.grey(getTime())} ${consoleColours.underline(this.name)} ${consoleColours.blueBright("[Info]")}`, ...data, @@ -175,6 +207,11 @@ export class Logger implements Config { * @param data Any information to be logged */ public warn(...data: any[]) { + if (this.options.logLevel > Level.Warn) return + if (this.files.enabled === true) { + this.writeFile(Level.Warn, ...data) + if (this.files.noConsole === true) return + } return this.log( `${consoleColours.grey(getTime())} ${consoleColours.underline(this.name)} ${consoleColours.yellow("[Warning]")}`, ...data, @@ -185,6 +222,11 @@ export class Logger implements Config { * @param data Any information to be logged */ public error(...data: any[]) { + if (this.options.logLevel > Level.Error) return + if (this.files.enabled === true) { + this.writeFile(Level.Error, ...data) + if (this.files.noConsole === true) return + } return this.log( `${consoleColours.grey(getTime())} ${consoleColours.underline(this.name)} ${consoleColours.redBright("[Error]")}`, ...data, @@ -195,6 +237,11 @@ export class Logger implements Config { * @param data Any information to be logged */ public fatal(...data: any[]) { + if (this.options.logLevel > Level.Fatal) return + if (this.files.enabled === true) { + this.writeFile(Level.Fatal, ...data) + if (this.files.noConsole === true) return + } return this.log( `${consoleColours.grey(getTime())} ${consoleColours.underline(this.name)} ${consoleColours.bgRed("[Fatal]")}`, ...data, @@ -202,10 +249,15 @@ export class Logger implements Config { } /** * Internal logging to pass to the user. - * @todo Add a option to disable this logging + * @note Cannot be disabled without disabling logging altogether, possibly a change? * @param data Any information to be logged */ private internal(...data: any[]) { + if (this.options.logLevel > Level.Internal) return + if (this.files.enabled === true) { + this.writeFile(Level.Internal, ...data) + if (this.files.noConsole === true) return + } return this.log( `${consoleColours.grey(getTime())} ${consoleColours.underline("Logger")} ${consoleColours.green("[Internal]")}`, ...data,