Skip to content

Commit 6021100

Browse files
authored
Add configuration for log level and services (#344)
* Add configuration for log level and services * Update
1 parent 9eb5d56 commit 6021100

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/utils/env.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ export const env = createEnv({
4747
NODE_ENV: z
4848
.enum(["production", "development", "testing", "local"])
4949
.default("development"),
50+
LOG_LEVEL: z
51+
.enum(["fatal", "error", "warn", "info", "debug", "trace"])
52+
.default("debug"),
53+
LOG_SERVICES: z
54+
.string()
55+
.default("server,worker")
56+
.transform((s) =>
57+
z.array(z.enum(["server", "worker"])).parse(s.split(",")),
58+
),
5059
THIRDWEB_API_SECRET_KEY: z.string().min(1),
5160
ADMIN_WALLET_ADDRESS: z.string().min(1),
5261
ENCRYPTION_PASSWORD: z.string().min(1),
@@ -68,6 +77,8 @@ export const env = createEnv({
6877
isServer: true,
6978
runtimeEnvStrict: {
7079
NODE_ENV: process.env.NODE_ENV,
80+
LOG_LEVEL: process.env.LOG_LEVEL,
81+
LOG_SERVICES: process.env.LOG_SERVICES,
7182
THIRDWEB_API_SECRET_KEY: process.env.THIRDWEB_API_SECRET_KEY,
7283
ADMIN_WALLET_ADDRESS: process.env.ADMIN_WALLET_ADDRESS,
7384
ENCRYPTION_PASSWORD: process.env.ENCRYPTION_PASSWORD,

src/utils/logger.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,14 @@ const defaultOptions: LoggerOptions = {
1111
singleLine: true,
1212
},
1313
},
14-
level:
15-
env.NODE_ENV === "production"
16-
? "info"
17-
: env.NODE_ENV === "development"
18-
? "debug"
19-
: env.NODE_ENV === "testing"
20-
? "debug"
21-
: "trace",
14+
level: env.LOG_LEVEL,
2215
};
2316

2417
const pino = Pino(defaultOptions);
2518

2619
interface LoggerParams {
27-
service: "server" | "worker";
28-
level: "trace" | "debug" | "info" | "warn" | "error" | "fatal";
20+
service: (typeof env)["LOG_SERVICES"][0];
21+
level: (typeof env)["LOG_LEVEL"];
2922
message: string;
3023
queueId?: string | null;
3124
error?: any;
@@ -40,6 +33,10 @@ export const logger = ({
4033
error,
4134
data,
4235
}: LoggerParams) => {
36+
if (!env.LOG_SERVICES.includes(service)) {
37+
return;
38+
}
39+
4340
let prefix = `[${service.charAt(0).toUpperCase() + service.slice(1)}] `;
4441
if (queueId) {
4542
prefix += `[Transaction] [${queueId}] `;

0 commit comments

Comments
 (0)