"use strict"; 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/index.ts var src_exports = {}; __export(src_exports, { Logger: () => Logger, LoggingAdapter: () => LoggingAdapter, default: () => loggingDomain, loggingAdapter: () => loggingAdapter, loggingDomain: () => loggingDomain, removeColorsFormatting: () => removeColorsFormatting }); module.exports = __toCommonJS(src_exports); // src/adapter.ts function loggingAdapter(args) { const ModifiedClass = class extends LoggingAdapter { static { __name(this, "ModifiedClass"); } log = args.log; debug = args.debug; info = args.info; warn = args.warn; error = args.error; }; return ModifiedClass; } __name(loggingAdapter, "loggingAdapter"); var LoggingAdapter = class { static { __name(this, "LoggingAdapter"); } /** * Log level log message. * * See: https://developer.mozilla.org/en-US/docs/Web/API/console/log * * @example * ```ts * logger.log('Hello World'); * ``` * * @param _message - The message to be logged. */ log(_message) { return void 0; } /** * Log level debug message. * * See: https://developer.mozilla.org/en-US/docs/Web/API/console/debug * * @example * ```ts * logger.debug('Hello World'); * ``` * * @param _message - The message to be logged. */ debug(_message) { return void 0; } /** * Log level warn message. * * See: https://developer.mozilla.org/en-US/docs/Web/API/console/warn * * @example * ```ts * logger.warn('Hello World'); * ``` * * @param _message - The message to be logged. */ warn(_message) { return void 0; } /** * Log level error message. * * See: https://developer.mozilla.org/en-US/docs/Web/API/console/error * * @example * ```ts * logger.error('Hello World'); * ``` * * @param _message - The message to be logged. */ error(_message) { return void 0; } /** * Log level info message. * * See: https://developer.mozilla.org/en-US/docs/Web/API/console/info * * @example * ```ts * logger.info('Hello World'); * ``` * * @param _message - The message to be logged. */ info(_message) { return void 0; } }; // src/config.ts function setLoggerAtLevel(level, logger) { if (typeof globalThis.$PLogging !== "object") globalThis.$PLogging = {}; if (globalThis.$PLogging[level]) globalThis.$PLogging[level].push(logger); else globalThis.$PLogging[level] = [ logger ]; } __name(setLoggerAtLevel, "setLoggerAtLevel"); 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"); } }; // src/utils.ts function removeColorsFormatting(message) { return message.replace(/\x1b\[\d+m/g, ""); } __name(removeColorsFormatting, "removeColorsFormatting"); // src/domain.ts var import_core = require("@palmares/core"); var loggingDomain = (0, import_core.domain)("@palmares/logging", "", { load: /* @__PURE__ */ __name(async (settings) => { if (settings?.logger) { if (settings.logger instanceof Promise || Array.isArray(settings.logger) === false) { const logger = settings.logger instanceof Promise ? (await settings.logger).default : settings.logger; const loggerData = { logger: new logger() }; setLoggerAtLevel("debug", loggerData); setLoggerAtLevel("log", loggerData); setLoggerAtLevel("info", loggerData); setLoggerAtLevel("warn", loggerData); setLoggerAtLevel("error", loggerData); } else if (Array.isArray(settings.logger)) { for (const logger of settings.logger) { const initializedLogger = Array.isArray(logger.logger) ? logger.logger.map((loggerConstructor) => { console.log(loggerConstructor); return new loggerConstructor(); }) : new logger.logger(); logger.logger = initializedLogger; const loggingLevels = Array.isArray(logger.level) ? logger.level : typeof logger.level === "string" ? [ logger.level ] : [ "debug", "log", "info", "warn", "error" ]; for (const level of loggingLevels) setLoggerAtLevel(level, { logger: logger.logger }); } } settings.__logger = Logger; } }, "load") }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Logger, LoggingAdapter, loggingAdapter, loggingDomain, removeColorsFormatting });