UNPKG

jorel

Version:

A unified wrapper for working with LLMs from multiple providers, including streams, images, documents & automatic tool use.

150 lines (149 loc) 6.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LogService = void 0; const winston_1 = require("winston"); /** * A service for logging messages to the console or other destinations. */ class LogService { constructor(loggerOption = "console", logLevel = "info") { this.logger = this.initializeLogger(loggerOption, logLevel); } /** * Gets the current logging level of the logger. * @returns The current log level. */ get logLevel() { return this.logger.level; } /** * Updates the logging level of the logger dynamically. * Useful for changing verbosity without recreating the logger. * @param logLevel The new log level to set. */ set logLevel(logLevel) { this.logger.level = logLevel; } /** * Clears all transports (output destinations) from the logger. * This can be useful if you want to dynamically reconfigure logging outputs. */ clear() { this.logger.clear(); } /** * Logs a message at the specified level with optional context. * Use this method for dynamically specified levels. * @param logDomain The domain or category of the log message. * @param level The log level (e.g., "info", "error"). * @param message The message to log. * @param context Optional metadata or contextual information to include. */ log(logDomain, level, message, context) { this.logger.log({ level, message, ...context, logDomain }); } /** * Logs a silly message with optional context. * Use for extremely detailed information during development and debugging. * @param logDomain The domain or category of the log message. * @param message The message to log. * @param context Optional metadata or contextual information to include. */ silly(logDomain, message, context) { if (typeof this.logger.silly === "function") this.logger.silly(message, { ...context, logDomain }); } /** * Logs a debug message with optional context. * Use for detailed information during development and debugging. * @param logDomain The domain or category of the log message. * @param message The message to log. * @param context Optional metadata or contextual information to include. */ debug(logDomain, message, context) { if (typeof this.logger.debug === "function") this.logger.debug(message, { ...context, logDomain }); } /** * Logs a verbose message with optional context. * Use for detailed operational logs that are more verbose than "info". * @param logDomain The domain or category of the log message. * @param message The message to log. * @param context Optional metadata or contextual information to include. */ verbose(logDomain, message, context) { if (typeof this.logger.verbose === "function") this.logger.verbose(message, { ...context, logDomain }); } /** * Logs an informational message with optional context. * Use for high-level operational messages and significant events. * @param logDomain The domain or category of the log message. * @param message The message to log. * @param context Optional metadata or contextual information to include. */ info(logDomain, message, context) { if (typeof this.logger.info === "function") this.logger.info(message, { ...context, logDomain }); } /** * Logs a warning message with optional context. * Use for recoverable issues or noteworthy events that might require attention. * @param logDomain The domain or category of the log message. * @param message The message to log. * @param context Optional metadata or contextual information to include. */ warn(logDomain, message, context) { if (typeof this.logger.warn === "function") this.logger.warn(message, { ...context, logDomain }); } /** * Logs an error message with optional context. * Use for critical issues or exceptions that require immediate attention. * @param logDomain The domain or category of the log message. * @param message The message to log. * @param context Optional metadata or contextual information to include. */ error(logDomain, message, context) { if (typeof this.logger.error === "function") this.logger.error(message, { ...context, logDomain }); } /** * Initializes the logger based on the provided option. * - For "console", creates a pre-configured Winston logger with colored output and timestamps. * - For custom functions, wraps them in a Winston-compatible interface. * - Uses a pre-existing Winston logger if provided. * @param loggerOption The logger option: "console", a custom function, or a pre-existing Winston logger. * @param logLevel The logging level to be set for the logger. * @returns The initialized Winston logger. * @internal */ initializeLogger(loggerOption, logLevel) { if (loggerOption === "console") { return (0, winston_1.createLogger)({ level: logLevel, format: winston_1.format.combine(winston_1.format.padLevels(), winston_1.format.colorize(), winston_1.format.timestamp(), winston_1.format.printf(({ timestamp, level, message, ...meta }) => { const { logDomain, ...context } = meta; const metaString = Object.keys(context).length ? ` | context: ${JSON.stringify(context)}` : ""; return `${timestamp} ${level} ${message} [${logDomain}]${metaString}`; })), transports: [new winston_1.transports.Console()], }); } if (typeof loggerOption === "function") { return (0, winston_1.createLogger)({ level: logLevel, transports: [ new winston_1.transports.Console({ log(info, callback) { loggerOption(info.level, info.message, info.meta); callback(); }, }), ], }); } return loggerOption; } } exports.LogService = LogService;