From b6817977591643d81d0991959adfc06db4a4ca4c Mon Sep 17 00:00:00 2001 From: "Nix \"UwU\" Krystik" Date: Wed, 11 Mar 2026 18:12:55 +0800 Subject: [PATCH] (init): Add initial logger files. --- .gitattributes | 1 + .gitignore | 3 ++ .npmignore | 10 +++++++ .prettierrc | 8 ++++++ package-lock.json | 62 ++++++++++++++++++++++++++++++++++++++++ package.json | 31 ++++++++++++++++++++ src/Logger.ts | 44 ++++++++++++++++++++++++++++ src/index.ts | 3 ++ src/typings/LogLevel.ts | 9 ++++++ src/typings/Transport.ts | 15 ++++++++++ tsconfig.json | 18 ++++++++++++ 11 files changed, 204 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 .prettierrc create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/Logger.ts create mode 100644 src/index.ts create mode 100644 src/typings/LogLevel.ts create mode 100644 src/typings/Transport.ts create mode 100644 tsconfig.json diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..94f480d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..680be6e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# Directories +node_modules/ +dist/ \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..32b0028 --- /dev/null +++ b/.npmignore @@ -0,0 +1,10 @@ +# Git +.gitignore +.gitattributes + +# Prettier +.prettierrc + +# Node +node_modules/ +package_lock.json \ No newline at end of file diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..b2eb07a --- /dev/null +++ b/.prettierrc @@ -0,0 +1,8 @@ +{ + "semi": true, + "singleQuote": true, + "printWidth": 100, + "tabWidth": 2, + "trailingComma": "all", + "arrowParens": "always" +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..79b1b4a --- /dev/null +++ b/package-lock.json @@ -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" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..6c504e9 --- /dev/null +++ b/package.json @@ -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" + } +} \ No newline at end of file diff --git a/src/Logger.ts b/src/Logger.ts new file mode 100644 index 0000000..cee05aa --- /dev/null +++ b/src/Logger.ts @@ -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 { + const tasks: Promise[] = []; + + 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; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..6358932 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,3 @@ +export * from './Logger'; +export * from './typings/LogLevel'; +export * from './typings/Transport'; diff --git a/src/typings/LogLevel.ts b/src/typings/LogLevel.ts new file mode 100644 index 0000000..145f2de --- /dev/null +++ b/src/typings/LogLevel.ts @@ -0,0 +1,9 @@ +enum LogLevel { + Debug, + Information, + Warning, + Error, + Fatal, +} + +export default LogLevel; diff --git a/src/typings/Transport.ts b/src/typings/Transport.ts new file mode 100644 index 0000000..8e36a73 --- /dev/null +++ b/src/typings/Transport.ts @@ -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; +} + +export default Transport; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..833daff --- /dev/null +++ b/tsconfig.json @@ -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"] +} \ No newline at end of file