import { isLevelEnabled } from "./level.js";
function formatBindingValue(value) {
  if (typeof value === "string") return value;
  if (value instanceof Error) return value.message;
  if (typeof value === "object" && value !== null) {
    try {
      return JSON.stringify(value) ?? String(value);
    } catch {
      return String(value);
    }
  }
  return String(value);
}
function formatBindings(bindings) {
  return Object.entries(bindings).filter(([, value]) => value !== void 0).map(([key, value]) => `${key}=${formatBindingValue(value)}`).join(" ");
}
function createConsoleLogger(namespace, bindings = {}) {
  const emit = (level, msg, fields) => {
    if (!isLevelEnabled(level)) return;
    const { err, ...rest } = { ...bindings, ...fields };
    const suffix = formatBindings(rest);
    const line = suffix ? `[${namespace}] ${msg} ${suffix}` : `[${namespace}] ${msg}`;
    if (err instanceof Error) console[level](line, err.stack ?? err.message);
    else if (err !== void 0) console[level](line, err);
    else console[level](line);
  };
  return {
    debug: (msg, fields) => emit("debug", msg, fields),
    info: (msg, fields) => emit("info", msg, fields),
    warn: (msg, fields) => emit("warn", msg, fields),
    error: (msg, fields) => emit("error", msg, fields),
    child: (childBindings) => createConsoleLogger(namespace, { ...bindings, ...childBindings })
  };
}
export {
  createConsoleLogger,
  formatBindings
};
//# sourceMappingURL=transport.console.js.map
