1export interface DebugLogger {
2 readonly isEnabled: () => boolean;
3 readonly log: (event: string, data?: Record<string, unknown>) => void;
4}
5
6function globalValue(name: string): unknown {
7 return typeof globalThis === "undefined"
8 ? undefined
9 : (globalThis as Record<string, unknown>)[name];
10}
11
12export function createDebugLogger(namespace: string): DebugLogger {
13 const isEnabled = () => {
14 const filter = globalValue("__T3_DEBUG__");
15 return filter === true || (Array.isArray(filter) && filter.includes(namespace));
16 };
17 const log = (event: string, data?: Record<string, unknown>) => {
18 if (!isEnabled()) {
19 return;
20 }
21 if (data === undefined) {
22 console.log(`[t3-${namespace}] ${event}`);
23 } else {
24 console.log(`[t3-${namespace}] ${event}`, data);
25 }
26 };
27 return { isEnabled, log };
28}