(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

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;