import { logs, SeverityNumber } from "@opentelemetry/api-logs";
import { errorOrObject } from "../../../shared/src/logging.js";
import { stringify } from "../../../shared/src/bigint-json.js";
import { startOtelAuto } from "./otel-start.js";
class OtelLogSink {
  #logger;
  constructor() {
    startOtelAuto();
    this.#logger = logs.getLogger("zero-cache");
  }
  log(level, context, ...args) {
    const lastObj = errorOrObject(args.at(-1));
    if (lastObj) {
      args.pop();
    }
    let message = args.length ? args.map((s) => typeof s === "string" ? s : stringify(s)).join(" ") : "";
    if (lastObj) {
      message += ` ${stringify(lastObj)}`;
    }
    const payload = {
      severityText: level,
      severityNumber: toErrorNum(level),
      body: message
    };
    if (context) {
      payload.attributes = context;
    }
    this.#logger.emit(payload);
  }
}
function toErrorNum(level) {
  switch (level) {
    case "error":
      return SeverityNumber.ERROR;
    case "warn":
      return SeverityNumber.WARN;
    case "info":
      return SeverityNumber.INFO;
    case "debug":
      return SeverityNumber.DEBUG;
    default:
      throw new Error(`Unknown log level: ${level}`);
  }
}
export {
  OtelLogSink
};
//# sourceMappingURL=otel-log-sink.js.map
