Initial commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
test.ts
|
||||
7
deno.json
Normal file
7
deno.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"imports": {
|
||||
"@std/assert": "jsr:@std/assert@1",
|
||||
"chalk": "npm:chalk@^5.3.0"
|
||||
},
|
||||
"exports": "./mod.ts"
|
||||
}
|
||||
30
deno.lock
generated
Normal file
30
deno.lock
generated
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"version": "4",
|
||||
"specifiers": {
|
||||
"jsr:@std/assert@1": "1.0.8",
|
||||
"jsr:@std/internal@^1.0.5": "1.0.5",
|
||||
"npm:chalk@^5.3.0": "5.3.0"
|
||||
},
|
||||
"jsr": {
|
||||
"@std/assert@1.0.8": {
|
||||
"integrity": "ebe0bd7eb488ee39686f77003992f389a06c3da1bbd8022184804852b2fa641b",
|
||||
"dependencies": [
|
||||
"jsr:@std/internal"
|
||||
]
|
||||
},
|
||||
"@std/internal@1.0.5": {
|
||||
"integrity": "54a546004f769c1ac9e025abd15a76b6671ddc9687e2313b67376125650dc7ba"
|
||||
}
|
||||
},
|
||||
"npm": {
|
||||
"chalk@5.3.0": {
|
||||
"integrity": "sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w=="
|
||||
}
|
||||
},
|
||||
"workspace": {
|
||||
"dependencies": [
|
||||
"jsr:@std/assert@1",
|
||||
"npm:chalk@^5.3.0"
|
||||
]
|
||||
}
|
||||
}
|
||||
68
mod.ts
Normal file
68
mod.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import chalk from "chalk";
|
||||
|
||||
class Logging {
|
||||
|
||||
visible: boolean
|
||||
source: string
|
||||
|
||||
/**
|
||||
* @param {string} source Module identifier. Used in every line to identify the module that sent the message.
|
||||
* @param {boolean} silent Set to false to log a message when the logger instantiates. Useful for debugging.
|
||||
* @returns A source for logging messages to the console. Functions for info, warnings, errors, debug statements, and network events are provided and have shorthands.
|
||||
*/
|
||||
constructor(source: string, silent?: boolean) {
|
||||
this.visible = true;
|
||||
this.source = source;
|
||||
if (typeof silent == 'boolean' && !silent) this.info(`Instantiated module logging`);
|
||||
}
|
||||
|
||||
log(type: MessageType, ...msgs: string[]) {
|
||||
if (type == MessageType.Info) this.i(...msgs);
|
||||
if (type == MessageType.Warn) this.w(...msgs);
|
||||
if (type == MessageType.Error) this.e(...msgs);
|
||||
if (type == MessageType.Debug) this.d(...msgs);
|
||||
if (type == MessageType.Network) this.n(...msgs);
|
||||
}
|
||||
|
||||
i(...msgs: string[]) {this.info(...msgs);}
|
||||
info(...msgs: string[]) {
|
||||
if (this.visible !== true) return;
|
||||
console.log(chalk.gray(`${new Date().toISOString()} `) + chalk.bgWhite.black(`${this.source} [INFO]`) + chalk.whiteBright(' ' + msgs.join(' ')));
|
||||
}
|
||||
|
||||
w(...msgs: string[]) {this.warn(...msgs);}
|
||||
warn(...msgs: string[]) {
|
||||
if (this.visible !== true) return;
|
||||
console.warn(chalk.gray(`${new Date().toISOString()} `) + chalk.bgYellow.black(`${this.source} [WARN]`) + chalk.yellowBright(' ' + msgs.join(' ')));
|
||||
}
|
||||
|
||||
e(...msgs: string[]) {this.error(...msgs);}
|
||||
error(...msgs: string[]) {
|
||||
if (this.visible !== true) return;
|
||||
console.error(chalk.gray(`${new Date().toISOString()} `) + chalk.bgRed.black(`${this.source} [ERROR]`) + chalk.redBright(' ' + msgs.join(' ')));
|
||||
}
|
||||
|
||||
d(...msgs: string[]) {this.debug(...msgs);}
|
||||
debug(...msgs: string[]) {
|
||||
if (this.visible !== true) return;
|
||||
console.debug(chalk.gray(`${new Date().toISOString()} `) + chalk.bgGreen.black(`${this.source} [DEBUG]`) + chalk.greenBright(' ' + msgs.join(' ')));
|
||||
}
|
||||
|
||||
n(...msgs: string[]) {this.network(...msgs);}
|
||||
network(...msgs: string[]) {
|
||||
if (this.visible !== true) return;
|
||||
console.log(chalk.gray(`${new Date().toISOString()} `) + chalk.bgCyan.black(`${this.source} [NETWORK]`) + chalk.cyanBright(' ' + msgs.join(' ')));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enum MessageType {
|
||||
Info,
|
||||
Warn,
|
||||
Error,
|
||||
Debug,
|
||||
Network
|
||||
}
|
||||
|
||||
export { MessageType };
|
||||
export default Logging;
|
||||
Reference in New Issue
Block a user