@palmares/logging
Version:
Palmares logging interface so you can log it to the console or other places as well
186 lines (180 loc) • 6.14 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/logger.ts
var logger_exports = {};
__export(logger_exports, {
Logger: () => Logger
});
module.exports = __toCommonJS(logger_exports);
// src/config.ts
function getLoggersAtLevel(level) {
return globalThis.$PLogging?.[level];
}
__name(getLoggersAtLevel, "getLoggersAtLevel");
// src/constants.ts
var FRAMEWORK_NAME = "palmares";
var stringByMessageType = {
debug: "\x1B[35mDEBUG\x1B[0m",
log: "\x1B[32mLOG\x1B[0m",
info: "\x1B[36mINFO\x1B[0m",
warn: "\x1B[33mWARN\x1B[0m",
error: "\x1B[31mERROR\x1B[0m"
};
// src/defaults.ts
function getDefaultFormattedMessage(args) {
return `\x1B[32m[${args.frameworkName}]\x1B[0m \x1B[33m${args.created}\x1B[0m \x1B[1m[${args.domain}]\x1B[0m${args.name && args.name.length > 0 ? ` \x1B[4m[${args.name}]\x1B[0m ` : " "}${stringByMessageType[args.logLevel.toLowerCase()]} ${args.message}`;
}
__name(getDefaultFormattedMessage, "getDefaultFormattedMessage");
// src/logger.ts
var Logger = class _Logger {
static {
__name(this, "Logger");
}
name = "";
domainName = "";
loggingMessages;
constructor(args, loggingMessages) {
this.domainName = args.domainName || "";
this.name = args.name || "";
this.loggingMessages = loggingMessages;
}
/**
* Retrieve a child logger from the current logger.
*
* @example
* ```ts
* const logger = new Logger({ domainName: 'my-custom-package' });
* const childLogger = logger.getChildLogger('migrations');
* ```
*
* @param name - The name of the child logger.
*
* @returns A new logger instance with the given name.
*/
getChildLogger(name, loggingMessages) {
const concatenatedLoggingMessages = this.loggingMessages && loggingMessages ? {
...this.loggingMessages,
...loggingMessages
} : this.loggingMessages ? this.loggingMessages : loggingMessages;
return new _Logger({
domainName: this.domainName,
name
}, concatenatedLoggingMessages);
}
/**
* If you don't use the `loggingMessages` property in the constructor, you can use this method to add logging
* messages to the logger.
*
* On here you are only able to add logging messages one by one. So you need to call this method for each
* logging message you want to add.
* This will work as a builder pattern so you can append as many logging messages as you want.
*
* @example
* ```ts
* const logger = new Logger({ name: 'my-logger' })
* .appendLogMessage('my-logging-message', {
* category: 'debug',
* handler: (args: { databaseName: string }) => `Hello World! ${args.databasename}`,
* });
*
* logger.logMessage('my-logging-message', { databaseName: 'my-database' });
* ```
*
* @param name - The name of the logging message.
* @param log - The logging function that will be called when the `logMessage` method is called.
*
* @returns - The current logger instance.
*/
appendLogMessage(name, log) {
this.loggingMessages[name] = log;
return this;
}
/**
* This will log the message that was added to the logger using the `appendLogMessage` method or the
* `loggingMessages` property in the constructor.
*
* @example
* ```ts
* const logger = new Logger({ name: 'my-logger' }, {
* 'my-logging-message': {
* category: 'debug',
* handler: (args: { databaseName: string }) => `Hello World! ${args.databasename}`,
* },
* });
*
* logger.logMessage('my-logging-message', { databaseName: 'my-database' });
* ```
*
* @param name - The name of the logging message.
* @param args - The arguments that will be passed to the handler function.
*/
logMessage(name, args) {
if (this.loggingMessages?.[name]) this.#log(this.loggingMessages[name].handler(args), this.loggingMessages[name].category);
}
/**
* This is the main logging method. This will log the message to the console or to a file or a database,
* or even an api.
*
* With this method we can reuse the same logic over and over again on each logging type.
*
* @param message - The message to be logged.
* @param level - The level of the message to be logged.
*
* @private
*/
#log(message, level) {
const data = {
message,
created: (/* @__PURE__ */ new Date()).toISOString(),
domain: this.domainName,
name: this.name,
frameworkName: FRAMEWORK_NAME,
logLevel: level.toUpperCase()
};
const loggers = getLoggersAtLevel(level);
if (!Array.isArray(loggers)) return;
for (const logger of loggers) {
const shouldLog = logger.filter ? logger.filter(data) : true;
if (!shouldLog) continue;
const formattedMessage = logger.formatter ? logger.formatter(data) : getDefaultFormattedMessage(data);
if (Array.isArray(logger.logger)) return logger.logger.forEach((logger2) => logger2.log(formattedMessage));
else return logger.logger.log(formattedMessage);
}
}
log(message) {
this.#log(message, "log");
}
info(message) {
this.#log(message, "info");
}
debug(message) {
this.#log(message, "debug");
}
warn(message) {
this.#log(message, "warn");
}
error(message) {
this.#log(message, "error");
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Logger
});