Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(logging): only the edge runtime may configure LogTape

Sidwebworks70eda5784e6b
diff --git a/packages/database/supabase/functions/lib/logging.ts b/packages/database/supabase/functions/lib/logging.ts
index 6204c70e6b..e5121a49d2 100644
--- a/packages/database/supabase/functions/lib/logging.ts
+++ b/packages/database/supabase/functions/lib/logging.ts
@@ -29,19 +29,30 @@ let configured = false;
function ensureConfigured(): void {
if (configured) return;
+ configured = true;
- // Dual-runtime: this module is imported by the scheduling engine, which now
- // runs BOTH in the Deno edge runtime and in-process in Node (the app/jobs no
- // longer round-trip to the `schedule` edge function). `Deno` is undefined in
- // Node, so read the level from whichever env is present (mirrors the
- // `typeof Deno` guard in postgres/index.ts).
+ // Dual-runtime: this module is imported by the scheduling engine, which runs
+ // BOTH in the Deno edge runtime and in-process in Node (@carbon/ee/planning,
+ // the ERP/MES apps, @carbon/jobs).
+ //
+ // ONLY the edge runtime may configure LogTape. In Node the host app already
+ // owns it (`@carbon/logger`'s config.server / config.client, called from each
+ // app's entry.server/entry.client), and `configureSync({ reset: true })` here
+ // would REPLACE that config: the HTTP access sink disappears and the
+ // contextLocalStorage goes with it, so `withContext` in requestIdMiddleware
+ // warns on every request and requestId stops reaching any log line. The two
+ // guards can't see each other — @carbon/logger keys off a globalThis symbol,
+ // this file off a module-local flag — so last writer wins, silently.
+ //
+ // This second configuration exists at all because Deno cannot import
+ // @carbon/logger: it is an npm workspace package, and the edge runtime only
+ // mounts supabase/functions. Under Node we simply inherit the app's config;
+ // `getLogger` below works either way.
const denoEnv = (globalThis as { Deno?: { env: { get(k: string): string | undefined } } })
.Deno?.env;
- const rawLevel = denoEnv
- ? denoEnv.get("LOG_LEVEL")
- : (globalThis as { process?: { env?: Record<string, string | undefined> } })
- .process?.env?.LOG_LEVEL;
- const raw = rawLevel?.toLowerCase().trim();
+ if (!denoEnv) return;
+
+ const raw = denoEnv.get("LOG_LEVEL")?.toLowerCase().trim();
const level: Level =
raw && (LOG_LEVELS as readonly string[]).includes(raw)
? (raw as Level)
@@ -59,8 +70,6 @@ function ensureConfigured(): void {
{ category: ["logtape", "meta"], lowestLevel: "warning", sinks: ["console"] },
],
});
-
- configured = true;
}
/** Logger for an edge function → category `["carbon","edge",fnName]`. */