import { selectTransport } from "./transport.js";
import { getLoggerExtension } from "./extension.js";
import { isLevelEnabled } from "./level.js";
import { getLogLevel, isLevelEnabled as isLevelEnabled2, resetLogLevelCache, OM_LOG_LEVEL_ENV } from "./level.js";
import { resetServerLoggerCache, OM_LOG_DESTINATION_ENV } from "./transport.server.js";
import { resetLogPrettyCache, OM_LOG_PRETTY_ENV } from "./transport.pretty.js";
import {
  getLoggerExtension as getLoggerExtension2,
  registerLoggerExtension,
  resetLoggerExtension
} from "./extension.js";
const loggerRegistry = /* @__PURE__ */ new Map();
function createExtendedLogger(namespace, transport, bindings = {}) {
  const emit = (level, msg, fields) => {
    if (!isLevelEnabled(level)) return;
    const extension = getLoggerExtension();
    let context = {};
    try {
      context = extension?.enrich?.() ?? {};
    } catch {
    }
    const mergedFields = { ...fields, ...context };
    transport[level](msg, mergedFields);
    try {
      extension?.emit?.({
        level,
        namespace,
        message: msg,
        fields: { ...bindings, ...mergedFields },
        time: Date.now()
      });
    } catch {
    }
  };
  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) => createExtendedLogger(
      namespace,
      transport.child(childBindings),
      { ...bindings, ...childBindings }
    )
  };
}
function createLogger(namespace) {
  const existing = loggerRegistry.get(namespace);
  if (existing) return existing;
  const created = createExtendedLogger(namespace, selectTransport(namespace));
  loggerRegistry.set(namespace, created);
  return created;
}
function resetLoggerRegistry() {
  loggerRegistry.clear();
}
export {
  OM_LOG_DESTINATION_ENV,
  OM_LOG_LEVEL_ENV,
  OM_LOG_PRETTY_ENV,
  createLogger,
  getLogLevel,
  getLoggerExtension2 as getLoggerExtension,
  isLevelEnabled2 as isLevelEnabled,
  registerLoggerExtension,
  resetLogLevelCache,
  resetLogPrettyCache,
  resetLoggerExtension,
  resetLoggerRegistry,
  resetServerLoggerCache
};
//# sourceMappingURL=index.js.map
