-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logger): integrate with discord webhook
- Loading branch information
phucvinh57
committed
Nov 21, 2023
1 parent
fe416ca
commit 260e991
Showing
10 changed files
with
305 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
*/ | ||
|
||
export * from './env'; | ||
export * from './logger'; | ||
export * from './swagger'; | ||
export * from './logger/discord'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { envs } from '@configs/env'; | ||
import { WebhookClient } from 'discord.js'; | ||
import moment from 'moment-timezone'; | ||
import build from 'pino-abstract-transport'; | ||
|
||
export default function (options: DiscordLogOptions) { | ||
const threadId = options.threadId; | ||
|
||
const webhookClient = new WebhookClient({ | ||
url: options.webhookUrl | ||
}); | ||
|
||
const fields = options.ignore ? options.ignore.split(',') : null; | ||
const ignoreFields = fields && fields.length > 0 ? fields.map((field) => field.trim()) : null; | ||
|
||
return build(async function (source) { | ||
for await (const obj of source) { | ||
const level = obj.level; | ||
let levelName: LogLevel; | ||
if (level >= 60) levelName = 'FATAL'; | ||
else if (level >= 50) levelName = 'ERROR'; | ||
else if (level >= 40) levelName = 'WARN'; | ||
else levelName = 'INFO'; | ||
|
||
if (ignoreFields) ignoreFields.forEach((field) => delete obj[field]); | ||
const time = moment.tz('Asia/Ho_Chi_Minh').format('HH:mm:ss DD/MM/YYYY'); | ||
const payload = JSON.stringify(obj, null, 2); | ||
|
||
const header = `🪲 [**${envs.NODE_ENV.toUpperCase()}**] [${levelName}] [${time}] 🪲\n`; | ||
const beginBody = '```bash\n'; | ||
const endBody = '\n```'; | ||
if (payload.length > 1800) { | ||
// Split message | ||
const subMessages: string[] = []; | ||
let i = 0; | ||
while (i * 1800 < payload.length) { | ||
subMessages.push(payload.substring(i * 1800, (i + 1) * 1800)); | ||
i++; | ||
} | ||
for (const message of subMessages) { | ||
try { | ||
await webhookClient.send({ | ||
threadId, | ||
content: header + beginBody + message + endBody | ||
}); | ||
} catch (e) { | ||
// Do nothing | ||
} | ||
} | ||
} else { | ||
webhookClient | ||
.send({ | ||
threadId, | ||
content: header + beginBody + payload + endBody | ||
}) | ||
.catch(() => { | ||
// Do nothing | ||
}); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type LogLevel = 'INFO' | 'WARN' | 'DEBUG' | 'ERROR' | 'FATAL'; | ||
|
||
type DiscordLogOptions = { | ||
webhookUrl: string; | ||
threadId?: string; | ||
ignore?: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,53 @@ | ||
import { envs, loggerConfig } from '@configs'; | ||
import { FastifyError } from 'fastify'; | ||
import { PinoLoggerOptions } from 'fastify/types/logger'; | ||
import { envs } from '@configs'; | ||
import pino from 'pino'; | ||
|
||
const errorSerialize = (err: FastifyError) => { | ||
const isInternalServerError = !err.statusCode || err.statusCode === 500; | ||
return { | ||
type: err.name, | ||
stack: isInternalServerError && err.stack ? err.stack : 'null', | ||
message: err.message, | ||
statusCode: err.statusCode | ||
}; | ||
}; | ||
|
||
const fileLogTargets = ['info', 'warn', 'error', 'fatal'].map((logLevel) => ({ | ||
target: 'pino/file', | ||
level: logLevel, | ||
options: { | ||
destination: process.cwd() + `/logs/${logLevel}.log`, | ||
mkdir: true | ||
} | ||
})); | ||
const pinoLogTarget = { | ||
target: 'pino-pretty', | ||
level: 'info', | ||
options: { | ||
translateTime: 'dd/mm/yy HH:MM:ss', | ||
ignore: 'pid,hostname' | ||
} | ||
}; | ||
const discordLogTarget = { | ||
target: '../configs/logger/discord', | ||
level: 'warn', | ||
options: { | ||
webhookUrl: envs.DISCORD_WEBHOOK_URL, | ||
ignore: 'time, pid, hostname' | ||
} as DiscordLogOptions | ||
}; | ||
|
||
const loggerConfig: Record<NodeEnv, PinoLoggerOptions> = { | ||
development: { | ||
transport: { targets: [pinoLogTarget, discordLogTarget] }, | ||
serializers: { err: errorSerialize } | ||
}, | ||
production: { | ||
transport: { targets: [discordLogTarget, ...fileLogTargets] }, | ||
serializers: { err: errorSerialize } | ||
}, | ||
test: { serializers: { err: errorSerialize } } | ||
}; | ||
|
||
export const logger = pino(loggerConfig[envs.NODE_ENV]); |
Oops, something went wrong.