{"version":3,"file":"Logger.cjs","names":["context","LogRecordImpl","SeverityNumber","trace","isSpanContextValid","TraceFlags"],"sources":["../src/Logger.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { Logger as ILogger, LogRecord } from '@opentelemetry/api';\nimport { SeverityNumber } from '@opentelemetry/api';\nimport type { Context } from '@opentelemetry/api';\nimport {\n  context,\n  trace,\n  TraceFlags,\n  isSpanContextValid,\n} from '@opentelemetry/api';\n\nimport { LogRecordImpl } from './LogRecordImpl';\nimport type { LoggerProviderSharedState } from './internal/LoggerProviderSharedState';\nimport type { LoggerConfig } from './types';\nimport type { LogInstrumentationScope } from './internal/utils';\n\nexport class Logger implements ILogger {\n  private readonly _instrumentationScope: LogInstrumentationScope;\n  private readonly _sharedState: LoggerProviderSharedState;\n  private readonly _loggerConfig: Required<LoggerConfig>;\n\n  constructor(\n    instrumentationScope: LogInstrumentationScope,\n    sharedState: LoggerProviderSharedState\n  ) {\n    this._instrumentationScope = instrumentationScope;\n    this._sharedState = sharedState;\n    // Cache the logger configuration at construction time\n    // Since we don't support re-configuration, this avoids map lookups\n    // and string allocations on each emit() call\n    this._loggerConfig = this._sharedState.getLoggerConfig(\n      this._instrumentationScope\n    );\n  }\n\n  public emit(logRecord: LogRecord): void {\n    const currentContext = logRecord.context || context.active();\n    if (!this.enabled(logRecord)) {\n      return;\n    }\n\n    /**\n     * If a Logger was obtained with include_trace_context=true,\n     * the LogRecords it emits MUST automatically include the Trace Context from the active Context,\n     * if Context has not been explicitly set.\n     */\n    const logRecordInstance = new LogRecordImpl(\n      this._sharedState,\n      this._instrumentationScope,\n      {\n        context: currentContext,\n        ...logRecord,\n      }\n    );\n    this._sharedState.loggerMetrics.emitLog();\n    /**\n     * the explicitly passed Context,\n     * the current Context, or an empty Context if the Logger was obtained with include_trace_context=false\n     */\n    this._sharedState.activeProcessor.onEmit(logRecordInstance, currentContext);\n    /**\n     * A LogRecordProcessor may freely modify logRecord for the duration of the OnEmit call.\n     * If logRecord is needed after OnEmit returns (i.e. for asynchronous processing) only reads are permitted.\n     */\n    logRecordInstance._makeReadonly();\n  }\n\n  public enabled(options?: {\n    context?: Context;\n    severityNumber?: SeverityNumber;\n    eventName?: string;\n  }): boolean {\n    if (this._sharedState.hasShutdown) {\n      return false;\n    }\n\n    const loggerConfig = this._loggerConfig;\n\n    if (!loggerConfig.enabled) {\n      return false;\n    }\n\n    // Severity number given and lower than the min configured\n    const severityNumber = options?.severityNumber;\n    if (\n      typeof severityNumber === 'number' &&\n      severityNumber !== SeverityNumber.UNSPECIFIED &&\n      severityNumber < loggerConfig.minimumSeverity\n    ) {\n      return false;\n    }\n\n    const currentContext = options?.context || context.active();\n    // Trace based: the context (given or the active) has a unsampled Span\n    if (loggerConfig.traceBased) {\n      const spanContext = trace.getSpanContext(currentContext);\n      if (spanContext && isSpanContextValid(spanContext)) {\n        const isSampled =\n          (spanContext.traceFlags & TraceFlags.SAMPLED) === TraceFlags.SAMPLED;\n        if (!isSampled) {\n          return false;\n        }\n      }\n    }\n\n    // Lastly check if there is any enabled processor\n    const enabledOpts = {\n      context: currentContext,\n      instrumentationScope: this._instrumentationScope,\n      severityNumber: options?.severityNumber,\n      eventName: options?.eventName,\n    };\n    for (const processor of this._sharedState.processors) {\n      if (!processor.enabled || processor.enabled(enabledOpts)) {\n        return true;\n      }\n    }\n    return false;\n  }\n}\n"],"mappings":";;;AAoBA,IAAa,SAAb,MAAuC;CACrC;CACA;CACA;CAEA,YACE,sBACA,aACA;EACA,KAAK,wBAAwB;EAC7B,KAAK,eAAe;EAIpB,KAAK,gBAAgB,KAAK,aAAa,gBACrC,KAAK,qBACP;CACF;CAEA,KAAY,WAA4B;EACtC,MAAM,iBAAiB,UAAU,WAAWA,mBAAAA,QAAQ,OAAO;EAC3D,IAAI,CAAC,KAAK,QAAQ,SAAS,GACzB;;;;;;EAQF,MAAM,oBAAoB,IAAIC,sBAAAA,cAC5B,KAAK,cAC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