Only display client logging on the browser

This commit is contained in:
DrMint 2023-02-22 06:45:22 +01:00
parent 75de7c5f2a
commit 7eb7495537
3 changed files with 30 additions and 14 deletions

View File

@ -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`;

View File

@ -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

View File

@ -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),
};
};