add documentation, fix package meta, bump the minor version, why not - yolo
This commit is contained in:
32
README.md
Normal file
32
README.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Undead Logging
|
||||||
|
Logging for stupid idiots like me
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import Logging from "@proxnet/undead-logging"
|
||||||
|
|
||||||
|
const log = new Logging("Main");
|
||||||
|
|
||||||
|
log.i("Hello World!");
|
||||||
|
```
|
||||||
|
|
||||||
|
Disable a source:
|
||||||
|
```ts
|
||||||
|
// .. logging source is in the scope
|
||||||
|
log.visible = false;
|
||||||
|
|
||||||
|
log.debug("I can't be seen!");
|
||||||
|
// no output
|
||||||
|
```
|
||||||
|
|
||||||
|
Change a source's name:
|
||||||
|
```ts
|
||||||
|
// .. logging source "Main" is in the scope
|
||||||
|
|
||||||
|
log.n("Network is networking");
|
||||||
|
log.source = "Main2";
|
||||||
|
log.n("Something happened");
|
||||||
|
|
||||||
|
// output:
|
||||||
|
// 2024-11-14T01:21:40.350Z Main [NETWORK] Network is networking
|
||||||
|
// 2024-11-14T01:21:40.350Z Main2 [NETWORK] Something happened
|
||||||
|
```
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
"chalk": "npm:chalk@^5.3.0"
|
"chalk": "npm:chalk@^5.3.0"
|
||||||
},
|
},
|
||||||
"exports": "./mod.ts",
|
"exports": "./mod.ts",
|
||||||
"version": "1.0.0",
|
"version": "1.1.0",
|
||||||
"name": "@proxnet/undead-logging",
|
"name": "@proxnet/undead-logging",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
|
|||||||
39
mod.ts
39
mod.ts
@@ -1,13 +1,24 @@
|
|||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A source for pretty and cool and fun logging
|
||||||
|
*/
|
||||||
class Logging {
|
class Logging {
|
||||||
|
|
||||||
|
/** Can I be seen by the console? */
|
||||||
visible: boolean
|
visible: boolean
|
||||||
|
/** What is my name? */
|
||||||
source: string
|
source: string
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} source Module identifier. Used in every line to identify the module that sent the message.
|
* Create a logging source
|
||||||
* @param {boolean} silent Set to false to log a message when the logger instantiates. Useful for debugging.
|
* ```ts
|
||||||
|
* const log = new Logging("Main");
|
||||||
|
*
|
||||||
|
* log.i("Hello World!");
|
||||||
|
* ```
|
||||||
|
* @param source Module identifier. Used in every line to identify the module that sent the message.
|
||||||
|
* @param 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.
|
* @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) {
|
constructor(source: string, silent?: boolean) {
|
||||||
@@ -16,6 +27,11 @@ class Logging {
|
|||||||
if (typeof silent == 'boolean' && !silent) this.info(`Instantiated module logging`);
|
if (typeof silent == 'boolean' && !silent) this.info(`Instantiated module logging`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param type The kind of message to log
|
||||||
|
* @param msgs Your message(s)
|
||||||
|
*/
|
||||||
log(type: MessageType, ...msgs: string[]) {
|
log(type: MessageType, ...msgs: string[]) {
|
||||||
if (type == MessageType.Info) this.i(...msgs);
|
if (type == MessageType.Info) this.i(...msgs);
|
||||||
if (type == MessageType.Warn) this.w(...msgs);
|
if (type == MessageType.Warn) this.w(...msgs);
|
||||||
@@ -24,31 +40,41 @@ class Logging {
|
|||||||
if (type == MessageType.Network) this.n(...msgs);
|
if (type == MessageType.Network) this.n(...msgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Logging Function */
|
||||||
i(...msgs: string[]) {this.info(...msgs);}
|
i(...msgs: string[]) {this.info(...msgs);}
|
||||||
|
/** Logging Function */
|
||||||
info(...msgs: string[]) {
|
info(...msgs: string[]) {
|
||||||
if (this.visible !== true) return;
|
if (this.visible !== true) return;
|
||||||
console.log(chalk.gray(`${new Date().toISOString()} `) + chalk.bgWhite.black(`${this.source} [INFO]`) + chalk.whiteBright(' ' + msgs.join(' ')));
|
console.log(chalk.gray(`${new Date().toISOString()} `) + chalk.bgWhite.black(`${this.source} [INFO]`) + chalk.whiteBright(' ' + msgs.join(' ')));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Logging Function */
|
||||||
w(...msgs: string[]) {this.warn(...msgs);}
|
w(...msgs: string[]) {this.warn(...msgs);}
|
||||||
|
/** Logging Function */
|
||||||
warn(...msgs: string[]) {
|
warn(...msgs: string[]) {
|
||||||
if (this.visible !== true) return;
|
if (this.visible !== true) return;
|
||||||
console.warn(chalk.gray(`${new Date().toISOString()} `) + chalk.bgYellow.black(`${this.source} [WARN]`) + chalk.yellowBright(' ' + msgs.join(' ')));
|
console.warn(chalk.gray(`${new Date().toISOString()} `) + chalk.bgYellow.black(`${this.source} [WARN]`) + chalk.yellowBright(' ' + msgs.join(' ')));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Logging Function */
|
||||||
e(...msgs: string[]) {this.error(...msgs);}
|
e(...msgs: string[]) {this.error(...msgs);}
|
||||||
|
/** Logging Function */
|
||||||
error(...msgs: string[]) {
|
error(...msgs: string[]) {
|
||||||
if (this.visible !== true) return;
|
if (this.visible !== true) return;
|
||||||
console.error(chalk.gray(`${new Date().toISOString()} `) + chalk.bgRed.black(`${this.source} [ERROR]`) + chalk.redBright(' ' + msgs.join(' ')));
|
console.error(chalk.gray(`${new Date().toISOString()} `) + chalk.bgRed.black(`${this.source} [ERROR]`) + chalk.redBright(' ' + msgs.join(' ')));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Logging Function */
|
||||||
d(...msgs: string[]) {this.debug(...msgs);}
|
d(...msgs: string[]) {this.debug(...msgs);}
|
||||||
|
/** Logging Function */
|
||||||
debug(...msgs: string[]) {
|
debug(...msgs: string[]) {
|
||||||
if (this.visible !== true) return;
|
if (this.visible !== true) return;
|
||||||
console.debug(chalk.gray(`${new Date().toISOString()} `) + chalk.bgGreen.black(`${this.source} [DEBUG]`) + chalk.greenBright(' ' + msgs.join(' ')));
|
console.debug(chalk.gray(`${new Date().toISOString()} `) + chalk.bgGreen.black(`${this.source} [DEBUG]`) + chalk.greenBright(' ' + msgs.join(' ')));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Logging Function */
|
||||||
n(...msgs: string[]) {this.network(...msgs);}
|
n(...msgs: string[]) {this.network(...msgs);}
|
||||||
|
/** Logging Function */
|
||||||
network(...msgs: string[]) {
|
network(...msgs: string[]) {
|
||||||
if (this.visible !== true) return;
|
if (this.visible !== true) return;
|
||||||
console.log(chalk.gray(`${new Date().toISOString()} `) + chalk.bgCyan.black(`${this.source} [NETWORK]`) + chalk.cyanBright(' ' + msgs.join(' ')));
|
console.log(chalk.gray(`${new Date().toISOString()} `) + chalk.bgCyan.black(`${this.source} [NETWORK]`) + chalk.cyanBright(' ' + msgs.join(' ')));
|
||||||
@@ -56,6 +82,9 @@ class Logging {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Useful for conditional logging in the `log` function.
|
||||||
|
*/
|
||||||
enum MessageType {
|
enum MessageType {
|
||||||
Info,
|
Info,
|
||||||
Warn,
|
Warn,
|
||||||
|
|||||||
Reference in New Issue
Block a user