Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE]: refactor codes and add logger #121

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 211 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"homepage": "https://github.com/Behzad-rabiei/tc-discordBot#readme",
"dependencies": {
"@sentry/node": "^7.51.2",
"@togethercrew.dev/db": "^2.4.95",
"@togethercrew.dev/db": "^2.4.96",
"@togethercrew.dev/tc-messagebroker": "^0.0.40",
"babel-jest": "^29.5.0",
"bullmq": "^3.14.0",
Expand All @@ -36,6 +36,7 @@
"mongodb": "^5.4.0",
"mongoose": "^6.11.1",
"node-fetch": "^2.6.7",
"pino": "^8.15.0",
"redis": "^4.6.6"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const envVarsSchema = Joi.object()
REDIS_HOST: Joi.string().required().description('Reids host'),
REDIS_PORT: Joi.string().required().description('Reids port'),
REDIS_PASSWORD: Joi.string().required().description('Reids password').allow(''),
LOG_LEVEL: Joi.string().required().description('Min allowed log level'),
cyri113 marked this conversation as resolved.
Show resolved Hide resolved
})
.unknown();

Expand Down Expand Up @@ -51,4 +52,7 @@ export default {
dsn: envVars.SENTRY_DSN,
env: envVars.SENTRY_ENV,
},
logger: {
level: envVars.LOG_LEVEL,
},
};
18 changes: 18 additions & 0 deletions src/config/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pino, { Bindings } from 'pino';
import config from './index';

export default pino({
level: config.logger.level,
formatters: {
level: label => {
return { level: label.toUpperCase() };
},
},
timestamp: () => `,"timestamp":"${new Date(Date.now()).toISOString()}"`,
bindings: (bindings: Bindings) => {
return {
pid: bindings.pid,
host: bindings.hostname,
};
},
});
10 changes: 7 additions & 3 deletions src/database/connection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Connection } from 'mongoose';
import parentLogger from '../config/logger';

const logger = parentLogger.child({ module: 'Connection' });

/**
* Closes a given Mongoose connection.
* @param {Connection} connection - The Mongoose connection object to be closed.
Expand All @@ -8,8 +12,8 @@ import { Connection } from 'mongoose';
export async function closeConnection(connection: Connection) {
try {
await connection.close();
console.log('The connection to the database has been successfully closed.');
} catch (err) {
console.log('Error closing connection to the database:', err);
logger.info({ database: connection.name }, 'The connection to database has been successfully closed');
} catch (error) {
logger.fatal({ database: connection.name, error }, 'Failed to close the connection to the database');
}
}
Loading