{"version":3,"file":"LoggerConfigurators.mjs","names":[],"sources":["../../src/config/LoggerConfigurators.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { InstrumentationScope } from '@opentelemetry/core';\nimport { SeverityNumber } from '@opentelemetry/api';\nimport type { LoggerConfig, LoggerConfigurator } from '../types';\n\n/**\n * Default LoggerConfig used when no pattern matches\n *\n * @experimental This feature is in development as per the OpenTelemetry specification.\n */\nconst DEFAULT_LOGGER_CONFIG: Required<LoggerConfig> = {\n  enabled: true,\n  minimumSeverity: SeverityNumber.UNSPECIFIED,\n  traceBased: false,\n};\n\n/**\n * Configuration for a specific logger pattern\n *\n * @experimental This feature is in development as per the OpenTelemetry specification.\n */\nexport interface LoggerPattern {\n  /**\n   * The logger name or pattern to match.\n   * Use '*' for wildcard matching.\n   *\n   * @experimental This feature is in development as per the OpenTelemetry specification.\n   */\n  pattern: string;\n\n  /**\n   * The configuration to apply to matching loggers.\n   * Partial config is allowed; unspecified properties will use defaults.\n   *\n   * @experimental This feature is in development as per the OpenTelemetry specification.\n   */\n  config: LoggerConfig;\n}\n\n/**\n * Creates a LoggerConfigurator from an array of logger patterns.\n * Patterns are evaluated in order, and the first matching pattern's config is used.\n * Supports exact matching and simple wildcard patterns with '*'.\n *\n * The returned configurator computes a complete LoggerConfig by merging the matched\n * pattern's config with default values for any unspecified properties.\n *\n * @param patterns - Array of logger patterns with their configurations\n * @returns A LoggerConfigurator function that computes complete LoggerConfig\n * @experimental This feature is in development as per the OpenTelemetry specification.\n *\n * @example\n * ```typescript\n * const configurator = createLoggerConfigurator([\n *   { pattern: 'debug-logger', config: { minimumSeverity: SeverityNumber.DEBUG } },\n *   { pattern: 'prod-*', config: { minimumSeverity: SeverityNumber.WARN } },\n *   { pattern: '*', config: { minimumSeverity: SeverityNumber.INFO } },\n * ]);\n * ```\n */\nexport function createLoggerConfigurator(\n  patterns: LoggerPattern[]\n): LoggerConfigurator {\n  return (loggerScope: InstrumentationScope): Required<LoggerConfig> => {\n    const loggerName = loggerScope.name;\n\n    // Find the first matching pattern\n    for (const { pattern, config } of patterns) {\n      if (matchesPattern(loggerName, pattern)) {\n        // Compute complete config by merging with defaults\n        return {\n          enabled: config.enabled ?? DEFAULT_LOGGER_CONFIG.enabled,\n          minimumSeverity:\n            config.minimumSeverity ?? DEFAULT_LOGGER_CONFIG.minimumSeverity,\n          traceBased: config.traceBased ?? DEFAULT_LOGGER_CONFIG.traceBased,\n        };\n      }\n    }\n\n    // No pattern matched, return default config\n    return { ...DEFAULT_LOGGER_CONFIG };\n  };\n}\n\n/**\n * Matches a logger name against a pattern.\n * Supports simple wildcard matching with '*'.\n *\n * @param name - The logger name to match\n * @param pattern - The pattern to match against (supports '*' wildcard)\n * @returns true if the name matches the pattern\n */\nfunction matchesPattern(name: string, pattern: string): boolean {\n  // Exact match\n  if (pattern === name) {\n    return true;\n  }\n\n  // Wildcard pattern\n  if (pattern.includes('*')) {\n    const regexPattern = pattern\n      .split('*')\n      .map(part => part.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&'))\n      .join('.*');\n    const regex = new RegExp(`^${regexPattern}$`);\n    return regex.test(name);\n  }\n\n  return false;\n}\n"],"mappings":";;;;;;;AAcA,MAAM,wBAAgD;CACpD,SAAS;CACT,iBAAiB,eAAe;CAChC,YAAY;AACd;;;;;;;;;;;;;;;;;;;;;;AA8CA,SAAgB,yBACd,UACoB;CACpB,QAAQ,gBAA8D;EACpE,MAAM,aAAa,YAAY;EAG/B,KAAK,MAAM,EAAE,SAAS,YAAY,UAChC,IAAI,eAAe,YAAY,OAAO,GAEpC,OAAO;GACL,SAAS,OAAO,WAAW,sBAAsB;GACjD,iBACE,OAAO,mBAAmB,sBAAsB;GAClD,YAAY,OAAO,cAAc,sBAAsB;EACzD;EAKJ,OAAO,EAAE,GAAG,sBAAsB;CACpC;AACF;;;;;;;;;AAUA,SAAS,eAAe,MAAc,SAA0B;CAE9D,IAAI,YAAY,MACd,OAAO;CAIT,IAAI,QAAQ,SAAS,GAAG,GAAG;EACzB,MAAM,eAAe,QAClB,MAAM,GAAG,CAAC,CACV,KAAI,SAAQ,KAAK,QAAQ,uBAAuB,MAAM,CAAC,CAAC,CACxD,KAAK,IAAI;EAEZ,OAAO,IADW,OAAO,IAAI,aAAa,EAC/B,CAAC,CAAC,KAAK,IAAI;CACxB;CAEA,OAAO;AACT"}