Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/utils/logger.server.ts
T

30 lines
1.4 KiB
TypeScript

import { createChildLogger, defaultLogger, type HiveGridLogger } from "@hivegrid/logger";
import { getWebServiceName, type WebBuildSurface } from "../../build-surface";
export interface WebLogIdentity {
service: "hivegrid-web" | "hivegrid-admin" | "hivegrid-portal";
surface: WebBuildSurface;
}
export function requireWebLogIdentity(env: Readonly<{ NODE_ENV?: string; VITEST?: string; WEB_RUNTIME_SURFACE?: string }> = process.env): WebLogIdentity {
const surface = env.WEB_RUNTIME_SURFACE;
if (surface === "core" || surface === "admin" || surface === "portal") {
return { service: getWebServiceName(surface), surface };
}
if (surface === undefined && (env.NODE_ENV === "test" || env.VITEST === "true")) {
return { service: getWebServiceName("core"), surface: "core" };
}
throw new Error("WEB_RUNTIME_SURFACE must be explicitly set to core, admin, or portal before web logger initialization");
}
// Top-level logger for the React Router web runtime. Use webLogger directly
// for one-off server-side messages or .child({ scope: "..." }) for module-scoped
// loggers that auto-prefix every record.
export const webLogger: HiveGridLogger = defaultLogger.child(requireWebLogIdentity());
export function createScopedLogger(scope: string, bindings: Record<string, unknown> = {}): HiveGridLogger {
return createChildLogger(webLogger, { scope, ...bindings });
}
export type { HiveGridLogger };