diff --git a/src/graphql/fetchLocalData.ts b/src/graphql/fetchLocalData.ts index 523b2c3..e5451b2 100644 --- a/src/graphql/fetchLocalData.ts +++ b/src/graphql/fetchLocalData.ts @@ -10,7 +10,7 @@ import { getLogger } from "helpers/logger"; config({ path: resolve(process.cwd(), ".env.local") }); const LOCAL_DATA_FOLDER = `${process.cwd()}/public/local-data`; -const logger = getLogger("💽 [Local Data]"); +const logger = getLogger("💽 [Local Data]", "server"); const writeLocalData = (name: LocalDataFile, localData: unknown) => { const path = `${LOCAL_DATA_FOLDER}/${name}.json`; diff --git a/src/graphql/icuToTypescript.ts b/src/graphql/icuToTypescript.ts index d6c47e8..3198930 100644 --- a/src/graphql/icuToTypescript.ts +++ b/src/graphql/icuToTypescript.ts @@ -6,7 +6,7 @@ import { filterDefined } from "helpers/asserts"; import { getLogger } from "helpers/logger"; const OUTPUT_FOLDER = `${process.cwd()}/src/graphql`; -const logger = getLogger("💽 [ICU to TS]"); +const logger = getLogger("💽 [ICU to TS]", "server"); const icuToTypescript = () => { // eslint-disable-next-line @typescript-eslint/no-unused-vars diff --git a/src/helpers/logger.ts b/src/helpers/logger.ts index 4ec5289..78975c9 100644 --- a/src/helpers/logger.ts +++ b/src/helpers/logger.ts @@ -1,13 +1,29 @@ +type LoggerMode = "both" | "client" | "server"; + +const isServer = typeof window === "undefined"; + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types -export const getLogger = (prefix: string) => ({ - error: (message?: unknown, ...optionalParams: unknown[]) => - console.error(prefix, message, ...optionalParams), - warn: (message?: unknown, ...optionalParams: unknown[]) => - console.warn(prefix, message, ...optionalParams), - log: (message?: unknown, ...optionalParams: unknown[]) => - console.log(prefix, message, ...optionalParams), - info: (message?: unknown, ...optionalParams: unknown[]) => - console.info(prefix, message, ...optionalParams), - debug: (message?: unknown, ...optionalParams: unknown[]) => - console.debug(prefix, message, ...optionalParams), -}); +export const getLogger = (prefix: string, mode: LoggerMode = "client") => { + if ((mode === "client" && isServer) || (mode === "server" && !isServer)) { + return { + error: () => null, + warn: () => null, + log: () => null, + info: () => null, + debug: () => null, + }; + } + + return { + error: (message?: unknown, ...optionalParams: unknown[]) => + console.error(prefix, message, ...optionalParams), + warn: (message?: unknown, ...optionalParams: unknown[]) => + console.warn(prefix, message, ...optionalParams), + log: (message?: unknown, ...optionalParams: unknown[]) => + console.log(prefix, message, ...optionalParams), + info: (message?: unknown, ...optionalParams: unknown[]) => + console.info(prefix, message, ...optionalParams), + debug: (message?: unknown, ...optionalParams: unknown[]) => + console.debug(prefix, message, ...optionalParams), + }; +};