Skip to content

Commit a0c4fb2

Browse files
authored
Fix: exclude health and root path (#54)
2 parents 6630df7 + da26030 commit a0c4fb2

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

src/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ import { UtilModule } from "./modules/util/util.module";
8888
})
8989
export class AppModule {
9090
configure(consumer: MiddlewareConsumer) {
91-
consumer.apply(logger).forRoutes("*");
91+
consumer.apply(logger).exclude({ path: "/favicon.ico", method: RequestMethod.GET }, { path: "/", method: RequestMethod.GET }, { path: "/health", method: RequestMethod.GET }).forRoutes("*");
9292
}
9393
}

src/common/middlewares/Logger.middleware.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ import { DiscordWebhook } from "src/utils/discord-webhook";
33

44
const discordWebhook = new DiscordWebhook();
55

6+
const EXCLUDED_PATHS = ["/health", "/"];
7+
68
export function logger(req: Request, res: Response, next: NextFunction) {
9+
const url = req.originalUrl || req.url;
10+
11+
// Skip logging for excluded paths
12+
if (EXCLUDED_PATHS.includes(url)) {
13+
return next();
14+
}
15+
716
const startTime = Date.now();
817
let responseBody: any = null;
918

src/utils/discord-webhook.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export class DiscordWebhook {
2525
return;
2626
}
2727

28+
// Ensure description doesn't exceed Discord's 4096 char limit
29+
if (body.description.length > 4096) {
30+
body.description = `${body.description.slice(0, 4080)}\n...(truncated)`;
31+
}
32+
2833
await axios.post(config.logging.webhookUrl, {
2934
embeds: [body],
3035
allowed_mentions: {
@@ -33,15 +38,29 @@ export class DiscordWebhook {
3338
});
3439

3540
this.logger.success("Logging message has been sent!");
36-
} catch (e) {
37-
this.logger.error(e);
41+
} catch (e: any) {
42+
const errorData = e?.response?.data ? JSON.stringify(e.response.data) : e?.message || "Unknown error";
43+
this.logger.error(`Discord webhook failed: ${errorData}`);
3844
}
3945
}
4046

4147
errorEmbed(...ctx: any[]): EmbedMessage {
48+
const formatted = ctx
49+
.map((item) => {
50+
if (item instanceof Error) {
51+
return `${item.name}: ${item.message}\n${item.stack || ""}`;
52+
}
53+
if (typeof item === "object") {
54+
return JSON.stringify(item, null, 2);
55+
}
56+
return String(item);
57+
})
58+
.join(" ")
59+
.slice(0, 4000);
60+
4261
return {
4362
title: "Service Error!!!",
44-
description: `\`\`\` ${ctx} \`\`\``,
63+
description: `\`\`\`\n${formatted}\n\`\`\``,
4564
color: 16711680,
4665
footer: {
4766
text: "Backend Service | ComCamp 37",

0 commit comments

Comments
 (0)