Skip to content

Commit

Permalink
- added get date function
Browse files Browse the repository at this point in the history
- updated files options in class declarence
- updated the default values definition
- error handling in file close
- added logging for other file formats
- added loglevel making a difference once again

Signed-off-by: TinnyTerr <168141209+TinnyTerr@users.noreply.github.com>
  • Loading branch information
TinnyTerr committed Oct 21, 2024
1 parent 982b042 commit 354d794
Showing 1 changed file with 66 additions and 14 deletions.
80 changes: 66 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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<UserOptions>,
files?: Partial<FilesOptions> | { enabled: false },
files?:
| { enabled: false }
| (Partial<FilesOptions> & { enabled: true; path: string }),
) {
this.name = name;

Expand Down Expand Up @@ -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("[");
}

Expand All @@ -93,7 +104,9 @@ export class Logger implements Config {
this.fileWriter.write("]");
}

this.fileWriter.close();
this.fileWriter.close(
err => { if (err) { throw err } }
);
});
}
}
Expand All @@ -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`);
}
}

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -195,17 +237,27 @@ 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,
);
}
/**
* 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,
Expand Down

0 comments on commit 354d794

Please sign in to comment.