diff --git a/.env.example b/.env.example index 9c4494817a..ff2e2c1875 100644 --- a/.env.example +++ b/.env.example @@ -394,3 +394,7 @@ VAPID_PRIVATE_KEY= # Custom privacy policy / terms URLs (for self-hosters: change to your privacy policy / terms URLs) NEXT_PUBLIC_WEBSITE_PRIVACY_POLICY_URL= NEXT_PUBLIC_WEBSITE_TERMS_URL= + +# NEXT_PUBLIC_LOGGER_LEVEL=3 sets to log info, warn, error and fatal logs. +# [0: silly & upwards, 1: trace & upwards, 2: debug & upwards, 3: info & upwards, 4: warn & upwards, 5: error & fatal, 6: fatal] +NEXT_PUBLIC_LOGGER_LEVEL= diff --git a/README.md b/README.md index dee240506a..bea598d0a6 100644 --- a/README.md +++ b/README.md @@ -173,11 +173,29 @@ yarn dx #### Development tip -> Add `NEXT_PUBLIC_DEBUG=1` anywhere in your `.env` to get logging information for all the queries and mutations driven by **tRPC**. +Add `NEXT_PUBLIC_LOGGER_LEVEL={level}` to your .env file to control the logging verbosity for all tRPC queries and mutations.\ +Where {level} can be one of the following: + +`0` for silly \ +`1` for trace \ +`2` for debug \ +`3` for info \ +`4` for warn \ +`5` for error \ +`6` for fatal + +When you set `NEXT_PUBLIC_LOGGER_LEVEL={level}` in your .env file, it enables logging at that level and higher. Here's how it works: + +The logger will include all logs that are at the specified level or higher. For example: \ +- If you set `NEXT_PUBLIC_LOGGER_LEVEL=2`, it will log from level 2 (debug) upwards, meaning levels 2 (debug), 3 (info), 4 (warn), 5 (error), and (fatal) will be logged. \ +- If you set `NEXT_PUBLIC_LOGGER_LEVEL=3`, it will log from level 3 (info) upwards, meaning levels 3 (info), 4 (warn), 5 (error), and 6 (fatal) will be logged, but level 2 (debug) and level 1 (trace) will be ignored. \ + + ```sh -echo 'NEXT_PUBLIC_DEBUG=1' >> .env +echo 'NEXT_PUBLIC_LOGGER_LEVEL=3' >> .env ``` +for Logger level to be set at info, for example. #### Gitpod Setup diff --git a/apps/web/app/_trpc/trpc-provider.tsx b/apps/web/app/_trpc/trpc-provider.tsx index 45100bd89f..6b80a08aba 100644 --- a/apps/web/app/_trpc/trpc-provider.tsx +++ b/apps/web/app/_trpc/trpc-provider.tsx @@ -55,7 +55,7 @@ export const TrpcProvider: React.FC<{ children: React.ReactNode; dehydratedState // adds pretty logs to your console in development and logs errors in production loggerLink({ enabled: (opts) => - !!process.env.NEXT_PUBLIC_DEBUG || (opts.direction === "down" && opts.result instanceof Error), + (typeof process.env.NEXT_PUBLIC_LOGGER_LEVEL === 'number' && process.env.NEXT_PUBLIC_LOGGER_LEVEL >= 0) || (opts.direction === "down" && opts.result instanceof Error), }), splitLink({ // check for context property `skipBatch` diff --git a/packages/lib/logger.ts b/packages/lib/logger.ts index 4d6d4be3c6..7f00bfbdb0 100644 --- a/packages/lib/logger.ts +++ b/packages/lib/logger.ts @@ -3,7 +3,7 @@ import { Logger } from "tslog"; import { IS_PRODUCTION } from "./constants"; const logger = new Logger({ - minLevel: !!process.env.NEXT_PUBLIC_DEBUG ? 2 : 4, + minLevel: parseInt(process.env.NEXT_PUBLIC_LOGGER_LEVEL ?? "4"), maskValuesOfKeys: ["password", "passwordConfirmation", "credentials", "credential"], prettyLogTimeZone: IS_PRODUCTION ? "UTC" : "local", prettyErrorStackTemplate: " • {{fileName}}\t{{method}}\n\t{{filePathWithLine}}", // default diff --git a/packages/prisma/index.ts b/packages/prisma/index.ts index 35214516b4..7fec983adc 100644 --- a/packages/prisma/index.ts +++ b/packages/prisma/index.ts @@ -15,7 +15,26 @@ const globalForPrisma = global as unknown as { prismaWithClientExtensions: PrismaClientWithExtensions; }; -if (!!process.env.NEXT_PUBLIC_DEBUG) prismaOptions.log = ["query", "error", "warn"]; +const loggerLevel = parseInt(process.env.NEXT_PUBLIC_LOGGER_LEVEL ?? "", 10); + +if (!isNaN(loggerLevel)) { + switch (loggerLevel) { + case 5: + case 6: + prismaOptions.log = ["error"]; + break; + case 4: + prismaOptions.log = ["warn", "error"]; + break; + case 3: + prismaOptions.log = ["info", "error", "warn"]; + break; + default: + // For values 0, 1, 2 (or anything else below 3) + prismaOptions.log = ["query", "info", "error", "warn"]; + break; + } +} // Prevents flooding with idle connections const prismaWithoutClientExtensions = diff --git a/packages/trpc/react/trpc.ts b/packages/trpc/react/trpc.ts index 0ec521c07b..a2b9343181 100644 --- a/packages/trpc/react/trpc.ts +++ b/packages/trpc/react/trpc.ts @@ -73,7 +73,7 @@ export const trpc: CreateTRPCNext = createTRPC // adds pretty logs to your console in development and logs errors in production loggerLink({ enabled: (opts) => - !!process.env.NEXT_PUBLIC_DEBUG || (opts.direction === "down" && opts.result instanceof Error), + (typeof process.env.NEXT_PUBLIC_LOGGER_LEVEL === 'number' && process.env.NEXT_PUBLIC_LOGGER_LEVEL >= 0) || (opts.direction === "down" && opts.result instanceof Error), }), splitLink({ // check for context property `skipBatch` diff --git a/turbo.json b/turbo.json index 9847db96b4..3091f2940a 100644 --- a/turbo.json +++ b/turbo.json @@ -342,7 +342,7 @@ "NEXT_PUBLIC_CALCOM_VERSION", "NEXT_PUBLIC_COMPANY_NAME", "NEXT_PUBLIC_CONSOLE_URL", - "NEXT_PUBLIC_DEBUG", + "NEXT_PUBLIC_LOGGER_LEVEL", "NEXT_PUBLIC_DISABLE_SIGNUP", "NEXT_PUBLIC_EMBED_LIB_URL", "NEXT_PUBLIC_FORMBRICKS_HOST_URL",