(init): Add initial logger files.
This commit is contained in:
44
src/Logger.ts
Normal file
44
src/Logger.ts
Normal 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
3
src/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './Logger';
|
||||
export * from './typings/LogLevel';
|
||||
export * from './typings/Transport';
|
||||
9
src/typings/LogLevel.ts
Normal file
9
src/typings/LogLevel.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
enum LogLevel {
|
||||
Debug,
|
||||
Information,
|
||||
Warning,
|
||||
Error,
|
||||
Fatal,
|
||||
}
|
||||
|
||||
export default LogLevel;
|
||||
15
src/typings/Transport.ts
Normal file
15
src/typings/Transport.ts
Normal 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;
|
||||
Reference in New Issue
Block a user