Scheduling infrastructure for absolutely everyone.
chore: add granular logging to the logger (#16734)
Syed Ali Shahbazc4a13829828f
diff --git a/.env.example b/.env.exampleindex 9c4494817ac..ff2e2c1875f 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.mdindex dee240506a4..bea598d0a6d 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 Setupdiff --git a/apps/web/app/_trpc/trpc-provider.tsx b/apps/web/app/_trpc/trpc-provider.tsxindex 45100bd89f4..6b80a08aba1 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 productionloggerLink({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.tsindex 4d6d4be3c67..7f00bfbdb04 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}}", // defaultdiff --git a/packages/prisma/index.ts b/packages/prisma/index.tsindex 35214516b45..7fec983adc0 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 connectionsconst prismaWithoutClientExtensions =diff --git a/packages/trpc/react/trpc.ts b/packages/trpc/react/trpc.tsindex 0ec521c07b7..a2b93431813 100644--- a/packages/trpc/react/trpc.ts+++ b/packages/trpc/react/trpc.ts@@ -73,7 +73,7 @@ export const trpc: CreateTRPCNext<AppRouter, NextPageContext, null> = createTRPC// adds pretty logs to your console in development and logs errors in productionloggerLink({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.jsonindex 9847db96b46..3091f2940ad 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",