Open History to see the last commit
1export interface DebugLogger {
2 readonly isEnabled: () => boolean;
3 readonly log: (event: string, data?: Record<string, unknown>) => void;
4}
5
6export interface DebugLoggerOptions {
7 readonly enabledInDev?: boolean;
8 readonly legacyGlobalFlag?: string;
9}
10
11function globalValue(name: string): unknown {
12 return typeof globalThis === "undefined"
13 ? undefined
14 : (globalThis as Record<string, unknown>)[name];
15}
16
17export function createDebugLogger(
18 namespace: string,
19 options: DebugLoggerOptions = {},
20): DebugLogger {
21 const isEnabled = () => {
22 if (options.enabledInDev === true && typeof __DEV__ !== "undefined" && __DEV__) {
23 return true;
24 }
25 if (options.legacyGlobalFlag !== undefined && globalValue(options.legacyGlobalFlag) === true) {
26 return true;
27 }
28 const filter = globalValue("__T3_DEBUG__");
29 return filter === true || (Array.isArray(filter) && filter.includes(namespace));
30 };
31 const log = (event: string, data?: Record<string, unknown>) => {
32 if (!isEnabled()) {
33 return;
34 }
35 if (data === undefined) {
36 console.log(`[t3-${namespace}] ${event}`);
37 } else {
38 console.log(`[t3-${namespace}] ${event}`, data);
39 }
40 };
41 return { isEnabled, log };
42}