This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.ts
53 lines (45 loc) · 2.31 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { brightBlue, cyan, format, green, brightCyan, yellow, red, ensureDirSync } from "./deps.ts";
export default class GLogger {
private _location: string = "";
private _logToFile: boolean;
private _logFolder: string;
private _date: string;
constructor(location: string, logToFile: boolean = false, logFolder: string = "logs") {
this._location = location;
this._logToFile = logToFile;
this._logFolder = logFolder;
this._date = format(new Date(), "yyyy|MM|dd");
}
info(message: string): void {
let noColorMsg = `[${format(new Date(), "yyyy/MM/dd h:mm:ss a")}] [${this._location}] [INFO] ${message}`;
console.log(`${cyan(("[" + format(new Date(), "yyyy/MM/dd h:mm:ss a") + "]"))} ${brightBlue(("[" + this._location + "]"))} ${green("[INFO]")} ${message}`);
if (this._logToFile) {
this.writeToFile(noColorMsg);
}
}
debug(message: string): void {
let noColorMsg = `[${format(new Date(), "yyyy/MM/dd h:mm:ss a")}] [${this._location}] [DEBUG] ${message}`;
console.log(`${cyan(("[" + format(new Date(), "yyyy/MM/dd h:mm:ss a") + "]"))} ${brightBlue(("[" + this._location + "]"))} ${brightCyan("[DEBUG]")} ${message}`);
if (this._logToFile) {
this.writeToFile(noColorMsg);
}
}
warn(message: string): void {
let noColorMsg = `[${format(new Date(), "yyyy/MM/dd h:mm:ss a")}] [${this._location}] [WARN] ${message}`;
console.log(`${cyan(("[" + format(new Date(), "yyyy/MM/dd h:mm:ss a") + "]"))} ${brightBlue(("[" + this._location + "]"))} ${yellow("[WARN]")} ${message}`);
if (this._logToFile) {
this.writeToFile(noColorMsg);
}
}
error(message: string): void {
let noColorMsg = `[${format(new Date(), "yyyy/MM/dd h:mm:ss a")}] [${this._location}] [ERROR] ${message}`;
console.log(`${cyan(("[" + format(new Date(), "yyyy/MM/dd h:mm:ss a") + "]"))} ${brightBlue(("[" + this._location + "]"))} ${red("[ERROR]")} ${message}`);
if (this._logToFile) {
this.writeToFile(noColorMsg);
}
}
private writeToFile(noColorMsg: string): void {
ensureDirSync(this._logFolder);
Deno.writeTextFileSync(`${this._logFolder}/${this._date}.log`, noColorMsg + "\n", {append: true, create: true});
}
}