(init): Add initial logger files.

This commit is contained in:
Nix "UwU" Krystik
2026-03-11 18:12:55 +08:00
commit b681797759
11 changed files with 204 additions and 0 deletions

1
.gitattributes vendored Normal file
View File

@@ -0,0 +1 @@
* text=auto eol=lf

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
# Directories
node_modules/
dist/

10
.npmignore Normal file
View File

@@ -0,0 +1,10 @@
# Git
.gitignore
.gitattributes
# Prettier
.prettierrc
# Node
node_modules/
package_lock.json

8
.prettierrc Normal file
View File

@@ -0,0 +1,8 @@
{
"semi": true,
"singleQuote": true,
"printWidth": 100,
"tabWidth": 2,
"trailingComma": "all",
"arrowParens": "always"
}

62
package-lock.json generated Normal file
View File

@@ -0,0 +1,62 @@
{
"name": "@nixkrystik/logger",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@nixkrystik/logger",
"version": "1.0.0",
"dependencies": {
"chalk": "5.6.2"
},
"devDependencies": {
"@types/node": "25.4.0",
"typescript": "5.9.3"
}
},
"node_modules/@types/node": {
"version": "25.4.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-25.4.0.tgz",
"integrity": "sha512-9wLpoeWuBlcbBpOY3XmzSTG3oscB6xjBEEtn+pYXTfhyXhIxC5FsBer2KTopBlvKEiW9l13po9fq+SJY/5lkhw==",
"dev": true,
"license": "MIT",
"dependencies": {
"undici-types": "~7.18.0"
}
},
"node_modules/chalk": {
"version": "5.6.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz",
"integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==",
"license": "MIT",
"engines": {
"node": "^12.17.0 || ^14.13 || >=16.0.0"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
},
"node_modules/typescript": {
"version": "5.9.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
"dev": true,
"license": "Apache-2.0",
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
},
"engines": {
"node": ">=14.17"
}
},
"node_modules/undici-types": {
"version": "7.18.2",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.18.2.tgz",
"integrity": "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w==",
"dev": true,
"license": "MIT"
}
}
}

31
package.json Normal file
View File

@@ -0,0 +1,31 @@
{
"name": "@nixkrystik/logger",
"description": "Simple, scalable logging package.",
"version": "1.0.0",
"author": {
"name": "Nix Krystik",
"url": "https://teamhydra.dev/"
},
"repository": {
"url": "https://git.hep.gg/nix/logger"
},
"bugs": {
"url": "https://git.hep.gg/nix/logger/issues"
},
"scripts": {
"prepublish": "tsc"
},
"dependencies": {
"chalk": "5.6.2"
},
"devDependencies": {
"@types/node": "25.4.0",
"typescript": "5.9.3"
}
}

44
src/Logger.ts Normal file
View File

@@ -0,0 +1,44 @@
import LogLevel from './typings/LogLevel';
import Transport from './typings/Transport';
export class Logger {
private transports: Transport[];
constructor(transports: Transport[] = []) {
this.transports = transports;
}
async log(level: LogLevel, module: string, content: string): Promise<void> {
const tasks: Promise<void>[] = [];
for (const transport of this.transports) {
if (level >= transport.minLevel) {
tasks.push(transport.log(level, module, content));
}
}
await Promise.all(tasks);
}
debug(module: string, content: string) {
return this.log(LogLevel.Debug, module, content);
}
info(module: string, content: string) {
return this.log(LogLevel.Information, module, content);
}
warn(module: string, content: string) {
return this.log(LogLevel.Warning, module, content);
}
error(module: string, content: string) {
return this.log(LogLevel.Error, module, content);
}
fatal(module: string, content: string) {
return this.log(LogLevel.Fatal, module, content);
}
}
export default Logger;

3
src/index.ts Normal file
View File

@@ -0,0 +1,3 @@
export * from './Logger';
export * from './typings/LogLevel';
export * from './typings/Transport';

9
src/typings/LogLevel.ts Normal file
View File

@@ -0,0 +1,9 @@
enum LogLevel {
Debug,
Information,
Warning,
Error,
Fatal,
}
export default LogLevel;

15
src/typings/Transport.ts Normal file
View File

@@ -0,0 +1,15 @@
import LogLevel from './LogLevel';
interface Transport {
minLevel: LogLevel;
/**
*
* @param level The logging level.
* @param module The name of the module. This is defined when logging.
* @param content The content of the module.
*/
log(level: LogLevel, module: string, content: string): Promise<void>;
}
export default Transport;

18
tsconfig.json Normal file
View File

@@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "ES2020",
"rootDir": "src",
"outDir": "dist",
"declaration": true,
"sourceMap": true,
"strict": true,
"esModuleInterop": true,
"moduleResolution": "node"
},
"include": ["src"],
"exclude": ["node_modules"]
}