@deepracticex/logger
Advanced tools
| /** | ||
| * Runtime environment type | ||
| */ | ||
| type RuntimeEnvironment = "nodejs" | "cloudflare-workers" | "browser" | "test"; | ||
| /** | ||
| * Logger configuration interface | ||
| */ | ||
| interface LoggerConfig { | ||
| /** | ||
| * Log level threshold | ||
| * @default "info" | ||
| */ | ||
| level?: "trace" | "debug" | "info" | "warn" | "error" | "fatal"; | ||
| /** | ||
| * Enable console output | ||
| * @default true | ||
| */ | ||
| console?: boolean; | ||
| /** | ||
| * File logging configuration | ||
| * @default { dirname: "~/.deepractice/logs" } | ||
| */ | ||
| file?: boolean | { | ||
| dirname?: string; | ||
| }; | ||
| /** | ||
| * Enable colored output | ||
| * @default true | ||
| */ | ||
| colors?: boolean; | ||
| /** | ||
| * Package/service name for identification | ||
| * @default "app" | ||
| */ | ||
| name?: string; | ||
| /** | ||
| * Explicitly specify the runtime environment | ||
| * If not provided, will auto-detect | ||
| * @default auto-detect | ||
| */ | ||
| environment?: RuntimeEnvironment; | ||
| } | ||
| /** | ||
| * Logger interface - all methods accept any arguments for maximum flexibility | ||
| */ | ||
| interface Logger { | ||
| /** | ||
| * Log trace level message | ||
| */ | ||
| trace: any; | ||
| /** | ||
| * Log debug level message | ||
| */ | ||
| debug: any; | ||
| /** | ||
| * Log info level message | ||
| */ | ||
| info: any; | ||
| /** | ||
| * Log warning level message | ||
| */ | ||
| warn: any; | ||
| /** | ||
| * Log error level message | ||
| */ | ||
| error: any; | ||
| /** | ||
| * Log fatal level message | ||
| */ | ||
| fatal: any; | ||
| } | ||
| /** | ||
| * Log level type | ||
| */ | ||
| type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal"; | ||
| export type { LoggerConfig as L, Logger as a, LogLevel as b }; |
| /** | ||
| * Runtime environment type | ||
| */ | ||
| type RuntimeEnvironment = "nodejs" | "cloudflare-workers" | "browser" | "test"; | ||
| /** | ||
| * Logger configuration interface | ||
| */ | ||
| interface LoggerConfig { | ||
| /** | ||
| * Log level threshold | ||
| * @default "info" | ||
| */ | ||
| level?: "trace" | "debug" | "info" | "warn" | "error" | "fatal"; | ||
| /** | ||
| * Enable console output | ||
| * @default true | ||
| */ | ||
| console?: boolean; | ||
| /** | ||
| * File logging configuration | ||
| * @default { dirname: "~/.deepractice/logs" } | ||
| */ | ||
| file?: boolean | { | ||
| dirname?: string; | ||
| }; | ||
| /** | ||
| * Enable colored output | ||
| * @default true | ||
| */ | ||
| colors?: boolean; | ||
| /** | ||
| * Package/service name for identification | ||
| * @default "app" | ||
| */ | ||
| name?: string; | ||
| /** | ||
| * Explicitly specify the runtime environment | ||
| * If not provided, will auto-detect | ||
| * @default auto-detect | ||
| */ | ||
| environment?: RuntimeEnvironment; | ||
| } | ||
| /** | ||
| * Logger interface - all methods accept any arguments for maximum flexibility | ||
| */ | ||
| interface Logger { | ||
| /** | ||
| * Log trace level message | ||
| */ | ||
| trace: any; | ||
| /** | ||
| * Log debug level message | ||
| */ | ||
| debug: any; | ||
| /** | ||
| * Log info level message | ||
| */ | ||
| info: any; | ||
| /** | ||
| * Log warning level message | ||
| */ | ||
| warn: any; | ||
| /** | ||
| * Log error level message | ||
| */ | ||
| error: any; | ||
| /** | ||
| * Log fatal level message | ||
| */ | ||
| fatal: any; | ||
| } | ||
| /** | ||
| * Log level type | ||
| */ | ||
| type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal"; | ||
| export type { LoggerConfig as L, Logger as a, LogLevel as b }; |
+271
| "use strict"; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __esm = (fn, res) => function __init() { | ||
| return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; | ||
| }; | ||
| 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/core/test-adapter.ts | ||
| var test_adapter_exports = {}; | ||
| __export(test_adapter_exports, { | ||
| clearTestLogs: () => clearTestLogs, | ||
| createTestLogger: () => createTestLogger, | ||
| getTestLogs: () => getTestLogs, | ||
| getTestLogsByLevel: () => getTestLogsByLevel, | ||
| getTestLogsCount: () => getTestLogsCount | ||
| }); | ||
| function getMinLogLevel(level = "info") { | ||
| return LOG_LEVEL_PRIORITY[level]; | ||
| } | ||
| function shouldLog(level, minLevel) { | ||
| return LOG_LEVEL_PRIORITY[level] >= minLevel; | ||
| } | ||
| function createLogEntry(level, args, shouldOutput, minLevel) { | ||
| if (!shouldLog(level, minLevel)) { | ||
| return; | ||
| } | ||
| let message; | ||
| let context; | ||
| if (args.length === 1) { | ||
| if (typeof args[0] === "string") { | ||
| message = args[0]; | ||
| } else { | ||
| context = args[0]; | ||
| message = JSON.stringify(args[0]); | ||
| } | ||
| } else if (args.length >= 2) { | ||
| if (typeof args[0] === "object" && args[0] !== null) { | ||
| context = args[0]; | ||
| message = args.slice(1).join(" "); | ||
| } else { | ||
| message = args.join(" "); | ||
| } | ||
| } else { | ||
| message = ""; | ||
| } | ||
| capturedLogs.push({ | ||
| level, | ||
| message, | ||
| context, | ||
| timestamp: Date.now() | ||
| }); | ||
| if (shouldOutput) { | ||
| const consoleMethod = level === "fatal" ? "error" : level; | ||
| const logFn = console[consoleMethod] || console.log; | ||
| if (context) { | ||
| logFn(`[${level.toUpperCase()}]`, context, message); | ||
| } else { | ||
| logFn(`[${level.toUpperCase()}]`, message); | ||
| } | ||
| } | ||
| } | ||
| function createTestLogger(config = {}) { | ||
| const shouldOutput = config.console === true; | ||
| const minLevel = getMinLogLevel(config.level); | ||
| return { | ||
| trace: (...args) => createLogEntry("trace", args, shouldOutput, minLevel), | ||
| debug: (...args) => createLogEntry("debug", args, shouldOutput, minLevel), | ||
| info: (...args) => createLogEntry("info", args, shouldOutput, minLevel), | ||
| warn: (...args) => createLogEntry("warn", args, shouldOutput, minLevel), | ||
| error: (...args) => createLogEntry("error", args, shouldOutput, minLevel), | ||
| fatal: (...args) => createLogEntry("fatal", args, shouldOutput, minLevel) | ||
| }; | ||
| } | ||
| function getTestLogs() { | ||
| return [...capturedLogs]; | ||
| } | ||
| function getTestLogsByLevel(level) { | ||
| return capturedLogs.filter((log) => log.level === level); | ||
| } | ||
| function clearTestLogs() { | ||
| capturedLogs.length = 0; | ||
| } | ||
| function getTestLogsCount() { | ||
| return capturedLogs.length; | ||
| } | ||
| var capturedLogs, LOG_LEVEL_PRIORITY; | ||
| var init_test_adapter = __esm({ | ||
| "src/core/test-adapter.ts"() { | ||
| "use strict"; | ||
| capturedLogs = []; | ||
| LOG_LEVEL_PRIORITY = { | ||
| trace: 0, | ||
| debug: 1, | ||
| info: 2, | ||
| warn: 3, | ||
| error: 4, | ||
| fatal: 5 | ||
| }; | ||
| } | ||
| }); | ||
| // src/core/console-adapter.ts | ||
| var console_adapter_exports = {}; | ||
| __export(console_adapter_exports, { | ||
| createConsoleLogger: () => createConsoleLogger | ||
| }); | ||
| function createConsoleLogger(config = {}) { | ||
| const finalConfig = { ...defaultConfig, ...config }; | ||
| const levelThreshold = LOG_LEVELS[finalConfig.level || "info"]; | ||
| const name = finalConfig.name || "app"; | ||
| const formatMessage = (level, ...args) => { | ||
| const timestamp = (/* @__PURE__ */ new Date()).toISOString(); | ||
| const prefix = `[${timestamp}] [${level.toUpperCase()}] [${name}]`; | ||
| return `${prefix} ${args.join(" ")}`; | ||
| }; | ||
| const shouldLog2 = (level) => { | ||
| return LOG_LEVELS[level] >= levelThreshold; | ||
| }; | ||
| const logMethod = (level, consoleMethod) => { | ||
| return (...args) => { | ||
| if (!shouldLog2(level)) return; | ||
| if (!finalConfig.console) return; | ||
| const message = formatMessage(level, ...args); | ||
| consoleMethod(message); | ||
| }; | ||
| }; | ||
| return { | ||
| trace: logMethod("trace", console.log), | ||
| debug: logMethod("debug", console.log), | ||
| info: logMethod("info", console.info), | ||
| warn: logMethod("warn", console.warn), | ||
| error: logMethod("error", console.error), | ||
| fatal: logMethod("fatal", console.error) | ||
| }; | ||
| } | ||
| var LOG_LEVELS, defaultConfig; | ||
| var init_console_adapter = __esm({ | ||
| "src/core/console-adapter.ts"() { | ||
| "use strict"; | ||
| LOG_LEVELS = { | ||
| trace: 0, | ||
| debug: 1, | ||
| info: 2, | ||
| warn: 3, | ||
| error: 4, | ||
| fatal: 5 | ||
| }; | ||
| defaultConfig = { | ||
| level: "info", | ||
| console: true, | ||
| colors: true, | ||
| name: "app" | ||
| }; | ||
| } | ||
| }); | ||
| // src/test.ts | ||
| var test_exports = {}; | ||
| __export(test_exports, { | ||
| clearTestLogs: () => clearTestLogs, | ||
| createLogger: () => createLogger, | ||
| createTestLogger: () => createTestLogger2, | ||
| getTestLogs: () => getTestLogs, | ||
| getTestLogsByLevel: () => getTestLogsByLevel, | ||
| getTestLogsCount: () => getTestLogsCount | ||
| }); | ||
| module.exports = __toCommonJS(test_exports); | ||
| // src/core/adapter-factory.ts | ||
| function detectEnvironment() { | ||
| if (typeof process !== "undefined" && (process.env.VITEST === "true" || process.env.JEST_WORKER_ID !== void 0 || process.env.NODE_ENV === "test")) { | ||
| return "test"; | ||
| } | ||
| if (typeof globalThis !== "undefined" && "caches" in globalThis && "Request" in globalThis && "Response" in globalThis) { | ||
| return "cloudflare-workers"; | ||
| } | ||
| if (typeof process !== "undefined" && process.versions && process.versions.node) { | ||
| return "nodejs"; | ||
| } | ||
| return "browser"; | ||
| } | ||
| async function createLoggerAdapter(config = {}) { | ||
| const env = config.environment || detectEnvironment(); | ||
| if (env === "test") { | ||
| const { createTestLogger: createTestLogger3 } = await Promise.resolve().then(() => (init_test_adapter(), test_adapter_exports)); | ||
| return createTestLogger3(config); | ||
| } else if (env === "nodejs") { | ||
| const adapterPath = "./pino-adapter.js"; | ||
| const { createPinoLogger } = await import( | ||
| /* @vite-ignore */ | ||
| adapterPath | ||
| ); | ||
| return createPinoLogger(config); | ||
| } else { | ||
| const { createConsoleLogger: createConsoleLogger2 } = await Promise.resolve().then(() => (init_console_adapter(), console_adapter_exports)); | ||
| return createConsoleLogger2(config); | ||
| } | ||
| } | ||
| // src/api/logger.ts | ||
| var DefaultLogger = class { | ||
| adapter; | ||
| initPromise; | ||
| constructor(config = {}) { | ||
| this.initPromise = createLoggerAdapter(config).then((adapter) => { | ||
| this.adapter = adapter; | ||
| }); | ||
| } | ||
| // All methods are typed as `any` for maximum flexibility | ||
| trace = async (...args) => { | ||
| await this.initPromise; | ||
| this.adapter.trace(...args); | ||
| }; | ||
| debug = async (...args) => { | ||
| await this.initPromise; | ||
| this.adapter.debug(...args); | ||
| }; | ||
| info = async (...args) => { | ||
| await this.initPromise; | ||
| this.adapter.info(...args); | ||
| }; | ||
| warn = async (...args) => { | ||
| await this.initPromise; | ||
| this.adapter.warn(...args); | ||
| }; | ||
| error = async (...args) => { | ||
| await this.initPromise; | ||
| this.adapter.error(...args); | ||
| }; | ||
| fatal = async (...args) => { | ||
| await this.initPromise; | ||
| this.adapter.fatal(...args); | ||
| }; | ||
| }; | ||
| function createLogger(config = {}) { | ||
| return new DefaultLogger(config); | ||
| } | ||
| // src/test.ts | ||
| init_test_adapter(); | ||
| function createTestLogger2(config = {}) { | ||
| return createLogger({ | ||
| ...config, | ||
| environment: "test" | ||
| }); | ||
| } | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| clearTestLogs, | ||
| createLogger, | ||
| createTestLogger, | ||
| getTestLogs, | ||
| getTestLogsByLevel, | ||
| getTestLogsCount | ||
| }); | ||
| //# sourceMappingURL=test.cjs.map |
| {"version":3,"sources":["../src/core/test-adapter.ts","../src/core/console-adapter.ts","../src/test.ts","../src/core/adapter-factory.ts","../src/api/logger.ts"],"sourcesContent":["/**\n * Test adapter - In-memory logger for testing environments\n * Captures all logs in memory for inspection and assertion\n */\nimport type { LoggerConfig, LogLevel } from \"~/types/index.js\";\n\nexport interface CapturedLog {\n level: LogLevel;\n message: string;\n context?: Record<string, any>;\n timestamp: number;\n}\n\n/**\n * Global log storage for test environment\n * Shared across all test logger instances in the same process\n */\nconst capturedLogs: CapturedLog[] = [];\n\n/**\n * Log level priority mapping for filtering\n */\nconst LOG_LEVEL_PRIORITY: Record<LogLevel, number> = {\n trace: 0,\n debug: 1,\n info: 2,\n warn: 3,\n error: 4,\n fatal: 5,\n};\n\n/**\n * Get current log level priority\n */\nfunction getMinLogLevel(level: LogLevel = \"info\"): number {\n return LOG_LEVEL_PRIORITY[level];\n}\n\n/**\n * Check if a log level should be recorded based on config\n */\nfunction shouldLog(level: LogLevel, minLevel: number): boolean {\n return LOG_LEVEL_PRIORITY[level] >= minLevel;\n}\n\n/**\n * Create a log entry and optionally output to console\n */\nfunction createLogEntry(\n level: LogLevel,\n args: any[],\n shouldOutput: boolean,\n minLevel: number,\n): void {\n // Check if this level should be logged\n if (!shouldLog(level, minLevel)) {\n return;\n }\n\n let message: string;\n let context: Record<string, any> | undefined;\n\n // Parse arguments - support both (message) and (context, message) patterns\n if (args.length === 1) {\n if (typeof args[0] === \"string\") {\n message = args[0];\n } else {\n context = args[0];\n message = JSON.stringify(args[0]);\n }\n } else if (args.length >= 2) {\n // First arg might be context object\n if (typeof args[0] === \"object\" && args[0] !== null) {\n context = args[0];\n message = args.slice(1).join(\" \");\n } else {\n message = args.join(\" \");\n }\n } else {\n message = \"\";\n }\n\n // Always capture in memory\n capturedLogs.push({\n level,\n message,\n context,\n timestamp: Date.now(),\n });\n\n // Optionally output to console\n if (shouldOutput) {\n const consoleMethod = level === \"fatal\" ? \"error\" : level;\n const logFn = (console as any)[consoleMethod] || console.log;\n\n if (context) {\n logFn(`[${level.toUpperCase()}]`, context, message);\n } else {\n logFn(`[${level.toUpperCase()}]`, message);\n }\n }\n}\n\n/**\n * Create a test logger instance\n */\nexport function createTestLogger(config: LoggerConfig = {}): any {\n // Default: silent in tests (console: false), unless explicitly enabled\n const shouldOutput = config.console === true;\n const minLevel = getMinLogLevel(config.level);\n\n return {\n trace: (...args: any[]) =>\n createLogEntry(\"trace\", args, shouldOutput, minLevel),\n debug: (...args: any[]) =>\n createLogEntry(\"debug\", args, shouldOutput, minLevel),\n info: (...args: any[]) =>\n createLogEntry(\"info\", args, shouldOutput, minLevel),\n warn: (...args: any[]) =>\n createLogEntry(\"warn\", args, shouldOutput, minLevel),\n error: (...args: any[]) =>\n createLogEntry(\"error\", args, shouldOutput, minLevel),\n fatal: (...args: any[]) =>\n createLogEntry(\"fatal\", args, shouldOutput, minLevel),\n };\n}\n\n/**\n * Get all captured logs\n */\nexport function getTestLogs(): CapturedLog[] {\n return [...capturedLogs];\n}\n\n/**\n * Get logs filtered by level\n */\nexport function getTestLogsByLevel(level: LogLevel): CapturedLog[] {\n return capturedLogs.filter((log) => log.level === level);\n}\n\n/**\n * Clear all captured logs\n */\nexport function clearTestLogs(): void {\n capturedLogs.length = 0;\n}\n\n/**\n * Get the count of captured logs\n */\nexport function getTestLogsCount(): number {\n return capturedLogs.length;\n}\n","/**\n * Console logger adapter - for edge runtimes and browsers\n */\nimport type { LoggerConfig } from \"~/types/config.js\";\nimport type { LogLevel } from \"~/types/logger.js\";\n\ninterface ConsoleLoggerInstance {\n trace: (...args: any[]) => void;\n debug: (...args: any[]) => void;\n info: (...args: any[]) => void;\n warn: (...args: any[]) => void;\n error: (...args: any[]) => void;\n fatal: (...args: any[]) => void;\n}\n\nconst LOG_LEVELS: Record<LogLevel, number> = {\n trace: 0,\n debug: 1,\n info: 2,\n warn: 3,\n error: 4,\n fatal: 5,\n};\n\nconst defaultConfig: LoggerConfig = {\n level: \"info\",\n console: true,\n colors: true,\n name: \"app\",\n};\n\n/**\n * Create a console-based logger instance for edge runtimes\n */\nexport function createConsoleLogger(\n config: LoggerConfig = {},\n): ConsoleLoggerInstance {\n const finalConfig = { ...defaultConfig, ...config };\n const levelThreshold = LOG_LEVELS[finalConfig.level || \"info\"];\n const name = finalConfig.name || \"app\";\n\n const formatMessage = (level: LogLevel, ...args: any[]): string => {\n const timestamp = new Date().toISOString();\n const prefix = `[${timestamp}] [${level.toUpperCase()}] [${name}]`;\n return `${prefix} ${args.join(\" \")}`;\n };\n\n const shouldLog = (level: LogLevel): boolean => {\n return LOG_LEVELS[level] >= levelThreshold;\n };\n\n const logMethod = (level: LogLevel, consoleMethod: any) => {\n return (...args: any[]) => {\n if (!shouldLog(level)) return;\n if (!finalConfig.console) return;\n\n const message = formatMessage(level, ...args);\n consoleMethod(message);\n };\n };\n\n return {\n trace: logMethod(\"trace\", console.log),\n debug: logMethod(\"debug\", console.log),\n info: logMethod(\"info\", console.info),\n warn: logMethod(\"warn\", console.warn),\n error: logMethod(\"error\", console.error),\n fatal: logMethod(\"fatal\", console.error),\n };\n}\n","/**\n * Test environment entry point\n * Provides in-memory logger for testing with inspection utilities\n */\nimport { createLogger } from \"~/api/logger.js\";\nimport type { LoggerConfig } from \"~/types/index.js\";\n\n// Export test utilities\nexport {\n getTestLogs,\n getTestLogsByLevel,\n clearTestLogs,\n getTestLogsCount,\n type CapturedLog,\n} from \"~/core/test-adapter.js\";\n\n/**\n * Create a test logger instance\n * Automatically uses test adapter with in-memory capture\n */\nexport function createTestLogger(config: LoggerConfig = {}): any {\n return createLogger({\n ...config,\n environment: \"test\",\n });\n}\n\n// Re-export the standard createLogger for convenience\nexport { createLogger };\n\n// Re-export types\nexport type { LoggerConfig, Logger, LogLevel } from \"~/types/index.js\";\n","/**\n * Logger adapter factory - auto-detect environment and create appropriate logger\n */\nimport type { LoggerConfig, RuntimeEnvironment } from \"~/types/config.js\";\n\n/**\n * Detect the current runtime environment\n */\nexport function detectEnvironment(): RuntimeEnvironment {\n // Priority 0: Check for test environment (vitest, jest, etc.)\n // Test environments should use in-memory test adapter\n if (\n typeof process !== \"undefined\" &&\n (process.env.VITEST === \"true\" ||\n process.env.JEST_WORKER_ID !== undefined ||\n process.env.NODE_ENV === \"test\")\n ) {\n return \"test\";\n }\n\n // Priority 1: Check for Cloudflare Workers specific globals\n // Workers have caches API - this is the most reliable indicator\n // If these globals exist, it's definitely Cloudflare Workers runtime\n if (\n typeof globalThis !== \"undefined\" &&\n \"caches\" in globalThis &&\n \"Request\" in globalThis &&\n \"Response\" in globalThis\n ) {\n // Presence of Workers globals is sufficient - return immediately\n // Don't check fs/process as they're unreliable in wrangler dev with nodejs_compat\n return \"cloudflare-workers\";\n }\n\n // Priority 2: Check for Node.js\n if (\n typeof process !== \"undefined\" &&\n process.versions &&\n process.versions.node\n ) {\n return \"nodejs\";\n }\n\n // Fallback to browser\n return \"browser\";\n}\n\n/**\n * Create appropriate logger adapter based on environment\n *\n * Uses dynamic import() to conditionally load adapters based on runtime.\n * Tree-shaking in production builds ensures unused adapters are removed.\n */\nexport async function createLoggerAdapter(\n config: LoggerConfig = {},\n): Promise<any> {\n const env = config.environment || detectEnvironment();\n\n if (env === \"test\") {\n // Test adapter for vitest/jest - in-memory, silent by default\n const { createTestLogger } = await import(\"./test-adapter.js\");\n return createTestLogger(config);\n } else if (env === \"nodejs\") {\n // Use string concatenation to hide import from bundler static analysis\n // This prevents edge runtime bundlers from including Node.js-only dependencies\n const adapterPath = \"./pino-adapter\" + \".js\";\n const { createPinoLogger } = await import(/* @vite-ignore */ adapterPath);\n return createPinoLogger(config);\n } else {\n // Console adapter is safe for all environments\n const { createConsoleLogger } = await import(\"./console-adapter.js\");\n return createConsoleLogger(config);\n }\n}\n","/**\n * Default logger implementation\n */\nimport type { Logger, LoggerConfig } from \"~/types/index.js\";\nimport { createLoggerAdapter } from \"~/core/adapter-factory.js\";\n\nexport class DefaultLogger implements Logger {\n private adapter: any;\n private initPromise: Promise<void>;\n\n constructor(config: LoggerConfig = {}) {\n // Initialize adapter asynchronously\n this.initPromise = createLoggerAdapter(config).then((adapter) => {\n this.adapter = adapter;\n });\n }\n\n // All methods are typed as `any` for maximum flexibility\n trace: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.trace as any)(...args);\n };\n\n debug: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.debug as any)(...args);\n };\n\n info: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.info as any)(...args);\n };\n\n warn: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.warn as any)(...args);\n };\n\n error: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.error as any)(...args);\n };\n\n fatal: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.fatal as any)(...args);\n };\n}\n\n/**\n * Factory function to create a logger instance\n */\nexport function createLogger(config: LoggerConfig = {}): Logger {\n return new DefaultLogger(config);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkCA,SAAS,eAAe,QAAkB,QAAgB;AACxD,SAAO,mBAAmB,KAAK;AACjC;AAKA,SAAS,UAAU,OAAiB,UAA2B;AAC7D,SAAO,mBAAmB,KAAK,KAAK;AACtC;AAKA,SAAS,eACP,OACA,MACA,cACA,UACM;AAEN,MAAI,CAAC,UAAU,OAAO,QAAQ,GAAG;AAC/B;AAAA,EACF;AAEA,MAAI;AACJ,MAAI;AAGJ,MAAI,KAAK,WAAW,GAAG;AACrB,QAAI,OAAO,KAAK,CAAC,MAAM,UAAU;AAC/B,gBAAU,KAAK,CAAC;AAAA,IAClB,OAAO;AACL,gBAAU,KAAK,CAAC;AAChB,gBAAU,KAAK,UAAU,KAAK,CAAC,CAAC;AAAA,IAClC;AAAA,EACF,WAAW,KAAK,UAAU,GAAG;AAE3B,QAAI,OAAO,KAAK,CAAC,MAAM,YAAY,KAAK,CAAC,MAAM,MAAM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,GAAG;AAErC,SAAK,cAAc,oBAAoB,MAAM,EAAE,KAAK,CAAC,YAAY;AAC/D,WAAK,UAAU;AAAA,IACjB,CAAC;AAAA,EACH;AAAA;AAAA,EAGA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AAAA,EAEA,OAAY,UAAU,SAAgB;AACpC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,KAAa,GAAG,IAAI;AAAA,EACpC;AAAA,EAEA,OAAY,UAAU,SAAgB;AACpC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,KAAa,GAAG,IAAI;AAAA,EACpC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AACF;AAKO,SAAS,aAAa,SAAuB,CAAC,GAAW;AAC9D,SAAO,IAAI,cAAc,MAAM;AACjC;;;AF9CA;AAYO,SAASC,kBAAiB,SAAuB,CAAC,GAAQ;AAC/D,SAAO,aAAa;AAAA,IAClB,GAAG;AAAA,IACH,aAAa;AAAA,EACf,CAAC;AACH;","names":["shouldLog","createTestLogger","createTestLogger","createConsoleLogger","createTestLogger"]} |
| export { createLogger } from './index.cjs'; | ||
| import { b as LogLevel, L as LoggerConfig } from './logger-Lmm6XEQc.cjs'; | ||
| export { a as Logger } from './logger-Lmm6XEQc.cjs'; | ||
| /** | ||
| * Test adapter - In-memory logger for testing environments | ||
| * Captures all logs in memory for inspection and assertion | ||
| */ | ||
| interface CapturedLog { | ||
| level: LogLevel; | ||
| message: string; | ||
| context?: Record<string, any>; | ||
| timestamp: number; | ||
| } | ||
| /** | ||
| * Get all captured logs | ||
| */ | ||
| declare function getTestLogs(): CapturedLog[]; | ||
| /** | ||
| * Get logs filtered by level | ||
| */ | ||
| declare function getTestLogsByLevel(level: LogLevel): CapturedLog[]; | ||
| /** | ||
| * Clear all captured logs | ||
| */ | ||
| declare function clearTestLogs(): void; | ||
| /** | ||
| * Get the count of captured logs | ||
| */ | ||
| declare function getTestLogsCount(): number; | ||
| /** | ||
| * Test environment entry point | ||
| * Provides in-memory logger for testing with inspection utilities | ||
| */ | ||
| /** | ||
| * Create a test logger instance | ||
| * Automatically uses test adapter with in-memory capture | ||
| */ | ||
| declare function createTestLogger(config?: LoggerConfig): any; | ||
| export { type CapturedLog, LogLevel, LoggerConfig, clearTestLogs, createTestLogger, getTestLogs, getTestLogsByLevel, getTestLogsCount }; |
| export { createLogger } from './index.js'; | ||
| import { b as LogLevel, L as LoggerConfig } from './logger-Lmm6XEQc.js'; | ||
| export { a as Logger } from './logger-Lmm6XEQc.js'; | ||
| /** | ||
| * Test adapter - In-memory logger for testing environments | ||
| * Captures all logs in memory for inspection and assertion | ||
| */ | ||
| interface CapturedLog { | ||
| level: LogLevel; | ||
| message: string; | ||
| context?: Record<string, any>; | ||
| timestamp: number; | ||
| } | ||
| /** | ||
| * Get all captured logs | ||
| */ | ||
| declare function getTestLogs(): CapturedLog[]; | ||
| /** | ||
| * Get logs filtered by level | ||
| */ | ||
| declare function getTestLogsByLevel(level: LogLevel): CapturedLog[]; | ||
| /** | ||
| * Clear all captured logs | ||
| */ | ||
| declare function clearTestLogs(): void; | ||
| /** | ||
| * Get the count of captured logs | ||
| */ | ||
| declare function getTestLogsCount(): number; | ||
| /** | ||
| * Test environment entry point | ||
| * Provides in-memory logger for testing with inspection utilities | ||
| */ | ||
| /** | ||
| * Create a test logger instance | ||
| * Automatically uses test adapter with in-memory capture | ||
| */ | ||
| declare function createTestLogger(config?: LoggerConfig): any; | ||
| export { type CapturedLog, LogLevel, LoggerConfig, clearTestLogs, createTestLogger, getTestLogs, getTestLogsByLevel, getTestLogsCount }; |
+246
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __esm = (fn, res) => function __init() { | ||
| return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; | ||
| }; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| // src/core/test-adapter.ts | ||
| var test_adapter_exports = {}; | ||
| __export(test_adapter_exports, { | ||
| clearTestLogs: () => clearTestLogs, | ||
| createTestLogger: () => createTestLogger, | ||
| getTestLogs: () => getTestLogs, | ||
| getTestLogsByLevel: () => getTestLogsByLevel, | ||
| getTestLogsCount: () => getTestLogsCount | ||
| }); | ||
| function getMinLogLevel(level = "info") { | ||
| return LOG_LEVEL_PRIORITY[level]; | ||
| } | ||
| function shouldLog(level, minLevel) { | ||
| return LOG_LEVEL_PRIORITY[level] >= minLevel; | ||
| } | ||
| function createLogEntry(level, args, shouldOutput, minLevel) { | ||
| if (!shouldLog(level, minLevel)) { | ||
| return; | ||
| } | ||
| let message; | ||
| let context; | ||
| if (args.length === 1) { | ||
| if (typeof args[0] === "string") { | ||
| message = args[0]; | ||
| } else { | ||
| context = args[0]; | ||
| message = JSON.stringify(args[0]); | ||
| } | ||
| } else if (args.length >= 2) { | ||
| if (typeof args[0] === "object" && args[0] !== null) { | ||
| context = args[0]; | ||
| message = args.slice(1).join(" "); | ||
| } else { | ||
| message = args.join(" "); | ||
| } | ||
| } else { | ||
| message = ""; | ||
| } | ||
| capturedLogs.push({ | ||
| level, | ||
| message, | ||
| context, | ||
| timestamp: Date.now() | ||
| }); | ||
| if (shouldOutput) { | ||
| const consoleMethod = level === "fatal" ? "error" : level; | ||
| const logFn = console[consoleMethod] || console.log; | ||
| if (context) { | ||
| logFn(`[${level.toUpperCase()}]`, context, message); | ||
| } else { | ||
| logFn(`[${level.toUpperCase()}]`, message); | ||
| } | ||
| } | ||
| } | ||
| function createTestLogger(config = {}) { | ||
| const shouldOutput = config.console === true; | ||
| const minLevel = getMinLogLevel(config.level); | ||
| return { | ||
| trace: (...args) => createLogEntry("trace", args, shouldOutput, minLevel), | ||
| debug: (...args) => createLogEntry("debug", args, shouldOutput, minLevel), | ||
| info: (...args) => createLogEntry("info", args, shouldOutput, minLevel), | ||
| warn: (...args) => createLogEntry("warn", args, shouldOutput, minLevel), | ||
| error: (...args) => createLogEntry("error", args, shouldOutput, minLevel), | ||
| fatal: (...args) => createLogEntry("fatal", args, shouldOutput, minLevel) | ||
| }; | ||
| } | ||
| function getTestLogs() { | ||
| return [...capturedLogs]; | ||
| } | ||
| function getTestLogsByLevel(level) { | ||
| return capturedLogs.filter((log) => log.level === level); | ||
| } | ||
| function clearTestLogs() { | ||
| capturedLogs.length = 0; | ||
| } | ||
| function getTestLogsCount() { | ||
| return capturedLogs.length; | ||
| } | ||
| var capturedLogs, LOG_LEVEL_PRIORITY; | ||
| var init_test_adapter = __esm({ | ||
| "src/core/test-adapter.ts"() { | ||
| "use strict"; | ||
| capturedLogs = []; | ||
| LOG_LEVEL_PRIORITY = { | ||
| trace: 0, | ||
| debug: 1, | ||
| info: 2, | ||
| warn: 3, | ||
| error: 4, | ||
| fatal: 5 | ||
| }; | ||
| } | ||
| }); | ||
| // src/core/console-adapter.ts | ||
| var console_adapter_exports = {}; | ||
| __export(console_adapter_exports, { | ||
| createConsoleLogger: () => createConsoleLogger | ||
| }); | ||
| function createConsoleLogger(config = {}) { | ||
| const finalConfig = { ...defaultConfig, ...config }; | ||
| const levelThreshold = LOG_LEVELS[finalConfig.level || "info"]; | ||
| const name = finalConfig.name || "app"; | ||
| const formatMessage = (level, ...args) => { | ||
| const timestamp = (/* @__PURE__ */ new Date()).toISOString(); | ||
| const prefix = `[${timestamp}] [${level.toUpperCase()}] [${name}]`; | ||
| return `${prefix} ${args.join(" ")}`; | ||
| }; | ||
| const shouldLog2 = (level) => { | ||
| return LOG_LEVELS[level] >= levelThreshold; | ||
| }; | ||
| const logMethod = (level, consoleMethod) => { | ||
| return (...args) => { | ||
| if (!shouldLog2(level)) return; | ||
| if (!finalConfig.console) return; | ||
| const message = formatMessage(level, ...args); | ||
| consoleMethod(message); | ||
| }; | ||
| }; | ||
| return { | ||
| trace: logMethod("trace", console.log), | ||
| debug: logMethod("debug", console.log), | ||
| info: logMethod("info", console.info), | ||
| warn: logMethod("warn", console.warn), | ||
| error: logMethod("error", console.error), | ||
| fatal: logMethod("fatal", console.error) | ||
| }; | ||
| } | ||
| var LOG_LEVELS, defaultConfig; | ||
| var init_console_adapter = __esm({ | ||
| "src/core/console-adapter.ts"() { | ||
| "use strict"; | ||
| LOG_LEVELS = { | ||
| trace: 0, | ||
| debug: 1, | ||
| info: 2, | ||
| warn: 3, | ||
| error: 4, | ||
| fatal: 5 | ||
| }; | ||
| defaultConfig = { | ||
| level: "info", | ||
| console: true, | ||
| colors: true, | ||
| name: "app" | ||
| }; | ||
| } | ||
| }); | ||
| // src/core/adapter-factory.ts | ||
| function detectEnvironment() { | ||
| if (typeof process !== "undefined" && (process.env.VITEST === "true" || process.env.JEST_WORKER_ID !== void 0 || process.env.NODE_ENV === "test")) { | ||
| return "test"; | ||
| } | ||
| if (typeof globalThis !== "undefined" && "caches" in globalThis && "Request" in globalThis && "Response" in globalThis) { | ||
| return "cloudflare-workers"; | ||
| } | ||
| if (typeof process !== "undefined" && process.versions && process.versions.node) { | ||
| return "nodejs"; | ||
| } | ||
| return "browser"; | ||
| } | ||
| async function createLoggerAdapter(config = {}) { | ||
| const env = config.environment || detectEnvironment(); | ||
| if (env === "test") { | ||
| const { createTestLogger: createTestLogger3 } = await Promise.resolve().then(() => (init_test_adapter(), test_adapter_exports)); | ||
| return createTestLogger3(config); | ||
| } else if (env === "nodejs") { | ||
| const adapterPath = "./pino-adapter.js"; | ||
| const { createPinoLogger } = await import( | ||
| /* @vite-ignore */ | ||
| adapterPath | ||
| ); | ||
| return createPinoLogger(config); | ||
| } else { | ||
| const { createConsoleLogger: createConsoleLogger2 } = await Promise.resolve().then(() => (init_console_adapter(), console_adapter_exports)); | ||
| return createConsoleLogger2(config); | ||
| } | ||
| } | ||
| // src/api/logger.ts | ||
| var DefaultLogger = class { | ||
| adapter; | ||
| initPromise; | ||
| constructor(config = {}) { | ||
| this.initPromise = createLoggerAdapter(config).then((adapter) => { | ||
| this.adapter = adapter; | ||
| }); | ||
| } | ||
| // All methods are typed as `any` for maximum flexibility | ||
| trace = async (...args) => { | ||
| await this.initPromise; | ||
| this.adapter.trace(...args); | ||
| }; | ||
| debug = async (...args) => { | ||
| await this.initPromise; | ||
| this.adapter.debug(...args); | ||
| }; | ||
| info = async (...args) => { | ||
| await this.initPromise; | ||
| this.adapter.info(...args); | ||
| }; | ||
| warn = async (...args) => { | ||
| await this.initPromise; | ||
| this.adapter.warn(...args); | ||
| }; | ||
| error = async (...args) => { | ||
| await this.initPromise; | ||
| this.adapter.error(...args); | ||
| }; | ||
| fatal = async (...args) => { | ||
| await this.initPromise; | ||
| this.adapter.fatal(...args); | ||
| }; | ||
| }; | ||
| function createLogger(config = {}) { | ||
| return new DefaultLogger(config); | ||
| } | ||
| // src/test.ts | ||
| init_test_adapter(); | ||
| function createTestLogger2(config = {}) { | ||
| return createLogger({ | ||
| ...config, | ||
| environment: "test" | ||
| }); | ||
| } | ||
| export { | ||
| clearTestLogs, | ||
| createLogger, | ||
| createTestLogger2 as createTestLogger, | ||
| getTestLogs, | ||
| getTestLogsByLevel, | ||
| getTestLogsCount | ||
| }; | ||
| //# sourceMappingURL=test.js.map |
| {"version":3,"sources":["../src/core/test-adapter.ts","../src/core/console-adapter.ts","../src/core/adapter-factory.ts","../src/api/logger.ts","../src/test.ts"],"sourcesContent":["/**\n * Test adapter - In-memory logger for testing environments\n * Captures all logs in memory for inspection and assertion\n */\nimport type { LoggerConfig, LogLevel } from \"~/types/index.js\";\n\nexport interface CapturedLog {\n level: LogLevel;\n message: string;\n context?: Record<string, any>;\n timestamp: number;\n}\n\n/**\n * Global log storage for test environment\n * Shared across all test logger instances in the same process\n */\nconst capturedLogs: CapturedLog[] = [];\n\n/**\n * Log level priority mapping for filtering\n */\nconst LOG_LEVEL_PRIORITY: Record<LogLevel, number> = {\n trace: 0,\n debug: 1,\n info: 2,\n warn: 3,\n error: 4,\n fatal: 5,\n};\n\n/**\n * Get current log level priority\n */\nfunction getMinLogLevel(level: LogLevel = \"info\"): number {\n return LOG_LEVEL_PRIORITY[level];\n}\n\n/**\n * Check if a log level should be recorded based on config\n */\nfunction shouldLog(level: LogLevel, minLevel: number): boolean {\n return LOG_LEVEL_PRIORITY[level] >= minLevel;\n}\n\n/**\n * Create a log entry and optionally output to console\n */\nfunction createLogEntry(\n level: LogLevel,\n args: any[],\n shouldOutput: boolean,\n minLevel: number,\n): void {\n // Check if this level should be logged\n if (!shouldLog(level, minLevel)) {\n return;\n }\n\n let message: string;\n let context: Record<string, any> | undefined;\n\n // Parse arguments - support both (message) and (context, message) patterns\n if (args.length === 1) {\n if (typeof args[0] === \"string\") {\n message = args[0];\n } else {\n context = args[0];\n message = JSON.stringify(args[0]);\n }\n } else if (args.length >= 2) {\n // First arg might be context object\n if (typeof args[0] === \"object\" && args[0] !== null) {\n context = args[0];\n message = args.slice(1).join(\" \");\n } else {\n message = args.join(\" \");\n }\n } else {\n message = \"\";\n }\n\n // Always capture in memory\n capturedLogs.push({\n level,\n message,\n context,\n timestamp: Date.now(),\n });\n\n // Optionally output to console\n if (shouldOutput) {\n const consoleMethod = level === \"fatal\" ? \"error\" : level;\n const logFn = (console as any)[consoleMethod] || console.log;\n\n if (context) {\n logFn(`[${level.toUpperCase()}]`, context, message);\n } else {\n logFn(`[${level.toUpperCase()}]`, message);\n }\n }\n}\n\n/**\n * Create a test logger instance\n */\nexport function createTestLogger(config: LoggerConfig = {}): any {\n // Default: silent in tests (console: false), unless explicitly enabled\n const shouldOutput = config.console === true;\n const minLevel = getMinLogLevel(config.level);\n\n return {\n trace: (...args: any[]) =>\n createLogEntry(\"trace\", args, shouldOutput, minLevel),\n debug: (...args: any[]) =>\n createLogEntry(\"debug\", args, shouldOutput, minLevel),\n info: (...args: any[]) =>\n createLogEntry(\"info\", args, shouldOutput, minLevel),\n warn: (...args: any[]) =>\n createLogEntry(\"warn\", args, shouldOutput, minLevel),\n error: (...args: any[]) =>\n createLogEntry(\"error\", args, shouldOutput, minLevel),\n fatal: (...args: any[]) =>\n createLogEntry(\"fatal\", args, shouldOutput, minLevel),\n };\n}\n\n/**\n * Get all captured logs\n */\nexport function getTestLogs(): CapturedLog[] {\n return [...capturedLogs];\n}\n\n/**\n * Get logs filtered by level\n */\nexport function getTestLogsByLevel(level: LogLevel): CapturedLog[] {\n return capturedLogs.filter((log) => log.level === level);\n}\n\n/**\n * Clear all captured logs\n */\nexport function clearTestLogs(): void {\n capturedLogs.length = 0;\n}\n\n/**\n * Get the count of captured logs\n */\nexport function getTestLogsCount(): number {\n return capturedLogs.length;\n}\n","/**\n * Console logger adapter - for edge runtimes and browsers\n */\nimport type { LoggerConfig } from \"~/types/config.js\";\nimport type { LogLevel } from \"~/types/logger.js\";\n\ninterface ConsoleLoggerInstance {\n trace: (...args: any[]) => void;\n debug: (...args: any[]) => void;\n info: (...args: any[]) => void;\n warn: (...args: any[]) => void;\n error: (...args: any[]) => void;\n fatal: (...args: any[]) => void;\n}\n\nconst LOG_LEVELS: Record<LogLevel, number> = {\n trace: 0,\n debug: 1,\n info: 2,\n warn: 3,\n error: 4,\n fatal: 5,\n};\n\nconst defaultConfig: LoggerConfig = {\n level: \"info\",\n console: true,\n colors: true,\n name: \"app\",\n};\n\n/**\n * Create a console-based logger instance for edge runtimes\n */\nexport function createConsoleLogger(\n config: LoggerConfig = {},\n): ConsoleLoggerInstance {\n const finalConfig = { ...defaultConfig, ...config };\n const levelThreshold = LOG_LEVELS[finalConfig.level || \"info\"];\n const name = finalConfig.name || \"app\";\n\n const formatMessage = (level: LogLevel, ...args: any[]): string => {\n const timestamp = new Date().toISOString();\n const prefix = `[${timestamp}] [${level.toUpperCase()}] [${name}]`;\n return `${prefix} ${args.join(\" \")}`;\n };\n\n const shouldLog = (level: LogLevel): boolean => {\n return LOG_LEVELS[level] >= levelThreshold;\n };\n\n const logMethod = (level: LogLevel, consoleMethod: any) => {\n return (...args: any[]) => {\n if (!shouldLog(level)) return;\n if (!finalConfig.console) return;\n\n const message = formatMessage(level, ...args);\n consoleMethod(message);\n };\n };\n\n return {\n trace: logMethod(\"trace\", console.log),\n debug: logMethod(\"debug\", console.log),\n info: logMethod(\"info\", console.info),\n warn: logMethod(\"warn\", console.warn),\n error: logMethod(\"error\", console.error),\n fatal: logMethod(\"fatal\", console.error),\n };\n}\n","/**\n * Logger adapter factory - auto-detect environment and create appropriate logger\n */\nimport type { LoggerConfig, RuntimeEnvironment } from \"~/types/config.js\";\n\n/**\n * Detect the current runtime environment\n */\nexport function detectEnvironment(): RuntimeEnvironment {\n // Priority 0: Check for test environment (vitest, jest, etc.)\n // Test environments should use in-memory test adapter\n if (\n typeof process !== \"undefined\" &&\n (process.env.VITEST === \"true\" ||\n process.env.JEST_WORKER_ID !== undefined ||\n process.env.NODE_ENV === \"test\")\n ) {\n return \"test\";\n }\n\n // Priority 1: Check for Cloudflare Workers specific globals\n // Workers have caches API - this is the most reliable indicator\n // If these globals exist, it's definitely Cloudflare Workers runtime\n if (\n typeof globalThis !== \"undefined\" &&\n \"caches\" in globalThis &&\n \"Request\" in globalThis &&\n \"Response\" in globalThis\n ) {\n // Presence of Workers globals is sufficient - return immediately\n // Don't check fs/process as they're unreliable in wrangler dev with nodejs_compat\n return \"cloudflare-workers\";\n }\n\n // Priority 2: Check for Node.js\n if (\n typeof process !== \"undefined\" &&\n process.versions &&\n process.versions.node\n ) {\n return \"nodejs\";\n }\n\n // Fallback to browser\n return \"browser\";\n}\n\n/**\n * Create appropriate logger adapter based on environment\n *\n * Uses dynamic import() to conditionally load adapters based on runtime.\n * Tree-shaking in production builds ensures unused adapters are removed.\n */\nexport async function createLoggerAdapter(\n config: LoggerConfig = {},\n): Promise<any> {\n const env = config.environment || detectEnvironment();\n\n if (env === \"test\") {\n // Test adapter for vitest/jest - in-memory, silent by default\n const { createTestLogger } = await import(\"./test-adapter.js\");\n return createTestLogger(config);\n } else if (env === \"nodejs\") {\n // Use string concatenation to hide import from bundler static analysis\n // This prevents edge runtime bundlers from including Node.js-only dependencies\n const adapterPath = \"./pino-adapter\" + \".js\";\n const { createPinoLogger } = await import(/* @vite-ignore */ adapterPath);\n return createPinoLogger(config);\n } else {\n // Console adapter is safe for all environments\n const { createConsoleLogger } = await import(\"./console-adapter.js\");\n return createConsoleLogger(config);\n }\n}\n","/**\n * Default logger implementation\n */\nimport type { Logger, LoggerConfig } from \"~/types/index.js\";\nimport { createLoggerAdapter } from \"~/core/adapter-factory.js\";\n\nexport class DefaultLogger implements Logger {\n private adapter: any;\n private initPromise: Promise<void>;\n\n constructor(config: LoggerConfig = {}) {\n // Initialize adapter asynchronously\n this.initPromise = createLoggerAdapter(config).then((adapter) => {\n this.adapter = adapter;\n });\n }\n\n // All methods are typed as `any` for maximum flexibility\n trace: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.trace as any)(...args);\n };\n\n debug: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.debug as any)(...args);\n };\n\n info: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.info as any)(...args);\n };\n\n warn: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.warn as any)(...args);\n };\n\n error: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.error as any)(...args);\n };\n\n fatal: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.fatal as any)(...args);\n };\n}\n\n/**\n * Factory function to create a logger instance\n */\nexport function createLogger(config: LoggerConfig = {}): Logger {\n return new DefaultLogger(config);\n}\n","/**\n * Test environment entry point\n * Provides in-memory logger for testing with inspection utilities\n */\nimport { createLogger } from \"~/api/logger.js\";\nimport type { LoggerConfig } from \"~/types/index.js\";\n\n// Export test utilities\nexport {\n getTestLogs,\n getTestLogsByLevel,\n clearTestLogs,\n getTestLogsCount,\n type CapturedLog,\n} from \"~/core/test-adapter.js\";\n\n/**\n * Create a test logger instance\n * Automatically uses test adapter with in-memory capture\n */\nexport function createTestLogger(config: LoggerConfig = {}): any {\n return createLogger({\n ...config,\n environment: \"test\",\n });\n}\n\n// Re-export the standard createLogger for convenience\nexport { createLogger };\n\n// Re-export types\nexport type { LoggerConfig, Logger, LogLevel } from \"~/types/index.js\";\n"],"mappings":";;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkCA,SAAS,eAAe,QAAkB,QAAgB;AACxD,SAAO,mBAAmB,KAAK;AACjC;AAKA,SAAS,UAAU,OAAiB,UAA2B;AAC7D,SAAO,mBAAmB,KAAK,KAAK;AACtC;AAKA,SAAS,eACP,OACA,MACA,cACA,UACM;AAEN,MAAI,CAAC,UAAU,OAAO,QAAQ,GAAG;AAC/B;AAAA,EACF;AAEA,MAAI;AACJ,MAAI;AAGJ,MAAI,KAAK,WAAW,GAAG;AACrB,QAAI,OAAO,KAAK,CAAC,MAAM,UAAU;AAC/B,gBAAU,KAAK,CAAC;AAAA,IAClB,OAAO;AACL,gBAAU,KAAK,CAAC;AAChB,gBAAU,KAAK,UAAU,KAAK,CAAC,CAAC;AAAA,IAClC;AAAA,EACF,WAAW,KAAK,UAAU,GAAG;AAE3B,QAAI,OAAO,KAAK,CAAC,MAAM,YAAY,KAAK,CAAC,MAAM,MAAM;AACnD,gBAAU,KAAK,CAAC;AAChB,gBAAU,KAAK,MAAM,CAAC,EAAE,KAAK,GAAG;AAAA,IAClC,OAAO;AACL,gBAAU,KAAK,KAAK,GAAG;AAAA,IACzB;AAAA,EACF,OAAO;AACL,cAAU;AAAA,EACZ;AAGA,eAAa,KAAK;AAAA,IAChB;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW,KAAK,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AA,EAEA,OAAY,UAAU,SAAgB;AACpC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,KAAa,GAAG,IAAI;AAAA,EACpC;AAAA,EAEA,OAAY,UAAU,SAAgB;AACpC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,KAAa,GAAG,IAAI;AAAA,EACpC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AACF;AAKO,SAAS,aAAa,SAAuB,CAAC,GAAW;AAC9D,SAAO,IAAI,cAAc,MAAM;AACjC;;;AC9CA;AAYO,SAASC,kBAAiB,SAAuB,CAAC,GAAQ;AAC/D,SAAO,aAAa;AAAA,IAClB,GAAG;AAAA,IACH,aAAa;AAAA,EACf,CAAC;AACH;","names":["shouldLog","createTestLogger","createConsoleLogger","createTestLogger"]} |
@@ -1,2 +0,2 @@ | ||
| import { L as LoggerConfig, a as Logger } from './logger-CraRs5pE.cjs'; | ||
| import { L as LoggerConfig, a as Logger } from './logger-Lmm6XEQc.cjs'; | ||
@@ -3,0 +3,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
| import { L as LoggerConfig, a as Logger } from './logger-CraRs5pE.js'; | ||
| import { L as LoggerConfig, a as Logger } from './logger-Lmm6XEQc.js'; | ||
@@ -3,0 +3,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
| import { L as LoggerConfig, a as Logger } from './logger-CraRs5pE.cjs'; | ||
| import { L as LoggerConfig, a as Logger } from './logger-Lmm6XEQc.cjs'; | ||
@@ -3,0 +3,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
| import { L as LoggerConfig, a as Logger } from './logger-CraRs5pE.js'; | ||
| import { L as LoggerConfig, a as Logger } from './logger-Lmm6XEQc.js'; | ||
@@ -3,0 +3,0 @@ /** |
+103
-3
@@ -23,2 +23,96 @@ "use strict"; | ||
| // src/core/test-adapter.ts | ||
| var test_adapter_exports = {}; | ||
| __export(test_adapter_exports, { | ||
| clearTestLogs: () => clearTestLogs, | ||
| createTestLogger: () => createTestLogger, | ||
| getTestLogs: () => getTestLogs, | ||
| getTestLogsByLevel: () => getTestLogsByLevel, | ||
| getTestLogsCount: () => getTestLogsCount | ||
| }); | ||
| function getMinLogLevel(level = "info") { | ||
| return LOG_LEVEL_PRIORITY[level]; | ||
| } | ||
| function shouldLog(level, minLevel) { | ||
| return LOG_LEVEL_PRIORITY[level] >= minLevel; | ||
| } | ||
| function createLogEntry(level, args, shouldOutput, minLevel) { | ||
| if (!shouldLog(level, minLevel)) { | ||
| return; | ||
| } | ||
| let message; | ||
| let context; | ||
| if (args.length === 1) { | ||
| if (typeof args[0] === "string") { | ||
| message = args[0]; | ||
| } else { | ||
| context = args[0]; | ||
| message = JSON.stringify(args[0]); | ||
| } | ||
| } else if (args.length >= 2) { | ||
| if (typeof args[0] === "object" && args[0] !== null) { | ||
| context = args[0]; | ||
| message = args.slice(1).join(" "); | ||
| } else { | ||
| message = args.join(" "); | ||
| } | ||
| } else { | ||
| message = ""; | ||
| } | ||
| capturedLogs.push({ | ||
| level, | ||
| message, | ||
| context, | ||
| timestamp: Date.now() | ||
| }); | ||
| if (shouldOutput) { | ||
| const consoleMethod = level === "fatal" ? "error" : level; | ||
| const logFn = console[consoleMethod] || console.log; | ||
| if (context) { | ||
| logFn(`[${level.toUpperCase()}]`, context, message); | ||
| } else { | ||
| logFn(`[${level.toUpperCase()}]`, message); | ||
| } | ||
| } | ||
| } | ||
| function createTestLogger(config = {}) { | ||
| const shouldOutput = config.console === true; | ||
| const minLevel = getMinLogLevel(config.level); | ||
| return { | ||
| trace: (...args) => createLogEntry("trace", args, shouldOutput, minLevel), | ||
| debug: (...args) => createLogEntry("debug", args, shouldOutput, minLevel), | ||
| info: (...args) => createLogEntry("info", args, shouldOutput, minLevel), | ||
| warn: (...args) => createLogEntry("warn", args, shouldOutput, minLevel), | ||
| error: (...args) => createLogEntry("error", args, shouldOutput, minLevel), | ||
| fatal: (...args) => createLogEntry("fatal", args, shouldOutput, minLevel) | ||
| }; | ||
| } | ||
| function getTestLogs() { | ||
| return [...capturedLogs]; | ||
| } | ||
| function getTestLogsByLevel(level) { | ||
| return capturedLogs.filter((log) => log.level === level); | ||
| } | ||
| function clearTestLogs() { | ||
| capturedLogs.length = 0; | ||
| } | ||
| function getTestLogsCount() { | ||
| return capturedLogs.length; | ||
| } | ||
| var capturedLogs, LOG_LEVEL_PRIORITY; | ||
| var init_test_adapter = __esm({ | ||
| "src/core/test-adapter.ts"() { | ||
| "use strict"; | ||
| capturedLogs = []; | ||
| LOG_LEVEL_PRIORITY = { | ||
| trace: 0, | ||
| debug: 1, | ||
| info: 2, | ||
| warn: 3, | ||
| error: 4, | ||
| fatal: 5 | ||
| }; | ||
| } | ||
| }); | ||
| // src/core/console-adapter.ts | ||
@@ -38,3 +132,3 @@ var console_adapter_exports = {}; | ||
| }; | ||
| const shouldLog = (level) => { | ||
| const shouldLog2 = (level) => { | ||
| return LOG_LEVELS[level] >= levelThreshold; | ||
@@ -44,3 +138,3 @@ }; | ||
| return (...args) => { | ||
| if (!shouldLog(level)) return; | ||
| if (!shouldLog2(level)) return; | ||
| if (!finalConfig.console) return; | ||
@@ -91,2 +185,5 @@ const message = formatMessage(level, ...args); | ||
| function detectEnvironment() { | ||
| if (typeof process !== "undefined" && (process.env.VITEST === "true" || process.env.JEST_WORKER_ID !== void 0 || process.env.NODE_ENV === "test")) { | ||
| return "test"; | ||
| } | ||
| if (typeof globalThis !== "undefined" && "caches" in globalThis && "Request" in globalThis && "Response" in globalThis) { | ||
@@ -102,3 +199,6 @@ return "cloudflare-workers"; | ||
| const env = config.environment || detectEnvironment(); | ||
| if (env === "nodejs") { | ||
| if (env === "test") { | ||
| const { createTestLogger: createTestLogger2 } = await Promise.resolve().then(() => (init_test_adapter(), test_adapter_exports)); | ||
| return createTestLogger2(config); | ||
| } else if (env === "nodejs") { | ||
| const adapterPath = "./pino-adapter.js"; | ||
@@ -105,0 +205,0 @@ const { createPinoLogger } = await import( |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../src/core/console-adapter.ts","../src/index.ts","../src/core/adapter-factory.ts","../src/api/logger.ts"],"sourcesContent":["/**\n * Console logger adapter - for edge runtimes and browsers\n */\nimport type { LoggerConfig } from \"~/types/config.js\";\nimport type { LogLevel } from \"~/types/logger.js\";\n\ninterface ConsoleLoggerInstance {\n trace: (...args: any[]) => void;\n debug: (...args: any[]) => void;\n info: (...args: any[]) => void;\n warn: (...args: any[]) => void;\n error: (...args: any[]) => void;\n fatal: (...args: any[]) => void;\n}\n\nconst LOG_LEVELS: Record<LogLevel, number> = {\n trace: 0,\n debug: 1,\n info: 2,\n warn: 3,\n error: 4,\n fatal: 5,\n};\n\nconst defaultConfig: LoggerConfig = {\n level: \"info\",\n console: true,\n colors: true,\n name: \"app\",\n};\n\n/**\n * Create a console-based logger instance for edge runtimes\n */\nexport function createConsoleLogger(\n config: LoggerConfig = {},\n): ConsoleLoggerInstance {\n const finalConfig = { ...defaultConfig, ...config };\n const levelThreshold = LOG_LEVELS[finalConfig.level || \"info\"];\n const name = finalConfig.name || \"app\";\n\n const formatMessage = (level: LogLevel, ...args: any[]): string => {\n const timestamp = new Date().toISOString();\n const prefix = `[${timestamp}] [${level.toUpperCase()}] [${name}]`;\n return `${prefix} ${args.join(\" \")}`;\n };\n\n const shouldLog = (level: LogLevel): boolean => {\n return LOG_LEVELS[level] >= levelThreshold;\n };\n\n const logMethod = (level: LogLevel, consoleMethod: any) => {\n return (...args: any[]) => {\n if (!shouldLog(level)) return;\n if (!finalConfig.console) return;\n\n const message = formatMessage(level, ...args);\n consoleMethod(message);\n };\n };\n\n return {\n trace: logMethod(\"trace\", console.log),\n debug: logMethod(\"debug\", console.log),\n info: logMethod(\"info\", console.info),\n warn: logMethod(\"warn\", console.warn),\n error: logMethod(\"error\", console.error),\n fatal: logMethod(\"fatal\", console.error),\n };\n}\n","/**\n * @deepracticex/logger\n *\n * Unified logging solution with environment-aware adapters\n * - Pino for Node.js (high performance, structured logging)\n * - Console adapter for edge runtimes (Cloudflare Workers, Deno, browser)\n * - Zero side effects: no instances created at module load time\n * - Explicit configuration: users must explicitly create logger instances\n */\n\n// Export core API only\nexport { DefaultLogger, createLogger } from \"~/api/logger.js\";\n\n// Export types\nexport type { Logger, LoggerConfig, LogLevel } from \"~/types/index.js\";\n","/**\n * Logger adapter factory - auto-detect environment and create appropriate logger\n */\nimport type { LoggerConfig, RuntimeEnvironment } from \"~/types/config.js\";\n\n/**\n * Detect the current runtime environment\n */\nexport function detectEnvironment(): RuntimeEnvironment {\n // Priority 1: Check for Cloudflare Workers specific globals\n // Workers have caches API - this is the most reliable indicator\n // If these globals exist, it's definitely Cloudflare Workers runtime\n if (\n typeof globalThis !== \"undefined\" &&\n \"caches\" in globalThis &&\n \"Request\" in globalThis &&\n \"Response\" in globalThis\n ) {\n // Presence of Workers globals is sufficient - return immediately\n // Don't check fs/process as they're unreliable in wrangler dev with nodejs_compat\n return \"cloudflare-workers\";\n }\n\n // Priority 2: Check for Node.js\n if (\n typeof process !== \"undefined\" &&\n process.versions &&\n process.versions.node\n ) {\n return \"nodejs\";\n }\n\n // Fallback to browser\n return \"browser\";\n}\n\n/**\n * Create appropriate logger adapter based on environment\n *\n * Uses dynamic import() to conditionally load adapters based on runtime.\n * Tree-shaking in production builds ensures unused adapters are removed.\n */\nexport async function createLoggerAdapter(\n config: LoggerConfig = {},\n): Promise<any> {\n const env = config.environment || detectEnvironment();\n\n if (env === \"nodejs\") {\n // Use string concatenation to hide import from bundler static analysis\n // This prevents edge runtime bundlers from including Node.js-only dependencies\n const adapterPath = \"./pino-adapter\" + \".js\";\n const { createPinoLogger } = await import(/* @vite-ignore */ adapterPath);\n return createPinoLogger(config);\n } else {\n // Console adapter is safe for all environments\n const { createConsoleLogger } = await import(\"./console-adapter.js\");\n return createConsoleLogger(config);\n }\n}\n","/**\n * Default logger implementation\n */\nimport type { Logger, LoggerConfig } from \"~/types/index.js\";\nimport { createLoggerAdapter } from \"~/core/adapter-factory.js\";\n\nexport class DefaultLogger implements Logger {\n private adapter: any;\n private initPromise: Promise<void>;\n\n constructor(config: LoggerConfig = {}) {\n // Initialize adapter asynchronously\n this.initPromise = createLoggerAdapter(config).then((adapter) => {\n this.adapter = adapter;\n });\n }\n\n // All methods are typed as `any` for maximum flexibility\n trace: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.trace as any)(...args);\n };\n\n debug: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.debug as any)(...args);\n };\n\n info: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.info as any)(...args);\n };\n\n warn: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.warn as any)(...args);\n };\n\n error: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.error as any)(...args);\n };\n\n fatal: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.fatal as any)(...args);\n };\n}\n\n/**\n * Factory function to create a logger instance\n */\nexport function createLogger(config: LoggerConfig = {}): Logger {\n return new DefaultLogger(config);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAkCO,SAAS,oBACd,SAAuB,CAAC,GACD;AACvB,QAAM,cAAc,EAAE,GAAG,eAAe,GAAG,OAAO;AAClD,QAAM,iBAAiB,WAAW,YAAY,SAAS,MAAM;AAC7D,QAAM,OAAO,YAAY,QAAQ;AAEjC,QAAM,gBAAgB,CAAC,UAAoB,SAAwB;AACjE,UAAM,aAAY,oBAAI,KAAK,GAAE,YAAY;AACzC,UAAM,SAAS,IAAI,SAAS,MAAM,MAAM,YAAY,CAAC,MAAM,IAAI;AAC/D,WAAO,GAAG,MAAM,IAAI,KAAK,KAAK,GAAG,CAAC;AAAA,EACpC;AAEA,QAAM,YA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EAAE,iBAAiB,IAAI,MAAM;AAAA;AAAA,MAA0B;AAAA;AAC7D,WAAO,iBAAiB,MAAM;AAAA,EAChC,OAAO;AAEL,UAAM,EAAE,qBAAAA,qBAAoB,IAAI,MAAM;AACtC,WAAOA,qBAAoB,MAAM;AAAA,EACnC;AACF;;;ACpDO,IAAM,gBAAN,MAAsC;AAAA,EACnC;AAAA,EACA;AAAA,EAER,YAAY,SAAuB,CAAC,GAAG;AAErC,SAAK,cAAc,oBAAoB,MAAM,EAAE,KAAK,CAAC,YAAY;AAC/D,WAAK,UAAU;AAAA,IACjB,CAAC;AAAA,EACH;AAAA;AAAA,EAGA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AAAA,EAEA,OAAY,UAAU,SAAgB;AACpC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,KAAa,GAAG,IAAI;AAAA,EACpC;AAAA,EAEA,OAAY,UAAU,SAAgB;AACpC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,KAAa,GAAG,IAAI;AAAA,EACpC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AACF;AAKO,SAAS,aAAa,SAAuB,CAAC,GAAW;AAC9D,SAAO,IAAI,cAAc,MAAM;AACjC;","names":["createConsoleLogger"]} | ||
| {"version":3,"sources":["../src/core/test-adapter.ts","../src/core/console-adapter.ts","../src/index.ts","../src/core/adapter-factory.ts","../src/api/logger.ts"],"sourcesContent":["/**\n * Test adapter - In-memory logger for testing environments\n * Captures all logs in memory for inspection and assertion\n */\nimport type { LoggerConfig, LogLevel } from \"~/types/index.js\";\n\nexport interface CapturedLog {\n level: LogLevel;\n message: string;\n context?: Record<string, any>;\n timestamp: number;\n}\n\n/**\n * Global log storage for test environment\n * Shared across all test logger instances in the same process\n */\nconst capturedLogs: CapturedLog[] = [];\n\n/**\n * Log level priority mapping for filtering\n */\nconst LOG_LEVEL_PRIORITY: Record<LogLevel, number> = {\n trace: 0,\n debug: 1,\n info: 2,\n warn: 3,\n error: 4,\n fatal: 5,\n};\n\n/**\n * Get current log level priority\n */\nfunction getMinLogLevel(level: LogLevel = \"info\"): number {\n return LOG_LEVEL_PRIORITY[level];\n}\n\n/**\n * Check if a log level should be recorded based on config\n */\nfunction shouldLog(level: LogLevel, minLevel: number): boolean {\n return LOG_LEVEL_PRIORITY[level] >= minLevel;\n}\n\n/**\n * Create a log entry and optionally output to console\n */\nfunction createLogEntry(\n level: LogLevel,\n args: any[],\n shouldOutput: boolean,\n minLevel: number,\n): void {\n // Check if this level should be logged\n if (!shouldLog(level, minLevel)) {\n return;\n }\n\n let message: string;\n let context: Record<string, any> | undefined;\n\n // Parse arguments - support both (message) and (context, message) patterns\n if (args.length === 1) {\n if (typeof args[0] === \"string\") {\n message = args[0];\n } else {\n context = args[0];\n message = JSON.stringify(args[0]);\n }\n } else if (args.length >= 2) {\n // First arg might be context object\n if (typeof args[0] === \"object\" && args[0] !== null) {\n context = args[0];\n message = args.slice(1).join(\" \");\n } else {\n message = args.join(\" \");\n }\n } else {\n message = \"\";\n }\n\n // Always capture in memory\n capturedLogs.push({\n level,\n message,\n context,\n timestamp: Date.now(),\n });\n\n // Optionally output to console\n if (shouldOutput) {\n const consoleMethod = level === \"fatal\" ? \"error\" : level;\n const logFn = (console as any)[consoleMethod] || console.log;\n\n if (context) {\n logFn(`[${level.toUpperCase()}]`, context, message);\n } else {\n logFn(`[${level.toUpperCase()}]`, message);\n }\n }\n}\n\n/**\n * Create a test logger instance\n */\nexport function createTestLogger(config: LoggerConfig = {}): any {\n // Default: silent in tests (console: false), unless explicitly enabled\n const shouldOutput = config.console === true;\n const minLevel = getMinLogLevel(config.level);\n\n return {\n trace: (...args: any[]) =>\n createLogEntry(\"trace\", args, shouldOutput, minLevel),\n debug: (...args: any[]) =>\n createLogEntry(\"debug\", args, shouldOutput, minLevel),\n info: (...args: any[]) =>\n createLogEntry(\"info\", args, shouldOutput, minLevel),\n warn: (...args: any[]) =>\n createLogEntry(\"warn\", args, shouldOutput, minLevel),\n error: (...args: any[]) =>\n createLogEntry(\"error\", args, shouldOutput, minLevel),\n fatal: (...args: any[]) =>\n createLogEntry(\"fatal\", args, shouldOutput, minLevel),\n };\n}\n\n/**\n * Get all captured logs\n */\nexport function getTestLogs(): CapturedLog[] {\n return [...capturedLogs];\n}\n\n/**\n * Get logs filtered by level\n */\nexport function getTestLogsByLevel(level: LogLevel): CapturedLog[] {\n return capturedLogs.filter((log) => log.level === level);\n}\n\n/**\n * Clear all captured logs\n */\nexport function clearTestLogs(): void {\n capturedLogs.length = 0;\n}\n\n/**\n * Get the count of captured logs\n */\nexport function getTestLogsCount(): number {\n return capturedLogs.length;\n}\n","/**\n * Console logger adapter - for edge runtimes and browsers\n */\nimport type { LoggerConfig } from \"~/types/config.js\";\nimport type { LogLevel } from \"~/types/logger.js\";\n\ninterface ConsoleLoggerInstance {\n trace: (...args: any[]) => void;\n debug: (...args: any[]) => void;\n info: (...args: any[]) => void;\n warn: (...args: any[]) => void;\n error: (...args: any[]) => void;\n fatal: (...args: any[]) => void;\n}\n\nconst LOG_LEVELS: Record<LogLevel, number> = {\n trace: 0,\n debug: 1,\n info: 2,\n warn: 3,\n error: 4,\n fatal: 5,\n};\n\nconst defaultConfig: LoggerConfig = {\n level: \"info\",\n console: true,\n colors: true,\n name: \"app\",\n};\n\n/**\n * Create a console-based logger instance for edge runtimes\n */\nexport function createConsoleLogger(\n config: LoggerConfig = {},\n): ConsoleLoggerInstance {\n const finalConfig = { ...defaultConfig, ...config };\n const levelThreshold = LOG_LEVELS[finalConfig.level || \"info\"];\n const name = finalConfig.name || \"app\";\n\n const formatMessage = (level: LogLevel, ...args: any[]): string => {\n const timestamp = new Date().toISOString();\n const prefix = `[${timestamp}] [${level.toUpperCase()}] [${name}]`;\n return `${prefix} ${args.join(\" \")}`;\n };\n\n const shouldLog = (level: LogLevel): boolean => {\n return LOG_LEVELS[level] >= levelThreshold;\n };\n\n const logMethod = (level: LogLevel, consoleMethod: any) => {\n return (...args: any[]) => {\n if (!shouldLog(level)) return;\n if (!finalConfig.console) return;\n\n const message = formatMessage(level, ...args);\n consoleMethod(message);\n };\n };\n\n return {\n trace: logMethod(\"trace\", console.log),\n debug: logMethod(\"debug\", console.log),\n info: logMethod(\"info\", console.info),\n warn: logMethod(\"warn\", console.warn),\n error: logMethod(\"error\", console.error),\n fatal: logMethod(\"fatal\", console.error),\n };\n}\n","/**\n * @deepracticex/logger\n *\n * Unified logging solution with environment-aware adapters\n * - Pino for Node.js (high performance, structured logging)\n * - Console adapter for edge runtimes (Cloudflare Workers, Deno, browser)\n * - Zero side effects: no instances created at module load time\n * - Explicit configuration: users must explicitly create logger instances\n */\n\n// Export core API only\nexport { DefaultLogger, createLogger } from \"~/api/logger.js\";\n\n// Export types\nexport type { Logger, LoggerConfig, LogLevel } from \"~/types/index.js\";\n","/**\n * Logger adapter factory - auto-detect environment and create appropriate logger\n */\nimport type { LoggerConfig, RuntimeEnvironment } from \"~/types/config.js\";\n\n/**\n * Detect the current runtime environment\n */\nexport function detectEnvironment(): RuntimeEnvironment {\n // Priority 0: Check for test environment (vitest, jest, etc.)\n // Test environments should use in-memory test adapter\n if (\n typeof process !== \"undefined\" &&\n (process.env.VITEST === \"true\" ||\n process.env.JEST_WORKER_ID !== undefined ||\n process.env.NODE_ENV === \"test\")\n ) {\n return \"test\";\n }\n\n // Priority 1: Check for Cloudflare Workers specific globals\n // Workers have caches API - this is the most reliable indicator\n // If these globals exist, it's definitely Cloudflare Workers runtime\n if (\n typeof globalThis !== \"undefined\" &&\n \"caches\" in globalThis &&\n \"Request\" in globalThis &&\n \"Response\" in globalThis\n ) {\n // Presence of Workers globals is sufficient - return immediately\n // Don't check fs/process as they're unreliable in wrangler dev with nodejs_compat\n return \"cloudflare-workers\";\n }\n\n // Priority 2: Check for Node.js\n if (\n typeof process !== \"undefined\" &&\n process.versions &&\n process.versions.node\n ) {\n return \"nodejs\";\n }\n\n // Fallback to browser\n return \"browser\";\n}\n\n/**\n * Create appropriate logger adapter based on environment\n *\n * Uses dynamic import() to conditionally load adapters based on runtime.\n * Tree-shaking in production builds ensures unused adapters are removed.\n */\nexport async function createLoggerAdapter(\n config: LoggerConfig = {},\n): Promise<any> {\n const env = config.environment || detectEnvironment();\n\n if (env === \"test\") {\n // Test adapter for vitest/jest - in-memory, silent by default\n const { createTestLogger } = await import(\"./test-adapter.js\");\n return createTestLogger(config);\n } else if (env === \"nodejs\") {\n // Use string concatenation to hide import from bundler static analysis\n // This prevents edge runtime bundlers from including Node.js-only dependencies\n const adapterPath = \"./pino-adapter\" + \".js\";\n const { createPinoLogger } = await import(/* @vite-ignore */ adapterPath);\n return createPinoLogger(config);\n } else {\n // Console adapter is safe for all environments\n const { createConsoleLogger } = await import(\"./console-adapter.js\");\n return createConsoleLogger(config);\n }\n}\n","/**\n * Default logger implementation\n */\nimport type { Logger, LoggerConfig } from \"~/types/index.js\";\nimport { createLoggerAdapter } from \"~/core/adapter-factory.js\";\n\nexport class DefaultLogger implements Logger {\n private adapter: any;\n private initPromise: Promise<void>;\n\n constructor(config: LoggerConfig = {}) {\n // Initialize adapter asynchronously\n this.initPromise = createLoggerAdapter(config).then((adapter) => {\n this.adapter = adapter;\n });\n }\n\n // All methods are typed as `any` for maximum flexibility\n trace: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.trace as any)(...args);\n };\n\n debug: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.debug as any)(...args);\n };\n\n info: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.info as any)(...args);\n };\n\n warn: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.warn as any)(...args);\n };\n\n error: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.error as any)(...args);\n };\n\n fatal: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.fatal as any)(...args);\n };\n}\n\n/**\n * Factory function to create a logger instance\n */\nexport function createLogger(config: LoggerConfig = {}): Logger {\n return new DefaultLogger(config);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkCA,SAAS,eAAe,QAAkB,QAAgB;AACxD,SAAO,mBAAmB,KAAK;AACjC;AAKA,SAAS,UAAU,OAAiB,UAA2B;AAC7D,SAAO,mBAAmB,KAAK,KAAK;AACtC;AAKA,SAAS,eACP,OACA,MACA,cACA,UACM;AAEN,MAAI,CAAC,UAAU,OAAO,QAAQ,GAAG;AAC/B;AAAA,EACF;AAEA,MAAI;AACJ,MAAI;AAGJ,MAAI,KAAK,WAAW,GAAG;AACrB,QAAI,OAAO,KAAK,CAAC,MAAM,UAAU;AAC/B,gBAAU,KAAK,CAAC;AAAA,IAClB,OAAO;AACL,gBAAU,KAAK,CAAC;AAChB,gBAAU,KAAK,UAAU,KAAK,CAAC,CAAC;AAAA,IAClC;AAAA,EACF,WAAW,KAAK,UAAU,GAAG;AAE3B,QAAI,OAAO,KAAK,CAAC,MAAM,YAAY,KAAK,CAAC,MAAM,MAAM;AACnD,gBAAU,KAAK,CAAC;AAChB,gBAAU,KAAK,MAAM,CAAC,EAAE,KAAK,GAAG;AAAA,IAClC,OAAO;AACL,gBAAU,KAAK,KAAK,GAAG;AAAA,IACzB;AAAA,EACF,OAAO;AACL,cAAU;AAAA,EACZ;AAGA,eAAa,KAAK;AAAA,IAChB;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW,KAAK,IAAI;AAAA,EACtB,CAAC;AAGD,MAA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C;AAAA,EAEA,OAAY,UAAU,SAAgB;AACpC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,KAAa,GAAG,IAAI;AAAA,EACpC;AAAA,EAEA,OAAY,UAAU,SAAgB;AACpC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,KAAa,GAAG,IAAI;AAAA,EACpC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AACF;AAKO,SAAS,aAAa,SAAuB,CAAC,GAAW;AAC9D,SAAO,IAAI,cAAc,MAAM;AACjC;","names":["shouldLog","createTestLogger","createConsoleLogger"]} |
+2
-2
@@ -1,3 +0,3 @@ | ||
| import { a as Logger, L as LoggerConfig } from './logger-CraRs5pE.cjs'; | ||
| export { b as LogLevel } from './logger-CraRs5pE.cjs'; | ||
| import { a as Logger, L as LoggerConfig } from './logger-Lmm6XEQc.cjs'; | ||
| export { b as LogLevel } from './logger-Lmm6XEQc.cjs'; | ||
@@ -4,0 +4,0 @@ /** |
+2
-2
@@ -1,3 +0,3 @@ | ||
| import { a as Logger, L as LoggerConfig } from './logger-CraRs5pE.js'; | ||
| export { b as LogLevel } from './logger-CraRs5pE.js'; | ||
| import { a as Logger, L as LoggerConfig } from './logger-Lmm6XEQc.js'; | ||
| export { b as LogLevel } from './logger-Lmm6XEQc.js'; | ||
@@ -4,0 +4,0 @@ /** |
+103
-3
@@ -11,2 +11,96 @@ var __defProp = Object.defineProperty; | ||
| // src/core/test-adapter.ts | ||
| var test_adapter_exports = {}; | ||
| __export(test_adapter_exports, { | ||
| clearTestLogs: () => clearTestLogs, | ||
| createTestLogger: () => createTestLogger, | ||
| getTestLogs: () => getTestLogs, | ||
| getTestLogsByLevel: () => getTestLogsByLevel, | ||
| getTestLogsCount: () => getTestLogsCount | ||
| }); | ||
| function getMinLogLevel(level = "info") { | ||
| return LOG_LEVEL_PRIORITY[level]; | ||
| } | ||
| function shouldLog(level, minLevel) { | ||
| return LOG_LEVEL_PRIORITY[level] >= minLevel; | ||
| } | ||
| function createLogEntry(level, args, shouldOutput, minLevel) { | ||
| if (!shouldLog(level, minLevel)) { | ||
| return; | ||
| } | ||
| let message; | ||
| let context; | ||
| if (args.length === 1) { | ||
| if (typeof args[0] === "string") { | ||
| message = args[0]; | ||
| } else { | ||
| context = args[0]; | ||
| message = JSON.stringify(args[0]); | ||
| } | ||
| } else if (args.length >= 2) { | ||
| if (typeof args[0] === "object" && args[0] !== null) { | ||
| context = args[0]; | ||
| message = args.slice(1).join(" "); | ||
| } else { | ||
| message = args.join(" "); | ||
| } | ||
| } else { | ||
| message = ""; | ||
| } | ||
| capturedLogs.push({ | ||
| level, | ||
| message, | ||
| context, | ||
| timestamp: Date.now() | ||
| }); | ||
| if (shouldOutput) { | ||
| const consoleMethod = level === "fatal" ? "error" : level; | ||
| const logFn = console[consoleMethod] || console.log; | ||
| if (context) { | ||
| logFn(`[${level.toUpperCase()}]`, context, message); | ||
| } else { | ||
| logFn(`[${level.toUpperCase()}]`, message); | ||
| } | ||
| } | ||
| } | ||
| function createTestLogger(config = {}) { | ||
| const shouldOutput = config.console === true; | ||
| const minLevel = getMinLogLevel(config.level); | ||
| return { | ||
| trace: (...args) => createLogEntry("trace", args, shouldOutput, minLevel), | ||
| debug: (...args) => createLogEntry("debug", args, shouldOutput, minLevel), | ||
| info: (...args) => createLogEntry("info", args, shouldOutput, minLevel), | ||
| warn: (...args) => createLogEntry("warn", args, shouldOutput, minLevel), | ||
| error: (...args) => createLogEntry("error", args, shouldOutput, minLevel), | ||
| fatal: (...args) => createLogEntry("fatal", args, shouldOutput, minLevel) | ||
| }; | ||
| } | ||
| function getTestLogs() { | ||
| return [...capturedLogs]; | ||
| } | ||
| function getTestLogsByLevel(level) { | ||
| return capturedLogs.filter((log) => log.level === level); | ||
| } | ||
| function clearTestLogs() { | ||
| capturedLogs.length = 0; | ||
| } | ||
| function getTestLogsCount() { | ||
| return capturedLogs.length; | ||
| } | ||
| var capturedLogs, LOG_LEVEL_PRIORITY; | ||
| var init_test_adapter = __esm({ | ||
| "src/core/test-adapter.ts"() { | ||
| "use strict"; | ||
| capturedLogs = []; | ||
| LOG_LEVEL_PRIORITY = { | ||
| trace: 0, | ||
| debug: 1, | ||
| info: 2, | ||
| warn: 3, | ||
| error: 4, | ||
| fatal: 5 | ||
| }; | ||
| } | ||
| }); | ||
| // src/core/console-adapter.ts | ||
@@ -26,3 +120,3 @@ var console_adapter_exports = {}; | ||
| }; | ||
| const shouldLog = (level) => { | ||
| const shouldLog2 = (level) => { | ||
| return LOG_LEVELS[level] >= levelThreshold; | ||
@@ -32,3 +126,3 @@ }; | ||
| return (...args) => { | ||
| if (!shouldLog(level)) return; | ||
| if (!shouldLog2(level)) return; | ||
| if (!finalConfig.console) return; | ||
@@ -71,2 +165,5 @@ const message = formatMessage(level, ...args); | ||
| function detectEnvironment() { | ||
| if (typeof process !== "undefined" && (process.env.VITEST === "true" || process.env.JEST_WORKER_ID !== void 0 || process.env.NODE_ENV === "test")) { | ||
| return "test"; | ||
| } | ||
| if (typeof globalThis !== "undefined" && "caches" in globalThis && "Request" in globalThis && "Response" in globalThis) { | ||
@@ -82,3 +179,6 @@ return "cloudflare-workers"; | ||
| const env = config.environment || detectEnvironment(); | ||
| if (env === "nodejs") { | ||
| if (env === "test") { | ||
| const { createTestLogger: createTestLogger2 } = await Promise.resolve().then(() => (init_test_adapter(), test_adapter_exports)); | ||
| return createTestLogger2(config); | ||
| } else if (env === "nodejs") { | ||
| const adapterPath = "./pino-adapter.js"; | ||
@@ -85,0 +185,0 @@ const { createPinoLogger } = await import( |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../src/core/console-adapter.ts","../src/core/adapter-factory.ts","../src/api/logger.ts"],"sourcesContent":["/**\n * Console logger adapter - for edge runtimes and browsers\n */\nimport type { LoggerConfig } from \"~/types/config.js\";\nimport type { LogLevel } from \"~/types/logger.js\";\n\ninterface ConsoleLoggerInstance {\n trace: (...args: any[]) => void;\n debug: (...args: any[]) => void;\n info: (...args: any[]) => void;\n warn: (...args: any[]) => void;\n error: (...args: any[]) => void;\n fatal: (...args: any[]) => void;\n}\n\nconst LOG_LEVELS: Record<LogLevel, number> = {\n trace: 0,\n debug: 1,\n info: 2,\n warn: 3,\n error: 4,\n fatal: 5,\n};\n\nconst defaultConfig: LoggerConfig = {\n level: \"info\",\n console: true,\n colors: true,\n name: \"app\",\n};\n\n/**\n * Create a console-based logger instance for edge runtimes\n */\nexport function createConsoleLogger(\n config: LoggerConfig = {},\n): ConsoleLoggerInstance {\n const finalConfig = { ...defaultConfig, ...config };\n const levelThreshold = LOG_LEVELS[finalConfig.level || \"info\"];\n const name = finalConfig.name || \"app\";\n\n const formatMessage = (level: LogLevel, ...args: any[]): string => {\n const timestamp = new Date().toISOString();\n const prefix = `[${timestamp}] [${level.toUpperCase()}] [${name}]`;\n return `${prefix} ${args.join(\" \")}`;\n };\n\n const shouldLog = (level: LogLevel): boolean => {\n return LOG_LEVELS[level] >= levelThreshold;\n };\n\n const logMethod = (level: LogLevel, consoleMethod: any) => {\n return (...args: any[]) => {\n if (!shouldLog(level)) return;\n if (!finalConfig.console) return;\n\n const message = formatMessage(level, ...args);\n consoleMethod(message);\n };\n };\n\n return {\n trace: logMethod(\"trace\", console.log),\n debug: logMethod(\"debug\", console.log),\n info: logMethod(\"info\", console.info),\n warn: logMethod(\"warn\", console.warn),\n error: logMethod(\"error\", console.error),\n fatal: logMethod(\"fatal\", console.error),\n };\n}\n","/**\n * Logger adapter factory - auto-detect environment and create appropriate logger\n */\nimport type { LoggerConfig, RuntimeEnvironment } from \"~/types/config.js\";\n\n/**\n * Detect the current runtime environment\n */\nexport function detectEnvironment(): RuntimeEnvironment {\n // Priority 1: Check for Cloudflare Workers specific globals\n // Workers have caches API - this is the most reliable indicator\n // If these globals exist, it's definitely Cloudflare Workers runtime\n if (\n typeof globalThis !== \"undefined\" &&\n \"caches\" in globalThis &&\n \"Request\" in globalThis &&\n \"Response\" in globalThis\n ) {\n // Presence of Workers globals is sufficient - return immediately\n // Don't check fs/process as they're unreliable in wrangler dev with nodejs_compat\n return \"cloudflare-workers\";\n }\n\n // Priority 2: Check for Node.js\n if (\n typeof process !== \"undefined\" &&\n process.versions &&\n process.versions.node\n ) {\n return \"nodejs\";\n }\n\n // Fallback to browser\n return \"browser\";\n}\n\n/**\n * Create appropriate logger adapter based on environment\n *\n * Uses dynamic import() to conditionally load adapters based on runtime.\n * Tree-shaking in production builds ensures unused adapters are removed.\n */\nexport async function createLoggerAdapter(\n config: LoggerConfig = {},\n): Promise<any> {\n const env = config.environment || detectEnvironment();\n\n if (env === \"nodejs\") {\n // Use string concatenation to hide import from bundler static analysis\n // This prevents edge runtime bundlers from including Node.js-only dependencies\n const adapterPath = \"./pino-adapter\" + \".js\";\n const { createPinoLogger } = await import(/* @vite-ignore */ adapterPath);\n return createPinoLogger(config);\n } else {\n // Console adapter is safe for all environments\n const { createConsoleLogger } = await import(\"./console-adapter.js\");\n return createConsoleLogger(config);\n }\n}\n","/**\n * Default logger implementation\n */\nimport type { Logger, LoggerConfig } from \"~/types/index.js\";\nimport { createLoggerAdapter } from \"~/core/adapter-factory.js\";\n\nexport class DefaultLogger implements Logger {\n private adapter: any;\n private initPromise: Promise<void>;\n\n constructor(config: LoggerConfig = {}) {\n // Initialize adapter asynchronously\n this.initPromise = createLoggerAdapter(config).then((adapter) => {\n this.adapter = adapter;\n });\n }\n\n // All methods are typed as `any` for maximum flexibility\n trace: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.trace as any)(...args);\n };\n\n debug: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.debug as any)(...args);\n };\n\n info: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.info as any)(...args);\n };\n\n warn: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.warn as any)(...args);\n };\n\n error: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.error as any)(...args);\n };\n\n fatal: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.fatal as any)(...args);\n };\n}\n\n/**\n * Factory function to create a logger instance\n */\nexport function createLogger(config: LoggerConfig = {}): Logger {\n return new DefaultLogger(config);\n}\n"],"mappings":";;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAkCO,SAAS,oBACd,SAAuB,CAAC,GACD;AACvB,QAAM,cAAc,EAAE,GAAG,eAAe,GAAG,OAAO;AAClD,QAAM,iBAAiB,WAAW,YAAY,SAAS,MAAM;AAC7D,QAAM,OAAO,YAAY,QAAQ;AAEjC,QAAM,gBAAgB,CAAC,UAAoB,SAAwB;AACjE,UAAM,aAAY,oBAAI,KAAK,GAAE,YAAY;AACzC,UAAM,SAAS,IAAI,SAAS,MAAM,MAAM,YAAY,CAAC,MAAM,IAAI;AAC/D,WAAO,GAAG,MAAM,IAAI,KAAK,KAAK,GAAG,CAAC;AAAA,EACpC;AAEA,QAAM,YAAY,CAAC,UAA6B;AAC9C,WAAO,WAAW,KAAK,KAAK;AAAA,EAC9B;AAEA,QAAM,YAAY,CAAC,OAAiB,kBAAuB;AACzD,WAAO,IAAI,SAAgB;AACzB,UAAI,CAAC,UAAU,KAAK,EAAG;AACvB,UAAI,CAAC,YAAY,QAAS;AAE1B,YAAM,UAAU,cAAc,OAAO,GAAG,IAAI;AAC5C,oBAAc,OAAO;AAAA,IACvB;AAAA,EACF;AAEA,SAAO;AAAA,IACL,OAAO,UAAU,SAAS,QAAQ,GAAG;AAAA,IACrC,OAAO,UAAU,SAAS,QAAQ,GAAG;AAAA,IACrC,MAAM,UAAU,QAAQ,QAAQ,IAAI;AAAA,IACpC,MAAM,UAAU,QAAQ,QAAQ,IAAI;AAAA,IACpC,OAAO,UAAU,SAAS,QAAQ,KAAK;AAAA,IACvC,OAAO,UAAU,S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AAG,IAAI;AAAA,EACrC;AAAA,EAEA,OAAY,UAAU,SAAgB;AACpC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,KAAa,GAAG,IAAI;AAAA,EACpC;AAAA,EAEA,OAAY,UAAU,SAAgB;AACpC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,KAAa,GAAG,IAAI;AAAA,EACpC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AACF;AAKO,SAAS,aAAa,SAAuB,CAAC,GAAW;AAC9D,SAAO,IAAI,cAAc,MAAM;AACjC;","names":["createConsoleLogger"]} | ||
| {"version":3,"sources":["../src/core/test-adapter.ts","../src/core/console-adapter.ts","../src/core/adapter-factory.ts","../src/api/logger.ts"],"sourcesContent":["/**\n * Test adapter - In-memory logger for testing environments\n * Captures all logs in memory for inspection and assertion\n */\nimport type { LoggerConfig, LogLevel } from \"~/types/index.js\";\n\nexport interface CapturedLog {\n level: LogLevel;\n message: string;\n context?: Record<string, any>;\n timestamp: number;\n}\n\n/**\n * Global log storage for test environment\n * Shared across all test logger instances in the same process\n */\nconst capturedLogs: CapturedLog[] = [];\n\n/**\n * Log level priority mapping for filtering\n */\nconst LOG_LEVEL_PRIORITY: Record<LogLevel, number> = {\n trace: 0,\n debug: 1,\n info: 2,\n warn: 3,\n error: 4,\n fatal: 5,\n};\n\n/**\n * Get current log level priority\n */\nfunction getMinLogLevel(level: LogLevel = \"info\"): number {\n return LOG_LEVEL_PRIORITY[level];\n}\n\n/**\n * Check if a log level should be recorded based on config\n */\nfunction shouldLog(level: LogLevel, minLevel: number): boolean {\n return LOG_LEVEL_PRIORITY[level] >= minLevel;\n}\n\n/**\n * Create a log entry and optionally output to console\n */\nfunction createLogEntry(\n level: LogLevel,\n args: any[],\n shouldOutput: boolean,\n minLevel: number,\n): void {\n // Check if this level should be logged\n if (!shouldLog(level, minLevel)) {\n return;\n }\n\n let message: string;\n let context: Record<string, any> | undefined;\n\n // Parse arguments - support both (message) and (context, message) patterns\n if (args.length === 1) {\n if (typeof args[0] === \"string\") {\n message = args[0];\n } else {\n context = args[0];\n message = JSON.stringify(args[0]);\n }\n } else if (args.length >= 2) {\n // First arg might be context object\n if (typeof args[0] === \"object\" && args[0] !== null) {\n context = args[0];\n message = args.slice(1).join(\" \");\n } else {\n message = args.join(\" \");\n }\n } else {\n message = \"\";\n }\n\n // Always capture in memory\n capturedLogs.push({\n level,\n message,\n context,\n timestamp: Date.now(),\n });\n\n // Optionally output to console\n if (shouldOutput) {\n const consoleMethod = level === \"fatal\" ? \"error\" : level;\n const logFn = (console as any)[consoleMethod] || console.log;\n\n if (context) {\n logFn(`[${level.toUpperCase()}]`, context, message);\n } else {\n logFn(`[${level.toUpperCase()}]`, message);\n }\n }\n}\n\n/**\n * Create a test logger instance\n */\nexport function createTestLogger(config: LoggerConfig = {}): any {\n // Default: silent in tests (console: false), unless explicitly enabled\n const shouldOutput = config.console === true;\n const minLevel = getMinLogLevel(config.level);\n\n return {\n trace: (...args: any[]) =>\n createLogEntry(\"trace\", args, shouldOutput, minLevel),\n debug: (...args: any[]) =>\n createLogEntry(\"debug\", args, shouldOutput, minLevel),\n info: (...args: any[]) =>\n createLogEntry(\"info\", args, shouldOutput, minLevel),\n warn: (...args: any[]) =>\n createLogEntry(\"warn\", args, shouldOutput, minLevel),\n error: (...args: any[]) =>\n createLogEntry(\"error\", args, shouldOutput, minLevel),\n fatal: (...args: any[]) =>\n createLogEntry(\"fatal\", args, shouldOutput, minLevel),\n };\n}\n\n/**\n * Get all captured logs\n */\nexport function getTestLogs(): CapturedLog[] {\n return [...capturedLogs];\n}\n\n/**\n * Get logs filtered by level\n */\nexport function getTestLogsByLevel(level: LogLevel): CapturedLog[] {\n return capturedLogs.filter((log) => log.level === level);\n}\n\n/**\n * Clear all captured logs\n */\nexport function clearTestLogs(): void {\n capturedLogs.length = 0;\n}\n\n/**\n * Get the count of captured logs\n */\nexport function getTestLogsCount(): number {\n return capturedLogs.length;\n}\n","/**\n * Console logger adapter - for edge runtimes and browsers\n */\nimport type { LoggerConfig } from \"~/types/config.js\";\nimport type { LogLevel } from \"~/types/logger.js\";\n\ninterface ConsoleLoggerInstance {\n trace: (...args: any[]) => void;\n debug: (...args: any[]) => void;\n info: (...args: any[]) => void;\n warn: (...args: any[]) => void;\n error: (...args: any[]) => void;\n fatal: (...args: any[]) => void;\n}\n\nconst LOG_LEVELS: Record<LogLevel, number> = {\n trace: 0,\n debug: 1,\n info: 2,\n warn: 3,\n error: 4,\n fatal: 5,\n};\n\nconst defaultConfig: LoggerConfig = {\n level: \"info\",\n console: true,\n colors: true,\n name: \"app\",\n};\n\n/**\n * Create a console-based logger instance for edge runtimes\n */\nexport function createConsoleLogger(\n config: LoggerConfig = {},\n): ConsoleLoggerInstance {\n const finalConfig = { ...defaultConfig, ...config };\n const levelThreshold = LOG_LEVELS[finalConfig.level || \"info\"];\n const name = finalConfig.name || \"app\";\n\n const formatMessage = (level: LogLevel, ...args: any[]): string => {\n const timestamp = new Date().toISOString();\n const prefix = `[${timestamp}] [${level.toUpperCase()}] [${name}]`;\n return `${prefix} ${args.join(\" \")}`;\n };\n\n const shouldLog = (level: LogLevel): boolean => {\n return LOG_LEVELS[level] >= levelThreshold;\n };\n\n const logMethod = (level: LogLevel, consoleMethod: any) => {\n return (...args: any[]) => {\n if (!shouldLog(level)) return;\n if (!finalConfig.console) return;\n\n const message = formatMessage(level, ...args);\n consoleMethod(message);\n };\n };\n\n return {\n trace: logMethod(\"trace\", console.log),\n debug: logMethod(\"debug\", console.log),\n info: logMethod(\"info\", console.info),\n warn: logMethod(\"warn\", console.warn),\n error: logMethod(\"error\", console.error),\n fatal: logMethod(\"fatal\", console.error),\n };\n}\n","/**\n * Logger adapter factory - auto-detect environment and create appropriate logger\n */\nimport type { LoggerConfig, RuntimeEnvironment } from \"~/types/config.js\";\n\n/**\n * Detect the current runtime environment\n */\nexport function detectEnvironment(): RuntimeEnvironment {\n // Priority 0: Check for test environment (vitest, jest, etc.)\n // Test environments should use in-memory test adapter\n if (\n typeof process !== \"undefined\" &&\n (process.env.VITEST === \"true\" ||\n process.env.JEST_WORKER_ID !== undefined ||\n process.env.NODE_ENV === \"test\")\n ) {\n return \"test\";\n }\n\n // Priority 1: Check for Cloudflare Workers specific globals\n // Workers have caches API - this is the most reliable indicator\n // If these globals exist, it's definitely Cloudflare Workers runtime\n if (\n typeof globalThis !== \"undefined\" &&\n \"caches\" in globalThis &&\n \"Request\" in globalThis &&\n \"Response\" in globalThis\n ) {\n // Presence of Workers globals is sufficient - return immediately\n // Don't check fs/process as they're unreliable in wrangler dev with nodejs_compat\n return \"cloudflare-workers\";\n }\n\n // Priority 2: Check for Node.js\n if (\n typeof process !== \"undefined\" &&\n process.versions &&\n process.versions.node\n ) {\n return \"nodejs\";\n }\n\n // Fallback to browser\n return \"browser\";\n}\n\n/**\n * Create appropriate logger adapter based on environment\n *\n * Uses dynamic import() to conditionally load adapters based on runtime.\n * Tree-shaking in production builds ensures unused adapters are removed.\n */\nexport async function createLoggerAdapter(\n config: LoggerConfig = {},\n): Promise<any> {\n const env = config.environment || detectEnvironment();\n\n if (env === \"test\") {\n // Test adapter for vitest/jest - in-memory, silent by default\n const { createTestLogger } = await import(\"./test-adapter.js\");\n return createTestLogger(config);\n } else if (env === \"nodejs\") {\n // Use string concatenation to hide import from bundler static analysis\n // This prevents edge runtime bundlers from including Node.js-only dependencies\n const adapterPath = \"./pino-adapter\" + \".js\";\n const { createPinoLogger } = await import(/* @vite-ignore */ adapterPath);\n return createPinoLogger(config);\n } else {\n // Console adapter is safe for all environments\n const { createConsoleLogger } = await import(\"./console-adapter.js\");\n return createConsoleLogger(config);\n }\n}\n","/**\n * Default logger implementation\n */\nimport type { Logger, LoggerConfig } from \"~/types/index.js\";\nimport { createLoggerAdapter } from \"~/core/adapter-factory.js\";\n\nexport class DefaultLogger implements Logger {\n private adapter: any;\n private initPromise: Promise<void>;\n\n constructor(config: LoggerConfig = {}) {\n // Initialize adapter asynchronously\n this.initPromise = createLoggerAdapter(config).then((adapter) => {\n this.adapter = adapter;\n });\n }\n\n // All methods are typed as `any` for maximum flexibility\n trace: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.trace as any)(...args);\n };\n\n debug: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.debug as any)(...args);\n };\n\n info: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.info as any)(...args);\n };\n\n warn: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.warn as any)(...args);\n };\n\n error: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.error as any)(...args);\n };\n\n fatal: any = async (...args: any[]) => {\n await this.initPromise;\n (this.adapter.fatal as any)(...args);\n };\n}\n\n/**\n * Factory function to create a logger instance\n */\nexport function createLogger(config: LoggerConfig = {}): Logger {\n return new DefaultLogger(config);\n}\n"],"mappings":";;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkCA,SAAS,eAAe,QAAkB,QAAgB;AACxD,SAAO,mBAAmB,KAAK;AACjC;AAKA,SAAS,UAAU,OAAiB,UAA2B;AAC7D,SAAO,mBAAmB,KAAK,KAAK;AACtC;AAKA,SAAS,eACP,OACA,MACA,cACA,UACM;AAEN,MAAI,CAAC,UAAU,OAAO,QAAQ,GAAG;AAC/B;AAAA,EACF;AAEA,MAAI;AACJ,MAAI;AAGJ,MAAI,KAAK,WAAW,GAAG;AACrB,QAAI,OAAO,KAAK,CAAC,MAAM,UAAU;AAC/B,gBAAU,KAAK,CAAC;AAAA,IAClB,OAAO;AACL,gBAAU,KAAK,CAAC;AAChB,gBAAU,KAAK,UAAU,KAAK,CAAC,CAAC;AAAA,IAClC;AAAA,EACF,WAAW,KAA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AAC,GAAG;AAErC,SAAK,cAAc,oBAAoB,MAAM,EAAE,KAAK,CAAC,YAAY;AAC/D,WAAK,UAAU;AAAA,IACjB,CAAC;AAAA,EACH;AAAA;AAAA,EAGA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AAAA,EAEA,OAAY,UAAU,SAAgB;AACpC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,KAAa,GAAG,IAAI;AAAA,EACpC;AAAA,EAEA,OAAY,UAAU,SAAgB;AACpC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,KAAa,GAAG,IAAI;AAAA,EACpC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AAAA,EAEA,QAAa,UAAU,SAAgB;AACrC,UAAM,KAAK;AACX,IAAC,KAAK,QAAQ,MAAc,GAAG,IAAI;AAAA,EACrC;AACF;AAKO,SAAS,aAAa,SAAuB,CAAC,GAAW;AAC9D,SAAO,IAAI,cAAc,MAAM;AACjC;","names":["shouldLog","createTestLogger","createConsoleLogger"]} |
@@ -1,2 +0,2 @@ | ||
| import { L as LoggerConfig, a as Logger } from './logger-CraRs5pE.cjs'; | ||
| import { L as LoggerConfig, a as Logger } from './logger-Lmm6XEQc.cjs'; | ||
@@ -3,0 +3,0 @@ /** |
+1
-1
@@ -1,2 +0,2 @@ | ||
| import { L as LoggerConfig, a as Logger } from './logger-CraRs5pE.js'; | ||
| import { L as LoggerConfig, a as Logger } from './logger-Lmm6XEQc.js'; | ||
@@ -3,0 +3,0 @@ /** |
+7
-2
| { | ||
| "name": "@deepracticex/logger", | ||
| "version": "1.0.2", | ||
| "version": "1.1.0", | ||
| "description": "Unified logging system for Deepractice projects using Pino", | ||
@@ -29,2 +29,7 @@ "type": "module", | ||
| "require": "./dist/browser.cjs" | ||
| }, | ||
| "./test": { | ||
| "types": "./dist/test.d.ts", | ||
| "import": "./dist/test.js", | ||
| "require": "./dist/test.cjs" | ||
| } | ||
@@ -63,4 +68,4 @@ }, | ||
| "devDependencies": { | ||
| "@deepracticex/testing-utils": "workspace:*" | ||
| "@deepracticex/vitest-cucumber": "^1.2.0" | ||
| } | ||
| } |
+143
-2
@@ -75,2 +75,30 @@ # @deepracticex/logger | ||
| ### Test Environment (Vitest/Jest) | ||
| ```typescript | ||
| // Uses in-memory test adapter with log capture | ||
| import { | ||
| createLogger, | ||
| getTestLogs, | ||
| clearTestLogs, | ||
| } from "@deepracticex/logger/test"; | ||
| const logger = createLogger({ | ||
| level: "debug", | ||
| name: "my-service", | ||
| console: false, // Silent by default in tests | ||
| }); | ||
| // Your test code | ||
| logger.info("test message"); | ||
| // Assert logs were captured | ||
| const logs = getTestLogs(); | ||
| expect(logs).toHaveLength(1); | ||
| expect(logs[0].message).toBe("test message"); | ||
| // Clean up between tests | ||
| clearTestLogs(); | ||
| ``` | ||
| ## Quick Start | ||
@@ -161,2 +189,3 @@ | ||
| **File Structure:** | ||
| ``` | ||
@@ -186,2 +215,60 @@ ~/.deepractice/logs/ | ||
| ### Test Environment | ||
| Uses in-memory test adapter: | ||
| - Auto-detected in vitest/jest (via `VITEST=true` or `NODE_ENV=test`) | ||
| - Silent by default (no console output) | ||
| - Captures all logs in memory for assertions | ||
| - Zero I/O overhead for fast tests | ||
| - Utilities: `getTestLogs()`, `clearTestLogs()`, `getTestLogsByLevel()` | ||
| **Usage in tests:** | ||
| ```typescript | ||
| import { | ||
| createLogger, | ||
| getTestLogs, | ||
| clearTestLogs, | ||
| } from "@deepracticex/logger/test"; | ||
| describe("my feature", () => { | ||
| const logger = createLogger(); | ||
| afterEach(() => { | ||
| clearTestLogs(); // Clean up between tests | ||
| }); | ||
| it("should log messages", () => { | ||
| logger.info("test started"); | ||
| logger.warn("warning message"); | ||
| const logs = getTestLogs(); | ||
| expect(logs).toHaveLength(2); | ||
| expect(logs[0].level).toBe("info"); | ||
| }); | ||
| }); | ||
| ``` | ||
| **Auto-detection:** | ||
| When running in vitest, the default `@deepracticex/logger` import automatically uses the test adapter: | ||
| ```typescript | ||
| // Automatically uses test adapter in vitest | ||
| import { createLogger } from "@deepracticex/logger"; | ||
| const logger = createLogger(); // No console output in tests | ||
| ``` | ||
| **Enable console output for debugging:** | ||
| ```typescript | ||
| // Show logs in test output | ||
| const logger = createLogger({ console: true }); | ||
| // Or run with verbose reporter | ||
| // pnpm test -- --reporter=verbose | ||
| ``` | ||
| ## Examples | ||
@@ -252,6 +339,7 @@ | ||
| The logger uses a two-adapter architecture: | ||
| The logger uses a multi-adapter architecture with automatic runtime detection: | ||
| 1. **Pino Adapter** - For Node.js (high performance, file support) | ||
| 2. **Console Adapter** - For edge/browser (lightweight, universal) | ||
| 3. **Test Adapter** - For test environments (in-memory, inspectable) | ||
@@ -261,8 +349,16 @@ Platform-specific entry points ensure only the necessary adapter is bundled: | ||
| ``` | ||
| @deepracticex/logger → nodejs.ts → pino-adapter | ||
| @deepracticex/logger → Auto-detect → appropriate adapter | ||
| @deepracticex/logger/nodejs → nodejs.ts → pino-adapter | ||
| @deepracticex/logger/cloudflare-workers → cloudflare-workers.ts → console-adapter | ||
| @deepracticex/logger/browser → browser.ts → console-adapter | ||
| @deepracticex/logger/test → test.ts → test-adapter | ||
| ``` | ||
| **Auto-detection priority:** | ||
| 1. Test environment (VITEST=true or NODE_ENV=test) → test adapter | ||
| 2. Cloudflare Workers (caches API present) → console adapter | ||
| 3. Node.js (process.versions.node) → pino adapter | ||
| 4. Fallback → console adapter (browser) | ||
| ## Bundle Size | ||
@@ -273,2 +369,3 @@ | ||
| - Browser: ~1.5KB (console adapter only) | ||
| - Test: ~2KB (test adapter with inspection utilities) | ||
@@ -318,4 +415,48 @@ ## FAQ | ||
| ### How do I use it in tests? | ||
| The logger automatically detects test environments (vitest/jest) and uses the test adapter: | ||
| ```typescript | ||
| // Automatically silent in tests | ||
| import { createLogger } from "@deepracticex/logger"; | ||
| const logger = createLogger(); | ||
| logger.info("message"); // Captured, but no console output | ||
| ``` | ||
| To inspect logs in tests: | ||
| ```typescript | ||
| import { | ||
| createLogger, | ||
| getTestLogs, | ||
| clearTestLogs, | ||
| } from "@deepracticex/logger/test"; | ||
| const logger = createLogger(); | ||
| logger.info("test message"); | ||
| const logs = getTestLogs(); | ||
| expect(logs[0].message).toBe("test message"); | ||
| clearTestLogs(); // Clean up | ||
| ``` | ||
| ### How do I see logs when debugging tests? | ||
| Use vitest's verbose reporter: | ||
| ```bash | ||
| pnpm test -- --reporter=verbose | ||
| ``` | ||
| Or enable console output explicitly: | ||
| ```typescript | ||
| const logger = createLogger({ console: true }); | ||
| ``` | ||
| ## License | ||
| MIT |
| /** | ||
| * Runtime environment type | ||
| */ | ||
| type RuntimeEnvironment = "nodejs" | "cloudflare-workers" | "browser"; | ||
| /** | ||
| * Logger configuration interface | ||
| */ | ||
| interface LoggerConfig { | ||
| /** | ||
| * Log level threshold | ||
| * @default "info" | ||
| */ | ||
| level?: "trace" | "debug" | "info" | "warn" | "error" | "fatal"; | ||
| /** | ||
| * Enable console output | ||
| * @default true | ||
| */ | ||
| console?: boolean; | ||
| /** | ||
| * File logging configuration | ||
| * @default { dirname: "~/.deepractice/logs" } | ||
| */ | ||
| file?: boolean | { | ||
| dirname?: string; | ||
| }; | ||
| /** | ||
| * Enable colored output | ||
| * @default true | ||
| */ | ||
| colors?: boolean; | ||
| /** | ||
| * Package/service name for identification | ||
| * @default "app" | ||
| */ | ||
| name?: string; | ||
| /** | ||
| * Explicitly specify the runtime environment | ||
| * If not provided, will auto-detect | ||
| * @default auto-detect | ||
| */ | ||
| environment?: RuntimeEnvironment; | ||
| } | ||
| /** | ||
| * Logger interface - all methods accept any arguments for maximum flexibility | ||
| */ | ||
| interface Logger { | ||
| /** | ||
| * Log trace level message | ||
| */ | ||
| trace: any; | ||
| /** | ||
| * Log debug level message | ||
| */ | ||
| debug: any; | ||
| /** | ||
| * Log info level message | ||
| */ | ||
| info: any; | ||
| /** | ||
| * Log warning level message | ||
| */ | ||
| warn: any; | ||
| /** | ||
| * Log error level message | ||
| */ | ||
| error: any; | ||
| /** | ||
| * Log fatal level message | ||
| */ | ||
| fatal: any; | ||
| } | ||
| /** | ||
| * Log level type | ||
| */ | ||
| type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal"; | ||
| export type { LoggerConfig as L, Logger as a, LogLevel as b }; |
| /** | ||
| * Runtime environment type | ||
| */ | ||
| type RuntimeEnvironment = "nodejs" | "cloudflare-workers" | "browser"; | ||
| /** | ||
| * Logger configuration interface | ||
| */ | ||
| interface LoggerConfig { | ||
| /** | ||
| * Log level threshold | ||
| * @default "info" | ||
| */ | ||
| level?: "trace" | "debug" | "info" | "warn" | "error" | "fatal"; | ||
| /** | ||
| * Enable console output | ||
| * @default true | ||
| */ | ||
| console?: boolean; | ||
| /** | ||
| * File logging configuration | ||
| * @default { dirname: "~/.deepractice/logs" } | ||
| */ | ||
| file?: boolean | { | ||
| dirname?: string; | ||
| }; | ||
| /** | ||
| * Enable colored output | ||
| * @default true | ||
| */ | ||
| colors?: boolean; | ||
| /** | ||
| * Package/service name for identification | ||
| * @default "app" | ||
| */ | ||
| name?: string; | ||
| /** | ||
| * Explicitly specify the runtime environment | ||
| * If not provided, will auto-detect | ||
| * @default auto-detect | ||
| */ | ||
| environment?: RuntimeEnvironment; | ||
| } | ||
| /** | ||
| * Logger interface - all methods accept any arguments for maximum flexibility | ||
| */ | ||
| interface Logger { | ||
| /** | ||
| * Log trace level message | ||
| */ | ||
| trace: any; | ||
| /** | ||
| * Log debug level message | ||
| */ | ||
| debug: any; | ||
| /** | ||
| * Log info level message | ||
| */ | ||
| info: any; | ||
| /** | ||
| * Log warning level message | ||
| */ | ||
| warn: any; | ||
| /** | ||
| * Log error level message | ||
| */ | ||
| error: any; | ||
| /** | ||
| * Log fatal level message | ||
| */ | ||
| fatal: any; | ||
| } | ||
| /** | ||
| * Log level type | ||
| */ | ||
| type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal"; | ||
| export type { LoggerConfig as L, Logger as a, LogLevel as b }; |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
167374
72.82%34
21.43%1742
74.03%456
44.76%24
100%