"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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);
var log_exports = {};
__export(log_exports, {
  enableOtelLogging: () => enableOtelLogging,
  initializeLogger: () => initializeLogger,
  log: () => log,
  loggerOptions: () => loggerOptions
});
module.exports = __toCommonJS(log_exports);
var import_node_stream = require("node:stream");
var import_pino = require("pino");
var import_pino_pretty = require("pino-pretty");
var import_pino_otel_transport = require("./telemetry/pino_otel_transport.cjs");
const LOGGER_KEY = Symbol.for("@livekit/agents:logger");
const LOGGER_OPTIONS_KEY = Symbol.for("@livekit/agents:loggerOptions");
const OTEL_ENABLED_KEY = Symbol.for("@livekit/agents:otelEnabled");
const globals = globalThis;
const loggerOptions = () => globals[LOGGER_OPTIONS_KEY];
const log = () => {
  const logger = globals[LOGGER_KEY];
  if (!logger) {
    throw new TypeError("logger not initialized. did you forget to run initializeLogger()?");
  }
  return logger;
};
const createLogger = ({ pretty, level }) => {
  const logLevel = level || "info";
  const streams = [
    { stream: pretty ? (0, import_pino_pretty.build)({ colorize: true }) : process.stdout, level: logLevel },
    { stream: new OtelDestination(), level: "debug" }
  ];
  return (0, import_pino.pino)(
    { level: logLevel, serializers: { error: import_pino.pino.stdSerializers.err } },
    (0, import_pino.multistream)(streams)
  );
};
const initializeLogger = ({ pretty, level }) => {
  globals[LOGGER_OPTIONS_KEY] = { pretty, level };
  globals[LOGGER_KEY] = createLogger({ pretty, level });
};
class OtelDestination extends import_node_stream.Writable {
  _write(chunk, _encoding, callback) {
    try {
      if (!globals[OTEL_ENABLED_KEY]) {
        callback();
        return;
      }
      const line = chunk.toString().trim();
      if (line) {
        const logObj = JSON.parse(line);
        (0, import_pino_otel_transport.emitToOtel)(logObj);
      }
    } catch {
    }
    callback();
  }
}
const enableOtelLogging = () => {
  if (globals[OTEL_ENABLED_KEY] || !globals[LOGGER_KEY]) {
    console.warn("OTEL logging already enabled or logger not initialized");
    return;
  }
  globals[OTEL_ENABLED_KEY] = true;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  enableOtelLogging,
  initializeLogger,
  log,
  loggerOptions
});
//# sourceMappingURL=log.cjs.map