chore: add granular logging to the logger (#16734)
* Update README.md * Update README.md * Update README.md * Update README.md * Update turbo.json * Update trpc-provider.tsx * Update logger.ts * update prisma index * Update trpc.ts * Update trpc-provider.tsx * Update trpc.ts * Update .env.example * Update logger.ts
This commit is contained in:
@@ -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=
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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`
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 =
|
||||
|
||||
@@ -73,7 +73,7 @@ export const trpc: CreateTRPCNext<AppRouter, NextPageContext, null> = 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`
|
||||
|
||||
+1
-1
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user