@node-elion/utils
Version:
Super scalable enterprise Node.js server library
123 lines • 3.84 kB
JavaScript
import { LoggerLevelsArray } from "./types.js";
export class Logger {
options;
name;
nameLevel;
callback;
level;
static child(options) {
return new Logger(options);
}
constructor(options) {
this.options = options || {};
if (this.options.parentLogger) {
this.name = this.options.name ?? "";
this.nameLevel = [
...this.options.parentLogger.nameLevel,
this.name,
];
this.level = options.level ?? this.options.parentLogger.level;
this.callback = this.options.parentLogger.callback.bind(this.options.parentLogger);
}
else {
this.level = this.options.level ?? "info";
this.callback = this.options.callback ?? (() => { });
this.name = this.options.name ?? "";
this.nameLevel = [this.name];
}
}
child(arg) {
if (typeof arg === "string") {
return this.child({
name: arg,
parentLogger: this,
});
}
return new Logger({
...arg,
parentLogger: this,
});
}
execute(level, ...message) {
if (LoggerLevelsArray.indexOf(level) <=
LoggerLevelsArray.indexOf(this.level)) {
this.callback({
level,
message,
meta: {
level: this.nameLevel,
},
});
}
}
/**
* Logs error messages.
* Use this logger level to log errors that occur during your program's execution.
* @param message The error message(s) to log
*/
error(...message) {
this.execute("error", ...message);
}
/**
* Logs warning messages.
* Use this logger level to log non-error messages that could potentially lead to application errors.
* @param message The warning message(s) to log
*/
warn(...message) {
this.execute("warn", ...message);
}
/**
* Logs basic information messages.
* Use this logger level for informational messages to track your application's normal behavior.
* @param message The information message(s) to log
*/
info(...message) {
this.execute("info", ...message);
}
/**
* Also logs informational messages. The same as the info logger level.
* @param message The message(s) to log
*/
log(...message) {
this.execute("info", ...message);
}
/**
* Logs HTTP requests.
* Use this logger level to log the details of HTTP requests and responses.
* @param message The HTTP request/response message(s) to log
*/
http(...message) {
this.execute("http", ...message);
}
/**
* Provides detailed logs. Use this logger level for detailed debug information beyond what you'd log for debugging.
* @param message The message(s) to log in verbose mode
*/
verbose(...message) {
this.execute("verbose", ...message);
}
/**
* Logs debug-level messages. Use this logger level to log information helpful in debugging.
* @param message The debug message(s) to log
*/
debug(...message) {
this.execute("debug", ...message);
}
/**
* Provides the most detailed logs.
* This is the highest level of logging and includes all levels of messages.
* @param message The message(s) to log in silly mode
*/
silly(...message) {
this.execute("silly", ...message);
}
/**
* Also provides the most detailed logs.
* The same as the silly logger level.
* @param message The message(s) to log in trace mode
*/
trace(...message) {
this.execute("silly", ...message);
}
}
//# sourceMappingURL=logger.js.map